1 | /*
|
---|
2 | * Copyright 2002-2015 Drew Noakes
|
---|
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 | * More information about this project is available at:
|
---|
17 | *
|
---|
18 | * https://drewnoakes.com/code/exif/
|
---|
19 | * https://github.com/drewnoakes/metadata-extractor
|
---|
20 | */
|
---|
21 |
|
---|
22 | package com.drew.lang;
|
---|
23 |
|
---|
24 | import com.drew.lang.annotations.NotNull;
|
---|
25 |
|
---|
26 | import java.io.IOException;
|
---|
27 | import java.io.UnsupportedEncodingException;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Base class for random access data reading operations of common data types.
|
---|
31 | * <p>
|
---|
32 | * By default, the reader operates with Motorola byte order (big endianness). This can be changed by calling
|
---|
33 | * {@link com.drew.lang.RandomAccessReader#setMotorolaByteOrder(boolean)}.
|
---|
34 | * <p>
|
---|
35 | * Concrete implementations include:
|
---|
36 | * <ul>
|
---|
37 | * <li>{@link ByteArrayReader}</li>
|
---|
38 | * <li>{@link RandomAccessStreamReader}</li>
|
---|
39 | * </ul>
|
---|
40 | *
|
---|
41 | * @author Drew Noakes https://drewnoakes.com
|
---|
42 | */
|
---|
43 | public abstract class RandomAccessReader
|
---|
44 | {
|
---|
45 | private boolean _isMotorolaByteOrder = true;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Gets the byte value at the specified byte <code>index</code>.
|
---|
49 | * <p>
|
---|
50 | * Implementations should not perform any bounds checking in this method. That should be performed
|
---|
51 | * in <code>validateIndex</code> and <code>isValidIndex</code>.
|
---|
52 | *
|
---|
53 | * @param index The index from which to read the byte
|
---|
54 | * @return The read byte value
|
---|
55 | * @throws IllegalArgumentException <code>index</code> or <code>count</code> are negative
|
---|
56 | * @throws BufferBoundsException if the requested byte is beyond the end of the underlying data source
|
---|
57 | * @throws IOException if the byte is unable to be read
|
---|
58 | */
|
---|
59 | protected abstract byte getByte(int index) throws IOException;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Returns the required number of bytes from the specified index from the underlying source.
|
---|
63 | *
|
---|
64 | * @param index The index from which the bytes begins in the underlying source
|
---|
65 | * @param count The number of bytes to be returned
|
---|
66 | * @return The requested bytes
|
---|
67 | * @throws IllegalArgumentException <code>index</code> or <code>count</code> are negative
|
---|
68 | * @throws BufferBoundsException if the requested bytes extend beyond the end of the underlying data source
|
---|
69 | * @throws IOException if the byte is unable to be read
|
---|
70 | */
|
---|
71 | @NotNull
|
---|
72 | public abstract byte[] getBytes(int index, int count) throws IOException;
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Ensures that the buffered bytes extend to cover the specified index. If not, an attempt is made
|
---|
76 | * to read to that point.
|
---|
77 | * <p>
|
---|
78 | * If the stream ends before the point is reached, a {@link BufferBoundsException} is raised.
|
---|
79 | *
|
---|
80 | * @param index the index from which the required bytes start
|
---|
81 | * @param bytesRequested the number of bytes which are required
|
---|
82 | * @throws IOException if the stream ends before the required number of bytes are acquired
|
---|
83 | */
|
---|
84 | protected abstract void validateIndex(int index, int bytesRequested) throws IOException;
|
---|
85 |
|
---|
86 | protected abstract boolean isValidIndex(int index, int bytesRequested) throws IOException;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Returns the length of the data source in bytes.
|
---|
90 | * <p>
|
---|
91 | * This is a simple operation for implementations (such as {@link RandomAccessFileReader} and
|
---|
92 | * {@link ByteArrayReader}) that have the entire data source available.
|
---|
93 | * <p>
|
---|
94 | * Users of this method must be aware that sequentially accessed implementations such as
|
---|
95 | * {@link RandomAccessStreamReader} will have to read and buffer the entire data source in
|
---|
96 | * order to determine the length.
|
---|
97 | *
|
---|
98 | * @return the length of the data source, in bytes.
|
---|
99 | */
|
---|
100 | public abstract long getLength() throws IOException;
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Sets the endianness of this reader.
|
---|
104 | * <ul>
|
---|
105 | * <li><code>true</code> for Motorola (or big) endianness (also known as network byte order), with MSB before LSB.</li>
|
---|
106 | * <li><code>false</code> for Intel (or little) endianness, with LSB before MSB.</li>
|
---|
107 | * </ul>
|
---|
108 | *
|
---|
109 | * @param motorolaByteOrder <code>true</code> for Motorola/big endian, <code>false</code> for Intel/little endian
|
---|
110 | */
|
---|
111 | public void setMotorolaByteOrder(boolean motorolaByteOrder)
|
---|
112 | {
|
---|
113 | _isMotorolaByteOrder = motorolaByteOrder;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Gets the endianness of this reader.
|
---|
118 | * <ul>
|
---|
119 | * <li><code>true</code> for Motorola (or big) endianness (also known as network byte order), with MSB before LSB.</li>
|
---|
120 | * <li><code>false</code> for Intel (or little) endianness, with LSB before MSB.</li>
|
---|
121 | * </ul>
|
---|
122 | */
|
---|
123 | public boolean isMotorolaByteOrder()
|
---|
124 | {
|
---|
125 | return _isMotorolaByteOrder;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Gets whether a bit at a specific index is set or not.
|
---|
130 | *
|
---|
131 | * @param index the number of bits at which to test
|
---|
132 | * @return true if the bit is set, otherwise false
|
---|
133 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
134 | */
|
---|
135 | public boolean getBit(int index) throws IOException
|
---|
136 | {
|
---|
137 | int byteIndex = index / 8;
|
---|
138 | int bitIndex = index % 8;
|
---|
139 |
|
---|
140 | validateIndex(byteIndex, 1);
|
---|
141 |
|
---|
142 | byte b = getByte(byteIndex);
|
---|
143 | return ((b >> bitIndex) & 1) == 1;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Returns an unsigned 8-bit int calculated from one byte of data at the specified index.
|
---|
148 | *
|
---|
149 | * @param index position within the data buffer to read byte
|
---|
150 | * @return the 8 bit int value, between 0 and 255
|
---|
151 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
152 | */
|
---|
153 | public short getUInt8(int index) throws IOException
|
---|
154 | {
|
---|
155 | validateIndex(index, 1);
|
---|
156 |
|
---|
157 | return (short) (getByte(index) & 0xFF);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Returns a signed 8-bit int calculated from one byte of data at the specified index.
|
---|
162 | *
|
---|
163 | * @param index position within the data buffer to read byte
|
---|
164 | * @return the 8 bit int value, between 0x00 and 0xFF
|
---|
165 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
166 | */
|
---|
167 | public byte getInt8(int index) throws IOException
|
---|
168 | {
|
---|
169 | validateIndex(index, 1);
|
---|
170 |
|
---|
171 | return getByte(index);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Returns an unsigned 16-bit int calculated from two bytes of data at the specified index.
|
---|
176 | *
|
---|
177 | * @param index position within the data buffer to read first byte
|
---|
178 | * @return the 16 bit int value, between 0x0000 and 0xFFFF
|
---|
179 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
180 | */
|
---|
181 | public int getUInt16(int index) throws IOException
|
---|
182 | {
|
---|
183 | validateIndex(index, 2);
|
---|
184 |
|
---|
185 | if (_isMotorolaByteOrder) {
|
---|
186 | // Motorola - MSB first
|
---|
187 | return (getByte(index ) << 8 & 0xFF00) |
|
---|
188 | (getByte(index + 1) & 0xFF);
|
---|
189 | } else {
|
---|
190 | // Intel ordering - LSB first
|
---|
191 | return (getByte(index + 1) << 8 & 0xFF00) |
|
---|
192 | (getByte(index ) & 0xFF);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Returns a signed 16-bit int calculated from two bytes of data at the specified index (MSB, LSB).
|
---|
198 | *
|
---|
199 | * @param index position within the data buffer to read first byte
|
---|
200 | * @return the 16 bit int value, between 0x0000 and 0xFFFF
|
---|
201 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
202 | */
|
---|
203 | public short getInt16(int index) throws IOException
|
---|
204 | {
|
---|
205 | validateIndex(index, 2);
|
---|
206 |
|
---|
207 | if (_isMotorolaByteOrder) {
|
---|
208 | // Motorola - MSB first
|
---|
209 | return (short) (((short)getByte(index ) << 8 & (short)0xFF00) |
|
---|
210 | ((short)getByte(index + 1) & (short)0xFF));
|
---|
211 | } else {
|
---|
212 | // Intel ordering - LSB first
|
---|
213 | return (short) (((short)getByte(index + 1) << 8 & (short)0xFF00) |
|
---|
214 | ((short)getByte(index ) & (short)0xFF));
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Get a 24-bit unsigned integer from the buffer, returning it as an int.
|
---|
220 | *
|
---|
221 | * @param index position within the data buffer to read first byte
|
---|
222 | * @return the unsigned 24-bit int value as a long, between 0x00000000 and 0x00FFFFFF
|
---|
223 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
224 | */
|
---|
225 | public int getInt24(int index) throws IOException
|
---|
226 | {
|
---|
227 | validateIndex(index, 3);
|
---|
228 |
|
---|
229 | if (_isMotorolaByteOrder) {
|
---|
230 | // Motorola - MSB first (big endian)
|
---|
231 | return (((int)getByte(index )) << 16 & 0xFF0000) |
|
---|
232 | (((int)getByte(index + 1)) << 8 & 0xFF00) |
|
---|
233 | (((int)getByte(index + 2)) & 0xFF);
|
---|
234 | } else {
|
---|
235 | // Intel ordering - LSB first (little endian)
|
---|
236 | return (((int)getByte(index + 2)) << 16 & 0xFF0000) |
|
---|
237 | (((int)getByte(index + 1)) << 8 & 0xFF00) |
|
---|
238 | (((int)getByte(index )) & 0xFF);
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Get a 32-bit unsigned integer from the buffer, returning it as a long.
|
---|
244 | *
|
---|
245 | * @param index position within the data buffer to read first byte
|
---|
246 | * @return the unsigned 32-bit int value as a long, between 0x00000000 and 0xFFFFFFFF
|
---|
247 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
248 | */
|
---|
249 | public long getUInt32(int index) throws IOException
|
---|
250 | {
|
---|
251 | validateIndex(index, 4);
|
---|
252 |
|
---|
253 | if (_isMotorolaByteOrder) {
|
---|
254 | // Motorola - MSB first (big endian)
|
---|
255 | return (((long)getByte(index )) << 24 & 0xFF000000L) |
|
---|
256 | (((long)getByte(index + 1)) << 16 & 0xFF0000L) |
|
---|
257 | (((long)getByte(index + 2)) << 8 & 0xFF00L) |
|
---|
258 | (((long)getByte(index + 3)) & 0xFFL);
|
---|
259 | } else {
|
---|
260 | // Intel ordering - LSB first (little endian)
|
---|
261 | return (((long)getByte(index + 3)) << 24 & 0xFF000000L) |
|
---|
262 | (((long)getByte(index + 2)) << 16 & 0xFF0000L) |
|
---|
263 | (((long)getByte(index + 1)) << 8 & 0xFF00L) |
|
---|
264 | (((long)getByte(index )) & 0xFFL);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Returns a signed 32-bit integer from four bytes of data at the specified index the buffer.
|
---|
270 | *
|
---|
271 | * @param index position within the data buffer to read first byte
|
---|
272 | * @return the signed 32 bit int value, between 0x00000000 and 0xFFFFFFFF
|
---|
273 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
274 | */
|
---|
275 | public int getInt32(int index) throws IOException
|
---|
276 | {
|
---|
277 | validateIndex(index, 4);
|
---|
278 |
|
---|
279 | if (_isMotorolaByteOrder) {
|
---|
280 | // Motorola - MSB first (big endian)
|
---|
281 | return (getByte(index ) << 24 & 0xFF000000) |
|
---|
282 | (getByte(index + 1) << 16 & 0xFF0000) |
|
---|
283 | (getByte(index + 2) << 8 & 0xFF00) |
|
---|
284 | (getByte(index + 3) & 0xFF);
|
---|
285 | } else {
|
---|
286 | // Intel ordering - LSB first (little endian)
|
---|
287 | return (getByte(index + 3) << 24 & 0xFF000000) |
|
---|
288 | (getByte(index + 2) << 16 & 0xFF0000) |
|
---|
289 | (getByte(index + 1) << 8 & 0xFF00) |
|
---|
290 | (getByte(index ) & 0xFF);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Get a signed 64-bit integer from the buffer.
|
---|
296 | *
|
---|
297 | * @param index position within the data buffer to read first byte
|
---|
298 | * @return the 64 bit int value, between 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF
|
---|
299 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
300 | */
|
---|
301 | public long getInt64(int index) throws IOException
|
---|
302 | {
|
---|
303 | validateIndex(index, 8);
|
---|
304 |
|
---|
305 | if (_isMotorolaByteOrder) {
|
---|
306 | // Motorola - MSB first
|
---|
307 | return ((long)getByte(index ) << 56 & 0xFF00000000000000L) |
|
---|
308 | ((long)getByte(index + 1) << 48 & 0xFF000000000000L) |
|
---|
309 | ((long)getByte(index + 2) << 40 & 0xFF0000000000L) |
|
---|
310 | ((long)getByte(index + 3) << 32 & 0xFF00000000L) |
|
---|
311 | ((long)getByte(index + 4) << 24 & 0xFF000000L) |
|
---|
312 | ((long)getByte(index + 5) << 16 & 0xFF0000L) |
|
---|
313 | ((long)getByte(index + 6) << 8 & 0xFF00L) |
|
---|
314 | ((long)getByte(index + 7) & 0xFFL);
|
---|
315 | } else {
|
---|
316 | // Intel ordering - LSB first
|
---|
317 | return ((long)getByte(index + 7) << 56 & 0xFF00000000000000L) |
|
---|
318 | ((long)getByte(index + 6) << 48 & 0xFF000000000000L) |
|
---|
319 | ((long)getByte(index + 5) << 40 & 0xFF0000000000L) |
|
---|
320 | ((long)getByte(index + 4) << 32 & 0xFF00000000L) |
|
---|
321 | ((long)getByte(index + 3) << 24 & 0xFF000000L) |
|
---|
322 | ((long)getByte(index + 2) << 16 & 0xFF0000L) |
|
---|
323 | ((long)getByte(index + 1) << 8 & 0xFF00L) |
|
---|
324 | ((long)getByte(index ) & 0xFFL);
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Gets a s15.16 fixed point float from the buffer.
|
---|
330 | * <p>
|
---|
331 | * This particular fixed point encoding has one sign bit, 15 numerator bits and 16 denominator bits.
|
---|
332 | *
|
---|
333 | * @return the floating point value
|
---|
334 | * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
|
---|
335 | */
|
---|
336 | public float getS15Fixed16(int index) throws IOException
|
---|
337 | {
|
---|
338 | validateIndex(index, 4);
|
---|
339 |
|
---|
340 | if (_isMotorolaByteOrder) {
|
---|
341 | float res = (getByte(index ) & 0xFF) << 8 |
|
---|
342 | (getByte(index + 1) & 0xFF);
|
---|
343 | int d = (getByte(index + 2) & 0xFF) << 8 |
|
---|
344 | (getByte(index + 3) & 0xFF);
|
---|
345 | return (float)(res + d/65536.0);
|
---|
346 | } else {
|
---|
347 | // this particular branch is untested
|
---|
348 | float res = (getByte(index + 3) & 0xFF) << 8 |
|
---|
349 | (getByte(index + 2) & 0xFF);
|
---|
350 | int d = (getByte(index + 1) & 0xFF) << 8 |
|
---|
351 | (getByte(index ) & 0xFF);
|
---|
352 | return (float)(res + d/65536.0);
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | public float getFloat32(int index) throws IOException
|
---|
357 | {
|
---|
358 | return Float.intBitsToFloat(getInt32(index));
|
---|
359 | }
|
---|
360 |
|
---|
361 | public double getDouble64(int index) throws IOException
|
---|
362 | {
|
---|
363 | return Double.longBitsToDouble(getInt64(index));
|
---|
364 | }
|
---|
365 |
|
---|
366 | @NotNull
|
---|
367 | public String getString(int index, int bytesRequested) throws IOException
|
---|
368 | {
|
---|
369 | return new String(getBytes(index, bytesRequested));
|
---|
370 | }
|
---|
371 |
|
---|
372 | @NotNull
|
---|
373 | public String getString(int index, int bytesRequested, String charset) throws IOException
|
---|
374 | {
|
---|
375 | byte[] bytes = getBytes(index, bytesRequested);
|
---|
376 | try {
|
---|
377 | return new String(bytes, charset);
|
---|
378 | } catch (UnsupportedEncodingException e) {
|
---|
379 | return new String(bytes);
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Creates a String from the _data buffer starting at the specified index,
|
---|
385 | * and ending where <code>byte=='\0'</code> or where <code>length==maxLength</code>.
|
---|
386 | *
|
---|
387 | * @param index The index within the buffer at which to start reading the string.
|
---|
388 | * @param maxLengthBytes The maximum number of bytes to read. If a zero-byte is not reached within this limit,
|
---|
389 | * reading will stop and the string will be truncated to this length.
|
---|
390 | * @return The read string.
|
---|
391 | * @throws IOException The buffer does not contain enough bytes to satisfy this request.
|
---|
392 | */
|
---|
393 | @NotNull
|
---|
394 | public String getNullTerminatedString(int index, int maxLengthBytes) throws IOException
|
---|
395 | {
|
---|
396 | // NOTE currently only really suited to single-byte character strings
|
---|
397 |
|
---|
398 | byte[] bytes = getBytes(index, maxLengthBytes);
|
---|
399 |
|
---|
400 | // Count the number of non-null bytes
|
---|
401 | int length = 0;
|
---|
402 | while (length < bytes.length && bytes[length] != '\0')
|
---|
403 | length++;
|
---|
404 |
|
---|
405 | return new String(bytes, 0, length);
|
---|
406 | }
|
---|
407 | }
|
---|