Package de.intarsys.tools.randomaccess
Interface IRandomAccess
-
- All Known Implementing Classes:
AbstractRandomAccess
,BufferedRandomAccess
,RandomAccessByteArray
,RandomAccessFile
,RandomAccessFilter
,RandomAccessViewport
public interface IRandomAccess
An interface for an object that can randomly access bytes in a data stream.This is an abstraction of
RandomAccessFile
to support other data storage objects (like byte arrays and so on).
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.io.InputStream
asInputStream()
AInputStream
view on the data structure.java.io.OutputStream
asOutputStream()
AOutputStream
view on the data structure.void
close()
Closes this random access data container and releases any system resources associated with the stream.void
flush()
Force changes to be made persistent.long
getLength()
Returns the length of this data container.long
getOffset()
Returns the current offset in this data container.boolean
isReadOnly()
true
if this is a read only data container.void
mark()
Mark the current offset into the data in a stack like manner.int
read()
Reads a byte of data from this data container.int
read(byte[] buffer)
Reads up tobuffer.length
bytes of data from this data container into an array of bytes.int
read(byte[] buffer, int start, int numBytes)
Reads up tolen
bytes of data from this data container into an array of bytes.void
reset()
Reset to the last position on the mark-stack.void
seek(long offset)
Sets the offset, measured from the beginning of the data container at which the next read or write occurs.void
seekBy(long delta)
Sets the offset, measured from the current offset at which the next read or write occurs.void
setLength(long newLength)
Assign the length.void
write(byte[] buffer)
Writesb.length
bytes from the specified byte array, starting at the current offset.void
write(byte[] buffer, int start, int numBytes)
Writeslen
bytes from the specified byte array starting atstart
.void
write(int b)
Writes the specified byte .
-
-
-
Method Detail
-
asInputStream
java.io.InputStream asInputStream()
AInputStream
view on the data structure.- Returns:
- A
InputStream
view on the data structure.
-
asOutputStream
java.io.OutputStream asOutputStream()
AOutputStream
view on the data structure.- Returns:
- A
OutputStream
view on the data structure.
-
close
void close() throws java.io.IOException
Closes this random access data container and releases any system resources associated with the stream. A closed random access data container cannot perform input or output operations and cannot be reopened.- Throws:
java.io.IOException
- if an I/O error occurs.
-
flush
void flush() throws java.io.IOException
Force changes to be made persistent.- Throws:
java.io.IOException
-
getLength
long getLength() throws java.io.IOException
Returns the length of this data container.- Returns:
- the length of this data container, measured in bytes.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
getOffset
long getOffset() throws java.io.IOException
Returns the current offset in this data container.- Returns:
- the offset from the beginning of the data container, in bytes, at which the next read or write occurs.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
isReadOnly
boolean isReadOnly()
true
if this is a read only data container.- Returns:
true
if this is a read only data container.
-
mark
void mark() throws java.io.IOException
Mark the current offset into the data in a stack like manner.- Throws:
java.io.IOException
-
read
int read() throws java.io.IOException
Reads a byte of data from this data container. The byte is returned as an integer in the range 0 to 255 (0x00-0x0ff
). This method blocks if no input is yet available.This method behaves in exactly the same way as the
InputStream.read()
method ofInputStream
.- Returns:
- the next byte of data, or
-1
if the end of the data container has been reached. - Throws:
java.io.IOException
- if an I/O error occurs. Not thrown if the end of the data container has been reached.
-
read
int read(byte[] buffer) throws java.io.IOException
Reads up tobuffer.length
bytes of data from this data container into an array of bytes. This method blocks until at least one byte of input is available.This method behaves in the exactly the same way as the
InputStream.read(byte[])
method ofInputStream
.- Parameters:
buffer
- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of this data container has been reached. - Throws:
java.io.IOException
- if an I/O error occurs.
-
read
int read(byte[] buffer, int start, int numBytes) throws java.io.IOException
Reads up tolen
bytes of data from this data container into an array of bytes. This method blocks until at least one byte of input is available.- Parameters:
buffer
- the buffer into which the data is read.start
- the start offset of the data.numBytes
- the maximum number of bytes read.- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of the file has been reached. - Throws:
java.io.IOException
- if an I/O error occurs.
-
reset
void reset() throws java.io.IOException
Reset to the last position on the mark-stack.- Throws:
java.io.IOException
-
seek
void seek(long offset) throws java.io.IOException
Sets the offset, measured from the beginning of the data container at which the next read or write occurs. The offset may be set beyond the end of the data container. Setting the offset beyond the end of the data container does not change the data container length. The length will change only by writing after the offset has been set beyond the end of the data container.- Parameters:
offset
- the offset position, measured in bytes from the beginning of the data container- Throws:
java.io.IOException
- ifoffset
is less than0
or if an I/O error occurs.
-
seekBy
void seekBy(long delta) throws java.io.IOException
Sets the offset, measured from the current offset at which the next read or write occurs. The offset may be set beyond the end of the data container. Setting the offset beyond the end of the data container does not change the data container length. The length will change only by writing after the offset has been set beyond the end of the data container.- Parameters:
delta
- the amount of bytes by which to change the current offset position- Throws:
java.io.IOException
- if the resultingoffset
is less than0
or if an I/O error occurs.
-
setLength
void setLength(long newLength) throws java.io.IOException
Assign the length. All bytes after length are truncated. If the real length is currently less than newLength, the data structure will be enlarged.- Parameters:
newLength
-- Throws:
java.io.IOException
-
write
void write(byte[] buffer) throws java.io.IOException
Writesb.length
bytes from the specified byte array, starting at the current offset.- Parameters:
buffer
- the data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
write
void write(byte[] buffer, int start, int numBytes) throws java.io.IOException
Writeslen
bytes from the specified byte array starting atstart
.- Parameters:
buffer
- the data.start
- the start offset in the data.numBytes
- the number of bytes to write.- Throws:
java.io.IOException
- if an I/O error occurs.
-
write
void write(int b) throws java.io.IOException
Writes the specified byte . The write starts at the current offset.- Parameters:
b
- thebyte
to be written.- Throws:
java.io.IOException
- if an I/O error occurs.
-
-