Top Description Fields Constructors Methods
org.apache.derby.impl.jdbc

public Class PositionedStoreStream

extends InputStream
implements PositionedStream, Resetable
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.types.Resetable, org.apache.derby.iapi.types.PositionedStream
Imports
java.io.EOFException, .IOException, .InputStream, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.services.io.InputStreamUtil, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.types.PositionedStream, .Resetable

A wrapper-stream able to reposition the underlying store stream.

Where a user expects the underlying stream to be at a given position, reposition must be called with the expected position first. A use case for this scenario is the LOB objects, where you can request a stream and at the same time (this does not mean concurrently) query the LOB about its length or ask to get a part of the LOB returned. Such multiplexed operations must result in consistent and valid data, and to achieve this the underlying store stream must be able to reposition itself. Synchronization: Access to instances of this class must be externally synchronized on the connection synchronization object. There are two reasons for this:

See Also
EmbedConnection#getConnectionSynchronization

Field Summary

Modifier and TypeField and Description
private long
pos

Position of the underlying store stream.

private final InputStream
stream

Underlying store stream serving bytes.

Constructor Summary

AccessConstructor and Description
public
PositionedStoreStream(InputStream
a Resetable-stream
in
)

Creates a positioned store stream on top of the specified resettable stream.

Method Summary

Modifier and TypeMethod and Description
public InputStream
asInputStream()

Implements org.apache.derby.iapi.types.PositionedStream.asInputStream.

Returns a reference to self as an InputStream.

public void
closeStream()

Implements org.apache.derby.iapi.types.Resetable.closeStream.

Closes the resettable stream.

public long

Returns:

Current byte position of the store stream.
getPosition
()

Implements org.apache.derby.iapi.types.PositionedStream.getPosition.

Returns the current position of the underlying store stream.

public void
initStream()

Implements org.apache.derby.iapi.types.Resetable.initStream.

Initialize the resettable stream for use.

public int

Returns:

The actual number of bytes read, or -1 if the end of the stream is reached.
read
(byte[]
the buffer into which the data is read.
b
)

Overrides java.io.InputStream.read.

Reads a number of bytes from the underlying stream and stores them in the specified byte array.

public int

Returns:

The actual number of bytes read, or -1 if the end of the stream is reached.
read
(byte[]
the buffer into which the data is read.
b
,
int
the start offset in array b at which the data is written.
off
,
int
the maximum number of bytes to read.
len
)

Overrides java.io.InputStream.read.

Reads a number of bytes from the underlying stream and stores them in the specified byte array at the specified offset.

public int

Returns:

The next byte of data, or -1 if the end of the stream is reached.
read
()

Implements abstract java.io.InputStream.read.

Reads a single byte from the underlying stream.

public void
reposition(final long
requested byte position, first position is 0
requestedPos
)

Implements org.apache.derby.iapi.types.PositionedStream.reposition.

Repositions the underlying store stream to the requested position.

public void
resetStream()

Implements org.apache.derby.iapi.types.Resetable.resetStream.

Resets the resettable stream.

public long

Returns:

The actual number of bytes skipped.
skip
(long
the number of bytes to be skipped.
toSkip
)

Overrides java.io.InputStream.skip.

Skips up to the specified number of bytes from the underlying stream.

Inherited from java.io.InputStream:
availableclosemarkmarkSupportednullInputStreamreadAllBytesreadNBytesreadNBytesresetskipNBytestransferTo

Field Detail

posback to summary
private long pos

Position of the underlying store stream. Note that the position is maintained by this class, not the underlying store stream itself. Future improvement: Add this functionality to the underlying store stream itself to avoid another level in the stream stack.

streamback to summary
private final InputStream stream

Underlying store stream serving bytes.

Constructor Detail

PositionedStoreStreamback to summary
public PositionedStoreStream(InputStream in) throws IOException, StandardException

Creates a positioned store stream on top of the specified resettable stream.

Upon creation, the underlying stream is initiated and reset to make sure the states of the streams are in sync with each other.

Parameters
in:InputStream

a Resetable-stream

Method Detail

asInputStreamback to summary
public InputStream asInputStream()

Implements org.apache.derby.iapi.types.PositionedStream.asInputStream.

Doc from org.apache.derby.iapi.types.PositionedStream.asInputStream.

Returns a reference to self as an InputStream.

This method is not allowed to return null.

Returns:InputStream

An InputStream reference to self.

closeStreamback to summary
public void closeStream()

Implements org.apache.derby.iapi.types.Resetable.closeStream.

Closes the resettable stream.

See Also
Resetable#closeStream
getPositionback to summary
public long getPosition()

Implements org.apache.derby.iapi.types.PositionedStream.getPosition.

Returns the current position of the underlying store stream.

Returns:long

Current byte position of the store stream.

initStreamback to summary
public void initStream() throws StandardException

Implements org.apache.derby.iapi.types.Resetable.initStream.

Initialize the resettable stream for use.

Exceptions
StandardException:
if initializing the store in stream fails
See Also
Resetable#initStream
readback to summary
public int read(byte[] b) throws IOException

Overrides java.io.InputStream.read.

Reads a number of bytes from the underlying stream and stores them in the specified byte array.

Parameters
b:byte[]

Doc from java.io.InputStream.read.

the buffer into which the data is read.

Returns:int

The actual number of bytes read, or -1 if the end of the stream is reached.

Exceptions
IOException:
if an I/O error occurs
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Reads a number of bytes from the underlying stream and stores them in the specified byte array at the specified offset.

Parameters
b:byte[]

Doc from java.io.InputStream.read.

the buffer into which the data is read.

off:int

Doc from java.io.InputStream.read.

the start offset in array b at which the data is written.

len:int

Doc from java.io.InputStream.read.

the maximum number of bytes to read.

Returns:int

The actual number of bytes read, or -1 if the end of the stream is reached.

Exceptions
IOException:
if an I/O error occurs
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Reads a single byte from the underlying stream.

Returns:int

The next byte of data, or -1 if the end of the stream is reached.

Exceptions
IOException:
if an I/O error occurs
repositionback to summary
public void reposition(final long requestedPos) throws IOException, StandardException

Implements org.apache.derby.iapi.types.PositionedStream.reposition.

Repositions the underlying store stream to the requested position.

Repositioning is required because there can be several uses of the store stream, which changes the position of it. If a class is dependent on the underlying stream not changing its position, it must call reposition with the position it expects before using the stream again.

If the repositioning fails because the stream is exhausted, most likely because of an invalid position specified by the user, the stream is reset to position zero and the EOFException is rethrown.

Parameters
requestedPos:long

Doc from org.apache.derby.iapi.types.PositionedStream.reposition.

requested byte position, first position is 0

Exceptions
IOException:
if reading from the store stream fails
StandardException:
if resetting the store in stream fails, or some other exception happens in store
EOFException:
if the stream is exhausted before the requested position is reached
See Also
getPosition
resetStreamback to summary
public void resetStream() throws IOException, StandardException

Implements org.apache.derby.iapi.types.Resetable.resetStream.

Resets the resettable stream.

Exceptions
IOException:

Doc from org.apache.derby.iapi.types.Resetable.resetStream.

on error

StandardException:
if resetting the stream in store fails
See Also
Resetable#resetStream
skipback to summary
public long skip(long toSkip) throws IOException

Overrides java.io.InputStream.skip.

Skips up to the specified number of bytes from the underlying stream.

Parameters
toSkip:long

Doc from java.io.InputStream.skip.

the number of bytes to be skipped.

Returns:long

The actual number of bytes skipped.

Exceptions
IOException:
if an I/O error occurs