source: josm/trunk/src/com/drew/lang/RandomAccessReader.java@ 8132

Last change on this file since 8132 was 8132, checked in by Don-vip, 10 years ago

fix #11162 - update to metadata-extractor 2.7.2

File size: 14.4 KB
Line 
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
22package com.drew.lang;
23
24import com.drew.lang.annotations.NotNull;
25
26import java.io.IOException;
27import 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 */
43public 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 * Returns an unsigned 8-bit int calculated from one byte of data at the specified index.
130 *
131 * @param index position within the data buffer to read byte
132 * @return the 8 bit int value, between 0 and 255
133 * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
134 */
135 public short getUInt8(int index) throws IOException
136 {
137 validateIndex(index, 1);
138
139 return (short) (getByte(index) & 0xFF);
140 }
141
142 /**
143 * Returns a signed 8-bit int calculated from one byte of data at the specified index.
144 *
145 * @param index position within the data buffer to read byte
146 * @return the 8 bit int value, between 0x00 and 0xFF
147 * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
148 */
149 public byte getInt8(int index) throws IOException
150 {
151 validateIndex(index, 1);
152
153 return getByte(index);
154 }
155
156 /**
157 * Returns an unsigned 16-bit int calculated from two bytes of data at the specified index.
158 *
159 * @param index position within the data buffer to read first byte
160 * @return the 16 bit int value, between 0x0000 and 0xFFFF
161 * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
162 */
163 public int getUInt16(int index) throws IOException
164 {
165 validateIndex(index, 2);
166
167 if (_isMotorolaByteOrder) {
168 // Motorola - MSB first
169 return (getByte(index ) << 8 & 0xFF00) |
170 (getByte(index + 1) & 0xFF);
171 } else {
172 // Intel ordering - LSB first
173 return (getByte(index + 1) << 8 & 0xFF00) |
174 (getByte(index ) & 0xFF);
175 }
176 }
177
178 /**
179 * Returns a signed 16-bit int calculated from two bytes of data at the specified index (MSB, LSB).
180 *
181 * @param index position within the data buffer to read first byte
182 * @return the 16 bit int value, between 0x0000 and 0xFFFF
183 * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
184 */
185 public short getInt16(int index) throws IOException
186 {
187 validateIndex(index, 2);
188
189 if (_isMotorolaByteOrder) {
190 // Motorola - MSB first
191 return (short) (((short)getByte(index ) << 8 & (short)0xFF00) |
192 ((short)getByte(index + 1) & (short)0xFF));
193 } else {
194 // Intel ordering - LSB first
195 return (short) (((short)getByte(index + 1) << 8 & (short)0xFF00) |
196 ((short)getByte(index ) & (short)0xFF));
197 }
198 }
199
200 /**
201 * Get a 32-bit unsigned integer from the buffer, returning it as a long.
202 *
203 * @param index position within the data buffer to read first byte
204 * @return the unsigned 32-bit int value as a long, between 0x00000000 and 0xFFFFFFFF
205 * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
206 */
207 public long getUInt32(int index) throws IOException
208 {
209 validateIndex(index, 4);
210
211 if (_isMotorolaByteOrder) {
212 // Motorola - MSB first (big endian)
213 return (((long)getByte(index )) << 24 & 0xFF000000L) |
214 (((long)getByte(index + 1)) << 16 & 0xFF0000L) |
215 (((long)getByte(index + 2)) << 8 & 0xFF00L) |
216 (((long)getByte(index + 3)) & 0xFFL);
217 } else {
218 // Intel ordering - LSB first (little endian)
219 return (((long)getByte(index + 3)) << 24 & 0xFF000000L) |
220 (((long)getByte(index + 2)) << 16 & 0xFF0000L) |
221 (((long)getByte(index + 1)) << 8 & 0xFF00L) |
222 (((long)getByte(index )) & 0xFFL);
223 }
224 }
225
226 /**
227 * Returns a signed 32-bit integer from four bytes of data at the specified index the buffer.
228 *
229 * @param index position within the data buffer to read first byte
230 * @return the signed 32 bit int value, between 0x00000000 and 0xFFFFFFFF
231 * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
232 */
233 public int getInt32(int index) throws IOException
234 {
235 validateIndex(index, 4);
236
237 if (_isMotorolaByteOrder) {
238 // Motorola - MSB first (big endian)
239 return (getByte(index ) << 24 & 0xFF000000) |
240 (getByte(index + 1) << 16 & 0xFF0000) |
241 (getByte(index + 2) << 8 & 0xFF00) |
242 (getByte(index + 3) & 0xFF);
243 } else {
244 // Intel ordering - LSB first (little endian)
245 return (getByte(index + 3) << 24 & 0xFF000000) |
246 (getByte(index + 2) << 16 & 0xFF0000) |
247 (getByte(index + 1) << 8 & 0xFF00) |
248 (getByte(index ) & 0xFF);
249 }
250 }
251
252 /**
253 * Get a signed 64-bit integer from the buffer.
254 *
255 * @param index position within the data buffer to read first byte
256 * @return the 64 bit int value, between 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF
257 * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
258 */
259 public long getInt64(int index) throws IOException
260 {
261 validateIndex(index, 8);
262
263 if (_isMotorolaByteOrder) {
264 // Motorola - MSB first
265 return ((long)getByte(index ) << 56 & 0xFF00000000000000L) |
266 ((long)getByte(index + 1) << 48 & 0xFF000000000000L) |
267 ((long)getByte(index + 2) << 40 & 0xFF0000000000L) |
268 ((long)getByte(index + 3) << 32 & 0xFF00000000L) |
269 ((long)getByte(index + 4) << 24 & 0xFF000000L) |
270 ((long)getByte(index + 5) << 16 & 0xFF0000L) |
271 ((long)getByte(index + 6) << 8 & 0xFF00L) |
272 ((long)getByte(index + 7) & 0xFFL);
273 } else {
274 // Intel ordering - LSB first
275 return ((long)getByte(index + 7) << 56 & 0xFF00000000000000L) |
276 ((long)getByte(index + 6) << 48 & 0xFF000000000000L) |
277 ((long)getByte(index + 5) << 40 & 0xFF0000000000L) |
278 ((long)getByte(index + 4) << 32 & 0xFF00000000L) |
279 ((long)getByte(index + 3) << 24 & 0xFF000000L) |
280 ((long)getByte(index + 2) << 16 & 0xFF0000L) |
281 ((long)getByte(index + 1) << 8 & 0xFF00L) |
282 ((long)getByte(index ) & 0xFFL);
283 }
284 }
285
286 /**
287 * Gets a s15.16 fixed point float from the buffer.
288 * <p>
289 * This particular fixed point encoding has one sign bit, 15 numerator bits and 16 denominator bits.
290 *
291 * @return the floating point value
292 * @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
293 */
294 public float getS15Fixed16(int index) throws IOException
295 {
296 validateIndex(index, 4);
297
298 if (_isMotorolaByteOrder) {
299 float res = (getByte(index ) & 0xFF) << 8 |
300 (getByte(index + 1) & 0xFF);
301 int d = (getByte(index + 2) & 0xFF) << 8 |
302 (getByte(index + 3) & 0xFF);
303 return (float)(res + d/65536.0);
304 } else {
305 // this particular branch is untested
306 float res = (getByte(index + 3) & 0xFF) << 8 |
307 (getByte(index + 2) & 0xFF);
308 int d = (getByte(index + 1) & 0xFF) << 8 |
309 (getByte(index ) & 0xFF);
310 return (float)(res + d/65536.0);
311 }
312 }
313
314 public float getFloat32(int index) throws IOException
315 {
316 return Float.intBitsToFloat(getInt32(index));
317 }
318
319 public double getDouble64(int index) throws IOException
320 {
321 return Double.longBitsToDouble(getInt64(index));
322 }
323
324 @NotNull
325 public String getString(int index, int bytesRequested) throws IOException
326 {
327 return new String(getBytes(index, bytesRequested));
328 }
329
330 @NotNull
331 public String getString(int index, int bytesRequested, String charset) throws IOException
332 {
333 byte[] bytes = getBytes(index, bytesRequested);
334 try {
335 return new String(bytes, charset);
336 } catch (UnsupportedEncodingException e) {
337 return new String(bytes);
338 }
339 }
340
341 /**
342 * Creates a String from the _data buffer starting at the specified index,
343 * and ending where <code>byte=='\0'</code> or where <code>length==maxLength</code>.
344 *
345 * @param index The index within the buffer at which to start reading the string.
346 * @param maxLengthBytes The maximum number of bytes to read. If a zero-byte is not reached within this limit,
347 * reading will stop and the string will be truncated to this length.
348 * @return The read string.
349 * @throws IOException The buffer does not contain enough bytes to satisfy this request.
350 */
351 @NotNull
352 public String getNullTerminatedString(int index, int maxLengthBytes) throws IOException
353 {
354 // NOTE currently only really suited to single-byte character strings
355
356 byte[] bytes = getBytes(index, maxLengthBytes);
357
358 // Count the number of non-null bytes
359 int length = 0;
360 while (length < bytes.length && bytes[length] != '\0')
361 length++;
362
363 return new String(bytes, 0, length);
364 }
365}
Note: See TracBrowser for help on using the repository browser.