1 /*
2  * Copyright 2012 Sebastian Annies, Hamburg
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.coremedia.iso;
17 
18 import java.nio.ByteBuffer;
19 
20 public final class IsoTypeWriter {
21 
writeUInt64(ByteBuffer bb, long u)22     public static void writeUInt64(ByteBuffer bb, long u) {
23         bb.putLong(u);
24     }
25 
writeUInt32(ByteBuffer bb, long u)26     public static void writeUInt32(ByteBuffer bb, long u) {
27         bb.putInt((int) u);
28 
29     }
30 
writeUInt32BE(ByteBuffer bb, long u)31     public static void writeUInt32BE(ByteBuffer bb, long u) {
32         assert u >= 0 && u <= 1L << 32 : "The given long is not in the range of uint32 (" + u + ")";
33         writeUInt16BE(bb, (int) u & 0xFFFF);
34         writeUInt16BE(bb, (int) ((u >> 16) & 0xFFFF));
35 
36     }
37 
38 
writeUInt24(ByteBuffer bb, int i)39     public static void writeUInt24(ByteBuffer bb, int i) {
40         i = i & 0xFFFFFF;
41         writeUInt16(bb, i >> 8);
42         writeUInt8(bb, i);
43 
44     }
45 
46 
writeUInt16(ByteBuffer bb, int i)47     public static void writeUInt16(ByteBuffer bb, int i) {
48         i = i & 0xFFFF;
49         writeUInt8(bb, i >> 8);
50         writeUInt8(bb, i & 0xFF);
51     }
52 
writeUInt16BE(ByteBuffer bb, int i)53     public static void writeUInt16BE(ByteBuffer bb, int i) {
54         i = i & 0xFFFF;
55         writeUInt8(bb, i & 0xFF);
56         writeUInt8(bb, i >> 8);
57     }
58 
writeUInt8(ByteBuffer bb, int i)59     public static void writeUInt8(ByteBuffer bb, int i) {
60         i = i & 0xFF;
61         bb.put((byte) i);
62     }
63 
64 
writeFixedPont1616(ByteBuffer bb, double v)65     public static void writeFixedPont1616(ByteBuffer bb, double v) {
66         int result = (int) (v * 65536);
67         bb.put((byte) ((result & 0xFF000000) >> 24));
68         bb.put((byte) ((result & 0x00FF0000) >> 16));
69         bb.put((byte) ((result & 0x0000FF00) >> 8));
70         bb.put((byte) ((result & 0x000000FF)));
71     }
72 
writeFixedPont88(ByteBuffer bb, double v)73     public static void writeFixedPont88(ByteBuffer bb, double v) {
74         short result = (short) (v * 256);
75         bb.put((byte) ((result & 0xFF00) >> 8));
76         bb.put((byte) ((result & 0x00FF)));
77     }
78 
writeIso639(ByteBuffer bb, String language)79     public static void writeIso639(ByteBuffer bb, String language) {
80         if (language.getBytes().length != 3) {
81             throw new IllegalArgumentException("\"" + language + "\" language string isn't exactly 3 characters long!");
82         }
83         int bits = 0;
84         for (int i = 0; i < 3; i++) {
85             bits += (language.getBytes()[i] - 0x60) << (2 - i) * 5;
86         }
87         writeUInt16(bb, bits);
88     }
89 
writeUtf8String(ByteBuffer bb, String string)90     public static void writeUtf8String(ByteBuffer bb, String string) {
91 
92         bb.put(Utf8.convert(string));
93         writeUInt8(bb, 0);
94     }
95 }
96