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