1 package org.simantics.databoard.util.binary;
3 import java.io.DataInput;
4 import java.io.DataOutput;
5 import java.io.IOException;
8 * DataInput and DataOutput serialize primitive numbers with big endian byte
9 * order. This utility absolutely does nothing but facades byte operations.
11 * @author Toni Kalajainen <toni.kalajainen@iki.fi>
14 public static void writeUInt24(DataOutput out, int value) throws IOException {
15 out.write((value >> 16) & 0xff);
16 out.write((value >> 8) & 0xff);
17 out.write( value & 0xff);
20 public static int readUInt24(DataInput in) throws IOException {
22 ( ( in.readByte() << 16) ) & 0xffffff |
23 ( in.readByte() << 8) |
30 * Write UInt32 with dynamic encoding (1-5 bytes).
36 public static void writeDynamicUInt32(DataOutput out, int length) throws IOException {
38 out.write((byte)length);
43 out.write( ((length&0x3f) | 0x80) );
44 out.write( (length>>>6) );
48 if(length < 0x200000) {
49 out.write( ((length&0x1f) | 0xc0) );
50 out.write( ((length>>>5)&0xff) );
51 out.write( ((length>>>13)&0xff) );
55 if(length < 0x10000000) {
56 out.write( ((length&0x0f) | 0xe0) );
57 out.write( ((length>>>4)&0xff) );
58 out.write( ((length>>>12)&0xff) );
59 out.write( ((length>>>20)&0xff) );
63 out.write( ((length&0x07) | 0xf0) );
64 out.write( ((length>>>3)&0xff) );
65 out.write( ((length>>>11)&0xff) );
66 out.write( ((length>>>19)&0xff) );
67 out.write( ((length>>>27)&0xff) );
75 public static int readDynamicUInt32(DataInput in) throws IOException {
76 int length = in.readByte()&0xff;
82 length += ((in.readByte()&0xff)<<3);
83 length += ((in.readByte()&0xff)<<11);
84 length += ((in.readByte()&0xff)<<19);
89 length += ((in.readByte()&0xff)<<4);
90 length += ((in.readByte()&0xff)<<12);
91 length += ((in.readByte()&0xff)<<20);
97 length += ((in.readByte()&0xff)<<5);
98 length += ((in.readByte()&0xff)<<13);
104 length += ((in.readByte()&0xff)<<6);
113 * Get number of bytes for dynamic encoding of UInt32 (1-5 bytes)
115 * @param length length value
116 * @return bytes required (1-5)
118 public static int getDynamicUInt32Length(int length)
120 if(length < 0x80) return 1;
121 if(length < 0x4080) return 2;
122 if(length < 0x204000) return 3;
123 if(length < 0x10200000) return 4;
129 * Decode an unsigned integer. The number of bytes read depends on maxValue.
134 * @throws IOException
136 public static int getUInt(DataInput in, int maxValue)
139 if (maxValue==0) return 0;
140 if (maxValue<0x100) {
141 return in.readByte() & 0xFF;
142 } else if (maxValue<0x10000) {
143 return in.readShort() & 0xFFFF;
144 } else if (maxValue<0x1000000) {
145 return Endian.readUInt24(in) & 0xFFFFFF;
152 * Calculate unsigned integer encoding length.
157 public static int getUIntLength(int maxValue)
161 } else if (maxValue<0x100) {
163 } else if (maxValue<0x10000) {
165 } else if (maxValue<0x1000000) {
173 * Encode and write an unsigned integer. The number of bytes written
174 * depends on the maxValue.
179 * @throws IOException
181 public static void putUInt(DataOutput out, int value, int maxValue)
184 else if (maxValue<0x100) {
186 } else if (maxValue<0x10000) {
187 out.writeShort(value);
188 } else if (maxValue<0x1000000) {
189 writeUInt24(out, value);