Package de.intarsys.tools.randomaccess
Class RandomAccessViewport
- java.lang.Object
-
- de.intarsys.tools.randomaccess.AbstractRandomAccess
-
- de.intarsys.tools.randomaccess.RandomAccessFilter
-
- de.intarsys.tools.randomaccess.RandomAccessViewport
-
- All Implemented Interfaces:
IRandomAccess
public class RandomAccessViewport extends RandomAccessFilter
A viewport in an existingIRandomAccess
.
-
-
Field Summary
-
Fields inherited from class de.intarsys.tools.randomaccess.RandomAccessFilter
random
-
-
Constructor Summary
Constructors Constructor Description RandomAccessViewport(IRandomAccess ra, long offset, long length)
Create a viewport to the existingIRandomAccess
ra
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
getLength()
Returns the length of this data container.long
getOffset()
Returns the current offset in this data container.protected long
getViewEnd()
protected long
getViewLength()
protected long
getViewOffset()
boolean
isReadOnly()
true
if this is a read only data container.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
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 .-
Methods inherited from class de.intarsys.tools.randomaccess.RandomAccessFilter
close, flush, getRandom
-
Methods inherited from class de.intarsys.tools.randomaccess.AbstractRandomAccess
asInputStream, asOutputStream, getPositionStack, mark, reset
-
-
-
-
Constructor Detail
-
RandomAccessViewport
public RandomAccessViewport(IRandomAccess ra, long offset, long length) throws java.io.IOException
Create a viewport to the existingIRandomAccess
ra
.ATTENTION: This will not seek to
offset
inra
. This means after creation before initial use you have to position theIRandomAccess
yourself usingseek
.- Parameters:
ra
-offset
-length
-- Throws:
java.io.IOException
-
-
Method Detail
-
getLength
public long getLength() throws java.io.IOException
Description copied from interface:IRandomAccess
Returns the length of this data container.- Specified by:
getLength
in interfaceIRandomAccess
- Overrides:
getLength
in classRandomAccessFilter
- Returns:
- the length of this data container, measured in bytes.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
getOffset
public long getOffset() throws java.io.IOException
Description copied from interface:IRandomAccess
Returns the current offset in this data container.- Specified by:
getOffset
in interfaceIRandomAccess
- Overrides:
getOffset
in classRandomAccessFilter
- 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.
-
getViewEnd
protected long getViewEnd()
-
getViewLength
protected long getViewLength()
-
getViewOffset
protected long getViewOffset()
-
isReadOnly
public boolean isReadOnly()
Description copied from interface:IRandomAccess
true
if this is a read only data container.- Specified by:
isReadOnly
in interfaceIRandomAccess
- Overrides:
isReadOnly
in classRandomAccessFilter
- Returns:
true
if this is a read only data container.
-
read
public int read() throws java.io.IOException
Description copied from interface:IRandomAccess
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
.- Specified by:
read
in interfaceIRandomAccess
- Overrides:
read
in classRandomAccessFilter
- 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
public int read(byte[] buffer) throws java.io.IOException
Description copied from interface:IRandomAccess
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
.- Specified by:
read
in interfaceIRandomAccess
- Overrides:
read
in classRandomAccessFilter
- 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
public int read(byte[] buffer, int start, int numBytes) throws java.io.IOException
Description copied from interface:IRandomAccess
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.- Specified by:
read
in interfaceIRandomAccess
- Overrides:
read
in classRandomAccessFilter
- 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.
-
seek
public void seek(long offset) throws java.io.IOException
Description copied from interface:IRandomAccess
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.- Specified by:
seek
in interfaceIRandomAccess
- Overrides:
seek
in classRandomAccessFilter
- 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
public void seekBy(long delta) throws java.io.IOException
Description copied from interface:IRandomAccess
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.- Specified by:
seekBy
in interfaceIRandomAccess
- Overrides:
seekBy
in classRandomAccessFilter
- 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
public void setLength(long newLength) throws java.io.IOException
Description copied from interface:IRandomAccess
Assign the length. All bytes after length are truncated. If the real length is currently less than newLength, the data structure will be enlarged.- Specified by:
setLength
in interfaceIRandomAccess
- Overrides:
setLength
in classRandomAccessFilter
- Throws:
java.io.IOException
-
write
public void write(byte[] buffer) throws java.io.IOException
Description copied from interface:IRandomAccess
Writesb.length
bytes from the specified byte array, starting at the current offset.- Specified by:
write
in interfaceIRandomAccess
- Overrides:
write
in classRandomAccessFilter
- Parameters:
buffer
- the data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
write
public void write(byte[] buffer, int start, int numBytes) throws java.io.IOException
Description copied from interface:IRandomAccess
Writeslen
bytes from the specified byte array starting atstart
.- Specified by:
write
in interfaceIRandomAccess
- Overrides:
write
in classRandomAccessFilter
- 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
public void write(int b) throws java.io.IOException
Description copied from interface:IRandomAccess
Writes the specified byte . The write starts at the current offset.- Specified by:
write
in interfaceIRandomAccess
- Overrides:
write
in classRandomAccessFilter
- Parameters:
b
- thebyte
to be written.- Throws:
java.io.IOException
- if an I/O error occurs.
-
-