Package gov.nih.mipav.model.file
Class MetadataExtractor.RandomAccessFileReader
- java.lang.Object
-
- gov.nih.mipav.model.file.MetadataExtractor.RandomAccessReader
-
- gov.nih.mipav.model.file.MetadataExtractor.RandomAccessFileReader
-
- Enclosing class:
- MetadataExtractor
public class MetadataExtractor.RandomAccessFileReader extends MetadataExtractor.RandomAccessReader
Provides methods to read specific values from aRandomAccessFile
, with a consistent, checked exception structure for issues.- Author:
- Drew Noakes https://drewnoakes.com
-
-
Field Summary
Fields Modifier and Type Field Description private int
_baseOffset
private int
_currentIndex
private java.io.RandomAccessFile
_file
private long
_length
-
Constructor Summary
Constructors Constructor Description RandomAccessFileReader(java.io.RandomAccessFile file)
RandomAccessFileReader(java.io.RandomAccessFile file, int baseOffset)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte
getByte(int index)
Gets the byte value at the specified byteindex
.byte[]
getBytes(int index, int count)
Returns the required number of bytes from the specified index from the underlying source.long
getLength()
Returns the length of the data source in bytes.protected boolean
isValidIndex(int index, int bytesRequested)
private void
seek(int index)
int
toUnshiftedOffset(int localOffset)
protected void
validateIndex(int index, int bytesRequested)
Ensures that the buffered bytes extend to cover the specified index.-
Methods inherited from class gov.nih.mipav.model.file.MetadataExtractor.RandomAccessReader
getBit, getDouble64, getFloat32, getInt16, getInt24, getInt32, getInt64, getInt8, getNullTerminatedBytes, getNullTerminatedString, getNullTerminatedStringValue, getS15Fixed16, getString, getString, getStringValue, getUInt16, getUInt32, getUInt8, isMotorolaByteOrder, setMotorolaByteOrder
-
-
-
-
Constructor Detail
-
RandomAccessFileReader
public RandomAccessFileReader(java.io.RandomAccessFile file) throws java.io.IOException
- Throws:
java.io.IOException
-
RandomAccessFileReader
public RandomAccessFileReader(java.io.RandomAccessFile file, int baseOffset) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
toUnshiftedOffset
public int toUnshiftedOffset(int localOffset)
- Specified by:
toUnshiftedOffset
in classMetadataExtractor.RandomAccessReader
-
getLength
public long getLength()
Description copied from class:MetadataExtractor.RandomAccessReader
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.- Specified by:
getLength
in classMetadataExtractor.RandomAccessReader
- Returns:
- the length of the data source, in bytes.
-
getByte
public byte getByte(int index) throws java.io.IOException
Description copied from class:MetadataExtractor.RandomAccessReader
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
.- Specified by:
getByte
in classMetadataExtractor.RandomAccessReader
- Parameters:
index
- The index from which to read the byte- Returns:
- The read byte value
- Throws:
MetadataExtractor.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 byte[] getBytes(int index, int count) throws java.io.IOException
Description copied from class:MetadataExtractor.RandomAccessReader
Returns the required number of bytes from the specified index from the underlying source.- Specified by:
getBytes
in classMetadataExtractor.RandomAccessReader
- 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:
MetadataExtractor.BufferBoundsException
- if the requested bytes extend beyond the end of the underlying data sourcejava.io.IOException
- if the byte is unable to be read
-
seek
private void seek(int index) throws java.io.IOException
- Throws:
java.io.IOException
-
isValidIndex
protected boolean isValidIndex(int index, int bytesRequested) throws java.io.IOException
- Specified by:
isValidIndex
in classMetadataExtractor.RandomAccessReader
- Throws:
java.io.IOException
-
validateIndex
protected void validateIndex(int index, int bytesRequested) throws java.io.IOException
Description copied from class:MetadataExtractor.RandomAccessReader
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.- Specified by:
validateIndex
in classMetadataExtractor.RandomAccessReader
- 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
-
-