/* * Copyright 2002-2016 Drew Noakes * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * More information about this project is available at: * * https://drewnoakes.com/code/exif/ * https://github.com/drewnoakes/metadata-extractor */ package com.drew.lang; import com.drew.lang.annotations.NotNull; import java.io.IOException; import java.io.UnsupportedEncodingException; /** * Base class for random access data reading operations of common data types. *
* By default, the reader operates with Motorola byte order (big endianness). This can be changed by calling * {@link com.drew.lang.RandomAccessReader#setMotorolaByteOrder(boolean)}. *
* Concrete implementations include: *
index
.
*
* Implementations should not perform any bounds checking in this method. That should be performed
* in validateIndex
and isValidIndex
.
*
* @param index The index from which to read the byte
* @return The read byte value
* @throws IllegalArgumentException index
is negative
* @throws BufferBoundsException if the requested byte is beyond the end of the underlying data source
* @throws IOException if the byte is unable to be read
*/
protected abstract byte getByte(int index) throws IOException;
/**
* Returns the required number of bytes from the specified index from the underlying source.
*
* @param index The index from which the bytes begins in the underlying source
* @param count The number of bytes to be returned
* @return The requested bytes
* @throws IllegalArgumentException index
or count
are negative
* @throws BufferBoundsException if the requested bytes extend beyond the end of the underlying data source
* @throws IOException if the byte is unable to be read
*/
@NotNull
public abstract byte[] getBytes(int index, int count) throws IOException;
/**
* Ensures that the buffered bytes extend to cover the specified index. If not, an attempt is made
* to read to that point.
*
* If the stream ends before the point is reached, a {@link BufferBoundsException} is raised. * * @param index the index from which the required bytes start * @param bytesRequested the number of bytes which are required * @throws IOException if the stream ends before the required number of bytes are acquired */ protected abstract void validateIndex(int index, int bytesRequested) throws IOException; protected abstract boolean isValidIndex(int index, int bytesRequested) throws IOException; /** * Returns the length of the data source in bytes. *
* This is a simple operation for implementations (such as {@link RandomAccessFileReader} and * {@link ByteArrayReader}) that have the entire data source available. *
* Users of this method must be aware that sequentially accessed implementations such as * {@link RandomAccessStreamReader} will have to read and buffer the entire data source in * order to determine the length. * * @return the length of the data source, in bytes. */ public abstract long getLength() throws IOException; /** * Sets the endianness of this reader. *
true
for Motorola (or big) endianness (also known as network byte order), with MSB before LSB.false
for Intel (or little) endianness, with LSB before MSB.true
for Motorola/big endian, false
for Intel/little endian
*/
public void setMotorolaByteOrder(boolean motorolaByteOrder)
{
_isMotorolaByteOrder = motorolaByteOrder;
}
/**
* Gets the endianness of this reader.
* true
for Motorola (or big) endianness (also known as network byte order), with MSB before LSB.false
for Intel (or little) endianness, with LSB before MSB.
* This particular fixed point encoding has one sign bit, 15 numerator bits and 16 denominator bits.
*
* @return the floating point value
* @throws IOException the buffer does not contain enough bytes to service the request, or index is negative
*/
public float getS15Fixed16(int index) throws IOException
{
validateIndex(index, 4);
if (_isMotorolaByteOrder) {
float res = (getByte(index ) & 0xFF) << 8 |
(getByte(index + 1) & 0xFF);
int d = (getByte(index + 2) & 0xFF) << 8 |
(getByte(index + 3) & 0xFF);
return (float)(res + d/65536.0);
} else {
// this particular branch is untested
float res = (getByte(index + 3) & 0xFF) << 8 |
(getByte(index + 2) & 0xFF);
int d = (getByte(index + 1) & 0xFF) << 8 |
(getByte(index ) & 0xFF);
return (float)(res + d/65536.0);
}
}
public float getFloat32(int index) throws IOException
{
return Float.intBitsToFloat(getInt32(index));
}
public double getDouble64(int index) throws IOException
{
return Double.longBitsToDouble(getInt64(index));
}
@NotNull
public String getString(int index, int bytesRequested) throws IOException
{
return new String(getBytes(index, bytesRequested));
}
@NotNull
public String getString(int index, int bytesRequested, String charset) throws IOException
{
byte[] bytes = getBytes(index, bytesRequested);
try {
return new String(bytes, charset);
} catch (UnsupportedEncodingException e) {
return new String(bytes);
}
}
/**
* Creates a String from the _data buffer starting at the specified index,
* and ending where byte=='\0'
or where length==maxLength
.
*
* @param index The index within the buffer at which to start reading the string.
* @param maxLengthBytes The maximum number of bytes to read. If a zero-byte is not reached within this limit,
* reading will stop and the string will be truncated to this length.
* @return The read string.
* @throws IOException The buffer does not contain enough bytes to satisfy this request.
*/
@NotNull
public String getNullTerminatedString(int index, int maxLengthBytes) throws IOException
{
// NOTE currently only really suited to single-byte character strings
byte[] bytes = getBytes(index, maxLengthBytes);
// Count the number of non-null bytes
int length = 0;
while (length < bytes.length && bytes[length] != '\0')
length++;
return new String(bytes, 0, length);
}
}