1:
30: package ;
31:
32: import ;
33: import ;
34: import ;
35: import ;
36: import ;
37: import ;
38:
39:
44: public class ByteAccessUtilities
45: {
46: private ByteAccessUtilities()
47: {
48: }
49:
50: public static int readUShort (final byte[] data, final int pos)
51: {
52: return ((data[pos] & 0xff) << 8) | (data[pos + 1] & 0xff);
53: }
54:
55: public static long readULong (final byte[] data, final int pos)
56: {
57: final int c1 = (data[pos] & 0xff);
58: final int c2 = (data[pos + 1] & 0xff);
59: final int c3 = (data[pos + 2] & 0xff);
60: final int c4 = (data[pos + 3] & 0xff);
61:
62: long retval = ((long) c1 << 24);
63: retval |= (long)c2 << 16;
64: retval |= (long)c3 << 8;
65: retval |= (long)c4;
66: return retval;
67: }
68:
69: public static float readFixed (final byte[] data, final int pos)
70: {
71: final short mantissa = readShort(data, pos);
72: final int fraction = readUShort(data, pos + 2);
73: if (fraction == 0 || mantissa == 0)
74: {
75: return 0;
76: }
77: return (float) mantissa / (fraction / 16384.0f);
78: }
79:
80: public static long readLongDateTime (final byte[] data, final int pos)
81: {
82: final int c1 = (data[pos] & 0xff);
83: final int c2 = (data[pos + 1] & 0xff);
84: final int c3 = (data[pos + 2] & 0xff);
85: final int c4 = (data[pos + 3] & 0xff);
86: final int c5 = (data[pos + 4] & 0xff);
87: final int c6 = (data[pos + 5] & 0xff);
88: final int c7 = (data[pos + 6] & 0xff);
89: final int c8 = (data[pos + 7] & 0xff);
90:
91: long retval = ((long) c1 << 56);
92: retval |= (long)c2 << 48;
93: retval |= (long)c3 << 40;
94: retval |= (long)c4 << 32;
95: retval |= (long)c5 << 24;
96: retval |= (long)c6 << 16;
97: retval |= (long)c7 << 8;
98: retval |= (long)c8;
99: return retval;
100: }
101:
102: public static byte[] readBytes (final byte[] data,
103: final int pos, final int length)
104: {
105: final byte[] retval = new byte[length];
106: System.arraycopy(data, pos, retval, 0, length);
107: return retval;
108: }
109:
110: public static short readShort (final byte[] data, final int pos)
111: {
112: return (short) ((data[pos] & 0xff) << 8 | (data[pos + 1] & 0xff));
113: }
114:
115: public static int readLong (final byte[] data, final int pos)
116: {
117: int retval = 0;
118: retval |= (long)(data[pos] & 0xff) << 24;
119: retval |= (long)(data[pos + 1] & 0xff) << 16;
120: retval |= (long)(data[pos + 2] & 0xff) << 8;
121: retval |= (long)(data[pos + 3] & 0xff);
122: return retval;
123: }
124:
125: public static int readZStringOffset (final byte[] data, final int pos, final int maxLength)
126: {
127: final int lastPos = Math.min (pos + maxLength, pos + data.length);
128: for (int i = pos; i < lastPos; i++)
129: {
130: if (data[i] == 0)
131: {
132: return i;
133: }
134: }
135:
136: return lastPos;
137: }
138:
139: public static String readZString (final byte[] data, final int pos, final int maxLength, final String encoding)
140: throws EncodingException
141: {
142: final int lastPos = Math.min (pos + maxLength, pos + data.length);
143: for (int i = pos; i < lastPos; i++)
144: {
145: if (data[i] == 0)
146: {
147: return readString(data, pos, i - pos, encoding);
148: }
149: }
150:
151: return readString(data, pos, lastPos, encoding);
152: }
153:
154: public static String readString (final byte[] data, final int pos,
155: final int length, final String encoding)
156: throws EncodingException
157: {
158: final Encoding enc;
159: if ("UTF-16".equals(encoding))
160: {
161: enc = EncodingRegistry.getInstance().getEncoding("UTF-16LE");
162: }
163: else
164: {
165: enc = EncodingRegistry.getInstance().getEncoding(encoding);
166: }
167: final ByteBuffer byteBuffer = new ByteBuffer(data, pos, length);
168: final CodePointBuffer cp = enc.decode(byteBuffer, null);
169: return Utf16LE.getInstance().encodeString(cp);
170: }
171: }