Class RandomAccessFileStreamImpl
- java.lang.Object
-
- de.bernd_michaely.common.io.random.RandomAccessFileStreamImpl
-
- All Implemented Interfaces:
RandomAccessData
,AutoCloseable
public class RandomAccessFileStreamImpl extends Object implements RandomAccessData
This class is a bufferedFileInputStream
based implementation of theRandomAccessData
interface.- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description RandomAccessFileStreamImpl(File file)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
long
getCurrentOffset()
Returns the current offset within this data.long
getLength()
Returns the length of the data in bytes.boolean
isClosed()
Returns true, if the data source was closed before.void
readBytes(byte[] buffer, int offset, int length)
Reads an array of bytes of the given length from this data source into the given buffer at the given offset.byte[]
readBytes(int length)
Reads an array of bytes of the given length from this data source.int
readUnsignedByte()
Reads an unsigned eight bit number from this data.void
setCurrentOffset(long offset)
Sets the current offset within this data.
-
-
-
Constructor Detail
-
RandomAccessFileStreamImpl
public RandomAccessFileStreamImpl(File file) throws FileNotFoundException
- Throws:
FileNotFoundException
-
-
Method Detail
-
close
public void close() throws RandomAccessDataException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceRandomAccessData
- Throws:
RandomAccessDataException
-
isClosed
public boolean isClosed()
Description copied from interface:RandomAccessData
Returns true, if the data source was closed before.- Specified by:
isClosed
in interfaceRandomAccessData
- Returns:
- true, if the data source was closed
-
getLength
public long getLength() throws RandomAccessDataException
Description copied from interface:RandomAccessData
Returns the length of the data in bytes.- Specified by:
getLength
in interfaceRandomAccessData
- Returns:
- the length of the data in bytes
- Throws:
RandomAccessDataException
- if RandomAccessData source is invalid
-
getCurrentOffset
public long getCurrentOffset() throws RandomAccessDataException
Description copied from interface:RandomAccessData
Returns the current offset within this data.- Specified by:
getCurrentOffset
in interfaceRandomAccessData
- Returns:
- offset in bytes
- Throws:
RandomAccessDataException
- if RandomAccessData source is invalid
-
setCurrentOffset
public void setCurrentOffset(long offset) throws RandomAccessDataException, IllegalArgumentException
Description copied from interface:RandomAccessData
Sets the current offset within this data. The offset may be set beyond the end of the file without throwing an exception. (Reading after that will do so.)- Specified by:
setCurrentOffset
in interfaceRandomAccessData
- Parameters:
offset
- offset in bytes- Throws:
RandomAccessDataException
- if an I/O error occursIllegalArgumentException
- if the offset given is less than zero
-
readUnsignedByte
public int readUnsignedByte() throws RandomAccessDataException
Description copied from interface:RandomAccessData
Reads an unsigned eight bit number from this data.- Specified by:
readUnsignedByte
in interfaceRandomAccessData
- Returns:
- the next byte of this file as an unsigned eight bit number
- Throws:
RandomAccessDataException
- if an I/O error occursRandomAccessEndOfDataException
- if the end of data is reached
-
readBytes
public byte[] readBytes(int length) throws RandomAccessDataException
Description copied from interface:RandomAccessData
Reads an array of bytes of the given length from this data source.- Specified by:
readBytes
in interfaceRandomAccessData
- Parameters:
length
- the number of bytes to read- Returns:
- the bytes read as byte array
- Throws:
RandomAccessDataException
- if an I/O error occursRandomAccessEndOfDataException
- if the end of data is reached
-
readBytes
public void readBytes(byte[] buffer, int offset, int length) throws RandomAccessDataException
Description copied from interface:RandomAccessData
Reads an array of bytes of the given length from this data source into the given buffer at the given offset. The caller is responsible to provide a buffer of sufficient size.- Specified by:
readBytes
in interfaceRandomAccessData
- Parameters:
buffer
- the buffer to write the read data tooffset
- starting index to write the data read into the bufferlength
- the number of bytes to read- Throws:
RandomAccessDataException
- if an I/O error occursRandomAccessEndOfDataException
- if the end of data is reached- See Also:
RandomAccessData.readBytes(int)
-
-