Class MetadataExtractor.RandomAccessReader
- java.lang.Object
-
- gov.nih.mipav.model.file.MetadataExtractor.RandomAccessReader
-
- Direct Known Subclasses:
MetadataExtractor.ByteArrayReader
,MetadataExtractor.RandomAccessFileReader
,MetadataExtractor.RandomAccessStreamReader
- Enclosing class:
- MetadataExtractor
public abstract class MetadataExtractor.RandomAccessReader extends java.lang.Object
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
com.drew.lang.RandomAccessReader#setMotorolaByteOrder(boolean)
.Concrete implementations include:
- Author:
- Drew Noakes https://drewnoakes.com
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
_isMotorolaByteOrder
-
Constructor Summary
Constructors Constructor Description RandomAccessReader()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
getBit(int index)
Gets whether a bit at a specific index is set or not.abstract byte
getByte(int index)
Gets the byte value at the specified byteindex
.abstract byte[]
getBytes(int index, int count)
Returns the required number of bytes from the specified index from the underlying source.double
getDouble64(int index)
float
getFloat32(int index)
short
getInt16(int index)
Returns a signed 16-bit int calculated from two bytes of data at the specified index (MSB, LSB).int
getInt24(int index)
Get a 24-bit unsigned integer from the buffer, returning it as an int.int
getInt32(int index)
Returns a signed 32-bit integer from four bytes of data at the specified index the buffer.long
getInt64(int index)
Get a signed 64-bit integer from the buffer.byte
getInt8(int index)
Returns a signed 8-bit int calculated from one byte of data at the specified index.abstract long
getLength()
Returns the length of the data source in bytes.byte[]
getNullTerminatedBytes(int index, int maxLengthBytes)
Returns the sequence of bytes punctuated by a\0
value.java.lang.String
getNullTerminatedString(int index, int maxLengthBytes, java.nio.charset.Charset charset)
Creates a String from the _data buffer starting at the specified index, and ending wherebyte=='\0'
or wherelength==maxLength
.MetadataExtractor.StringValue
getNullTerminatedStringValue(int index, int maxLengthBytes, java.nio.charset.Charset charset)
float
getS15Fixed16(int index)
Gets a s15.16 fixed point float from the buffer.java.lang.String
getString(int index, int bytesRequested, java.lang.String charset)
java.lang.String
getString(int index, int bytesRequested, java.nio.charset.Charset charset)
MetadataExtractor.StringValue
getStringValue(int index, int bytesRequested, java.nio.charset.Charset charset)
int
getUInt16(int index)
Returns an unsigned 16-bit int calculated from two bytes of data at the specified index.long
getUInt32(int index)
Get a 32-bit unsigned integer from the buffer, returning it as a long.short
getUInt8(int index)
Returns an unsigned 8-bit int calculated from one byte of data at the specified index.boolean
isMotorolaByteOrder()
Gets the endianness of this reader.protected abstract boolean
isValidIndex(int index, int bytesRequested)
void
setMotorolaByteOrder(boolean motorolaByteOrder)
Sets the endianness of this reader.abstract int
toUnshiftedOffset(int localOffset)
protected abstract void
validateIndex(int index, int bytesRequested)
Ensures that the buffered bytes extend to cover the specified index.
-
-
-
Method Detail
-
toUnshiftedOffset
public abstract int toUnshiftedOffset(int localOffset)
-
getByte
public abstract byte getByte(int index) throws java.io.IOException
Gets the byte value at the specified byteindex
.Implementations should not perform any bounds checking in this method. That should be performed in
validateIndex
andisValidIndex
.- Parameters:
index
- The index from which to read the byte- Returns:
- The read byte value
- Throws:
java.lang.IllegalArgumentException
-index
is negativeMetadataExtractor.BufferBoundsException
- if the requested byte is beyond the end of the underlying data sourcejava.io.IOException
- if the byte is unable to be read
-
getBytes
public abstract byte[] getBytes(int index, int count) throws java.io.IOException
Returns the required number of bytes from the specified index from the underlying source.- Parameters:
index
- The index from which the bytes begins in the underlying sourcecount
- The number of bytes to be returned- Returns:
- The requested bytes
- Throws:
java.lang.IllegalArgumentException
-index
orcount
are negativeMetadataExtractor.BufferBoundsException
- if the requested bytes extend beyond the end of the underlying data sourcejava.io.IOException
- if the byte is unable to be read
-
validateIndex
protected abstract void validateIndex(int index, int bytesRequested) throws java.io.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
MetadataExtractor.BufferBoundsException
is raised.- Parameters:
index
- the index from which the required bytes startbytesRequested
- the number of bytes which are required- Throws:
java.io.IOException
- if the stream ends before the required number of bytes are acquired
-
isValidIndex
protected abstract boolean isValidIndex(int index, int bytesRequested) throws java.io.IOException
- Throws:
java.io.IOException
-
getLength
public abstract long getLength() throws java.io.IOException
Returns the length of the data source in bytes.This is a simple operation for implementations (such as
MetadataExtractor.RandomAccessFileReader
andMetadataExtractor.ByteArrayReader
) that have the entire data source available.Users of this method must be aware that sequentially accessed implementations such as
MetadataExtractor.RandomAccessStreamReader
will have to read and buffer the entire data source in order to determine the length.- Returns:
- the length of the data source, in bytes.
- Throws:
java.io.IOException
-
setMotorolaByteOrder
public void setMotorolaByteOrder(boolean motorolaByteOrder)
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.
- Parameters:
motorolaByteOrder
-true
for Motorola/big endian,false
for Intel/little endian
-
isMotorolaByteOrder
public boolean isMotorolaByteOrder()
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.
-
getBit
public boolean getBit(int index) throws java.io.IOException
Gets whether a bit at a specific index is set or not.- Parameters:
index
- the number of bits at which to test- Returns:
- true if the bit is set, otherwise false
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getUInt8
public short getUInt8(int index) throws java.io.IOException
Returns an unsigned 8-bit int calculated from one byte of data at the specified index.- Parameters:
index
- position within the data buffer to read byte- Returns:
- the 8 bit int value, between 0 and 255
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getInt8
public byte getInt8(int index) throws java.io.IOException
Returns a signed 8-bit int calculated from one byte of data at the specified index.- Parameters:
index
- position within the data buffer to read byte- Returns:
- the 8 bit int value, between 0x00 and 0xFF
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getUInt16
public int getUInt16(int index) throws java.io.IOException
Returns an unsigned 16-bit int calculated from two bytes of data at the specified index.- Parameters:
index
- position within the data buffer to read first byte- Returns:
- the 16 bit int value, between 0x0000 and 0xFFFF
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getInt16
public short getInt16(int index) throws java.io.IOException
Returns a signed 16-bit int calculated from two bytes of data at the specified index (MSB, LSB).- Parameters:
index
- position within the data buffer to read first byte- Returns:
- the 16 bit int value, between 0x0000 and 0xFFFF
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getInt24
public int getInt24(int index) throws java.io.IOException
Get a 24-bit unsigned integer from the buffer, returning it as an int.- Parameters:
index
- position within the data buffer to read first byte- Returns:
- the unsigned 24-bit int value as a long, between 0x00000000 and 0x00FFFFFF
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getUInt32
public long getUInt32(int index) throws java.io.IOException
Get a 32-bit unsigned integer from the buffer, returning it as a long.- Parameters:
index
- position within the data buffer to read first byte- Returns:
- the unsigned 32-bit int value as a long, between 0x00000000 and 0xFFFFFFFF
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getInt32
public int getInt32(int index) throws java.io.IOException
Returns a signed 32-bit integer from four bytes of data at the specified index the buffer.- Parameters:
index
- position within the data buffer to read first byte- Returns:
- the signed 32 bit int value, between 0x00000000 and 0xFFFFFFFF
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getInt64
public long getInt64(int index) throws java.io.IOException
Get a signed 64-bit integer from the buffer.- Parameters:
index
- position within the data buffer to read first byte- Returns:
- the 64 bit int value, between 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getS15Fixed16
public float getS15Fixed16(int index) throws java.io.IOException
Gets a s15.16 fixed point float from the buffer.This particular fixed point encoding has one sign bit, 15 numerator bits and 16 denominator bits.
- Returns:
- the floating point value
- Throws:
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negative
-
getFloat32
public float getFloat32(int index) throws java.io.IOException
- Throws:
java.io.IOException
-
getDouble64
public double getDouble64(int index) throws java.io.IOException
- Throws:
java.io.IOException
-
getStringValue
public MetadataExtractor.StringValue getStringValue(int index, int bytesRequested, java.nio.charset.Charset charset) throws java.io.IOException
- Throws:
java.io.IOException
-
getString
public java.lang.String getString(int index, int bytesRequested, java.nio.charset.Charset charset) throws java.io.IOException
- Throws:
java.io.IOException
-
getString
public java.lang.String getString(int index, int bytesRequested, java.lang.String charset) throws java.io.IOException
- Throws:
java.io.IOException
-
getNullTerminatedString
public java.lang.String getNullTerminatedString(int index, int maxLengthBytes, java.nio.charset.Charset charset) throws java.io.IOException
Creates a String from the _data buffer starting at the specified index, and ending wherebyte=='\0'
or wherelength==maxLength
.- Parameters:
index
- The index within the buffer at which to start reading the string.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.- Returns:
- The read string.
- Throws:
java.io.IOException
- The buffer does not contain enough bytes to satisfy this request.
-
getNullTerminatedStringValue
public MetadataExtractor.StringValue getNullTerminatedStringValue(int index, int maxLengthBytes, java.nio.charset.Charset charset) throws java.io.IOException
- Throws:
java.io.IOException
-
getNullTerminatedBytes
public byte[] getNullTerminatedBytes(int index, int maxLengthBytes) throws java.io.IOException
Returns the sequence of bytes punctuated by a\0
value.- Parameters:
index
- The index within the buffer at which to start reading the string.maxLengthBytes
- The maximum number of bytes to read. If a\0
byte is not reached within this limit, the returned array will bemaxLengthBytes
long.- Returns:
- The read byte array, excluding the null terminator.
- Throws:
java.io.IOException
- The buffer does not contain enough bytes to satisfy this request.
-
-