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

pack-priv final Class EmbedBlob

extends ConnectionChild
implements Blob, EngineLOB
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.jdbc.EngineLOB, java.sql.Blob
Imports
org.apache.derby.shared.common.reference.SQLState, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.jdbc.EngineLOB, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.types.DataValueDescriptor, .RawToBinaryFormatStream, .Resetable, org.apache.derby.iapi.services.io.InputStreamUtil, org.apache.derby.iapi.sql.conn.LanguageConnectionContext, org.apache.derby.iapi.util.InterruptStatus, java.sql.SQLException, .Blob, java.io.EOFException, .InputStream, .IOException

Implements java.sql.Blob (see the JDBC 2.0 spec). A blob sits on top of a BINARY, VARBINARY or LONG VARBINARY column. If its data is small (less than 1 page) it is a byte array taken from the SQLBit class. If it is large (more than 1 page) it is a long column in the database. The long column is accessed as a stream, and is implemented in store as an OverflowInputStream. The Resetable interface allows sending messages to that stream to initialize itself (reopen its container and lock the corresponding row) and to reset itself to the beginning.

Note

In the case that the data is large, it is represented as a stream. This stream is returned to the user in the getBinaryStream() method. This means that we have limited control over the state of the stream, since the user can read bytes from it at any time. Thus all methods here reset the stream to the beginning before doing any work.

Caveat

The methods may not behave correctly if a user sets up multiple threads and sucks data from the stream (returned from getBinaryStream()) at the same time as calling the Blob methods.

Supports

Field Summary

Modifier and TypeField and Description
private LOBStreamControl
private boolean
private int
locator

Locator value for this Blob, used as a handle by the client driver to map operations to the correct Blob on the server side.

private boolean
materialized

Tells whether the Blob has been materialized or not.

private PositionedStoreStream
myStream

The underlying positionable store stream, if any.

private long
streamLength

Length of the stream representing the Blob.

private final int
streamPositionOffset

Position offset for the stream representing the Blob, if any.

Inherited from org.apache.derby.impl.jdbc.ConnectionChild:
factorylocalConn

Constructor Summary

AccessConstructor and Description
pack-priv
EmbedBlob(byte[]
A byte array containing the data to be stores in the Blob.
blobBytes
,
EmbedConnection
The EmbedConnection object associated with this Blob object.
con
)

This constructor is used to create a empty Blob object.

protected

Method Summary

Modifier and TypeMethod and Description
private boolean

Returns:

true if a match is found, false if not.
checkMatch
(byte[]
the byte array to search for, passed in by the user
pattern
,
long
the position in the Blob content to start searching from
pos
)

Checks if the pattern (starting from the second byte) appears inside the Blob content.

private boolean

Returns:

true if a match is found, false if not.
checkMatch
(Blob
the Blob to search for, passed in by the user
pattern
,
long
the position in the Blob (this) content to start searching
pos
)

Checks if the pattern (starting from the second byte) appears inside the Blob content.

private void
protected void
finalize()

Overrides java.lang.Object.finalize.

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

public void
free()

Implements java.sql.Blob.free, org.apache.derby.iapi.jdbc.EngineLOB.free.

This method frees the Blob object and releases the resources that it holds.

public InputStream

Returns:

a stream containing the BLOB data
getBinaryStream
()

Implements java.sql.Blob.getBinaryStream.

Retrieves the BLOB designated by this Blob instance as a stream.

public InputStream

Returns:

through which the partial Blob value can be read.
getBinaryStream
(long
the offset to the first byte of the partial value to be retrieved. The first byte in the Blob is at position 1
pos
,
long
the length in bytes of the partial value to be retrieved
length
)

Implements java.sql.Blob.getBinaryStream.

Returns an InputStream object that contains a partial Blob value, starting with the byte specified by pos, which is length bytes in length.

public byte[]

Returns:

a byte array containing up to length consecutive bytes from the BLOB value designated by this Blob object, starting with the byte at position startPos.
getBytes
(long
the ordinal position of the first byte in the BLOB value to be extracted; the first byte is at position 1
startPos
,
int
is the number of consecutive bytes to be copied
length
)

Implements java.sql.Blob.getBytes.

Returns as an array of bytes part or all of the BLOB value that this Blob object designates.

public int

Returns:

The locator identifying this lob.
getLocator
()

Implements org.apache.derby.iapi.jdbc.EngineLOB.getLocator.

Return locator for this lob.

private SQLException
private int

Returns:

The offset into the stream where the user data begins (used if resetting the stream).
handleStreamValue
(InputStream
the source stream
dvdStream
,
EmbedConnection
the connection owning the Blob
con
)

Constructs a Blob object on top of a stream.

pack-priv boolean

Returns:

true if materialized else false
isMaterialized
()

Returns if blob data is stored locally (using LOBStreamControl).

public long

Returns:

length of the BLOB in bytes
length
()

Implements java.sql.Blob.length.

Returns the number of bytes in the BLOB value designated by this Blob object.

public long

Returns:

the position at which the pattern appears, else -1.
position
(byte[]
the byte array for which to search
pattern
,
long
the position at which to begin searching; the first position is 1
start
)

Implements java.sql.Blob.position.

Determines the byte position at which the specified byte pattern begins within the BLOB value that this Blob object represents.

public long

Returns:

the position at which the pattern begins, else -1
position
(Blob
the Blob object designating the BLOB value for which to search
pattern
,
long
the position in the BLOB value at which to begin searching; the first position is 1
start
)

Implements java.sql.Blob.position.

Determines the byte position in the BLOB value designated by this Blob object at which pattern begins.

private int

Returns:

the byte at the current position, or -1 if end of file has been reached
read
(long pos)

Reads one byte from the Blob at the specified position.

public OutputStream

Returns:

a java.io.OutputStream object to which data can be written
setBinaryStream
(long
- the position in the BLOB object at which to start writing
pos
)

Implements java.sql.Blob.setBinaryStream.

JDBC 3.0 Retrieves a stream that can be used to write to the BLOB value that this Blob object represents.

private long

Returns:

The new position, which will be equal to the requested position.
setBlobPosition
(long
requested Blob position, 0-based
logicalPos
)

Sets the position of the Blob to logicalPos, where position 0 is the beginning of the Blob content.

public int

Returns:

The number of bytes written to the BLOB.
setBytes
(long
the position in the BLOB object at which to start writing
pos
,
byte[]
the array of bytes to be written to the BLOB value that this Blob object represents
bytes
)

Implements java.sql.Blob.setBytes.

Writes the given array of bytes to the BLOB value that this Blob object represents, starting at position pos, and returns the number of bytes written.

public int

Returns:

The number of bytes written to the BLOB.
setBytes
(long
the position in the BLOB object at which to start writing
pos
,
byte[]
the array of bytes to be written to the BLOB value that this Blob object represents
bytes
,
int
the offset into the byte array at which to start reading the bytes to be written
offset
,
int
the number of bytes to be written to the BLOB value from the array of bytes bytes
len
)

Implements java.sql.Blob.setBytes.

Writes all or part of the given array of byte array to the BLOB value that this Blob object represents and returns the number of bytes written.

public void
truncate(long
- the length, in bytes, to which the BLOB value that this Blob object represents should be truncated
len
)

Implements java.sql.Blob.truncate.

JDBC 3.0 Truncates the BLOB value that this Blob object represents to be len bytes in length.

Inherited from org.apache.derby.impl.jdbc.ConnectionChild:
commitIfAutoCommitcommitIfNeededgetCalgetConnectionSynchronizationgetEmbedConnectiongetLanguageConnectionContextgetLCChandleExceptionneedCommitnewSQLExceptionrestoreContextStackrestoreIntrFlagIfSeensetupContextStack

Field Detail

controlback to summary
private LOBStreamControl control
isValidback to summary
private boolean isValid
locatorback to summary
private int locator

Locator value for this Blob, used as a handle by the client driver to map operations to the correct Blob on the server side.

See Also
getLocator()
materializedback to summary
private boolean materialized

Tells whether the Blob has been materialized or not.

Materialization happens when the Blob is updated by the user. A materialized Blob is represented either in memory or in a temporary file on disk, depending on size.

A Blob that has not been materialized is represented by a stream into the Derby store, and is read-only.

myStreamback to summary
private PositionedStoreStream myStream

The underlying positionable store stream, if any.

If materialized is true, the stream is null.

streamLengthback to summary
private long streamLength

Length of the stream representing the Blob.

Set to -1 when the stream has been materialized or the length of the stream is not currently known.

streamPositionOffsetback to summary
private final int streamPositionOffset

Position offset for the stream representing the Blob, if any.

This offset accounts for the bytes encoding the stream length at the head of the stream. Data byte pos is at pos + streamPositionOffset in the underlying stream. Set to Integer.MIN_VALUE if the Blob isn't represented by a store stream.

Constructor Detail

EmbedBlobback to summary
pack-priv EmbedBlob(byte[] blobBytes, EmbedConnection con) throws SQLException

This constructor is used to create a empty Blob object. It is used by the Connection interface method createBlob().

Parameters
blobBytes:byte[]

A byte array containing the data to be stores in the Blob.

con:EmbedConnection

The EmbedConnection object associated with this Blob object.

EmbedBlobback to summary
protected EmbedBlob(DataValueDescriptor dvd, EmbedConnection con) throws StandardException, SQLException

Method Detail

checkMatchback to summary
private boolean checkMatch(byte[] pattern, long pos) throws IOException, StandardException

Checks if the pattern (starting from the second byte) appears inside the Blob content.

At this point, the first byte of the pattern must already have been matched, and pos must be pointing at the second byte to compare.

Parameters
pattern:byte[]

the byte array to search for, passed in by the user

pos:long

the position in the Blob content to start searching from

Returns:boolean

true if a match is found, false if not.

checkMatchback to summary
private boolean checkMatch(Blob pattern, long pos) throws IOException, StandardException

Checks if the pattern (starting from the second byte) appears inside the Blob content.

Parameters
pattern:Blob

the Blob to search for, passed in by the user

pos:long

the position in the Blob (this) content to start searching

Returns:boolean

true if a match is found, false if not.

checkValidityback to summary
private void checkValidity() throws SQLException
finalizeback to summary
protected void finalize()

Overrides java.lang.Object.finalize.

Doc from java.lang.Object.finalize.

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

When running in a Java virtual machine in which finalization has been disabled or removed, the garbage collector will never call finalize(). In a Java virtual machine in which finalization is enabled, the garbage collector might call finalize only after an indefinite delay.

The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Annotations
@SuppressWarnings:deprecation, removal
freeback to summary
public void free() throws SQLException

Implements java.sql.Blob.free, org.apache.derby.iapi.jdbc.EngineLOB.free.

This method frees the Blob object and releases the resources that it holds. The object is invalid once the free method is called. If free is called multiple times, the subsequent calls to free are treated as a no-op.

Exceptions
SQLException:
if an error occurs releasing the Blob's resources
getBinaryStreamback to summary
public InputStream getBinaryStream() throws SQLException

Implements java.sql.Blob.getBinaryStream.

Retrieves the BLOB designated by this Blob instance as a stream.

Returns:InputStream

a stream containing the BLOB data

Exceptions
SQLException:
if there is an error accessing the BLOB
getBinaryStreamback to summary
public InputStream getBinaryStream(long pos, long length) throws SQLException

Implements java.sql.Blob.getBinaryStream.

Returns an InputStream object that contains a partial Blob value, starting with the byte specified by pos, which is length bytes in length.

Parameters
pos:long

the offset to the first byte of the partial value to be retrieved. The first byte in the Blob is at position 1

length:long

the length in bytes of the partial value to be retrieved

Returns:InputStream

through which the partial Blob value can be read.

Exceptions
SQLException:
if pos is less than 1 or if pos is greater than the number of bytes in the Blob or if pos + length is greater than Blob.length() +1
getBytesback to summary
public byte[] getBytes(long startPos, int length) throws SQLException

Implements java.sql.Blob.getBytes.

Returns as an array of bytes part or all of the BLOB value that this Blob object designates. The byte array contains up to length consecutive bytes starting at position startPos. The starting position must be between 1 and the length of the BLOB plus 1. This allows for zero-length BLOB values, from which only zero-length byte arrays can be returned. If a larger length is requested than there are bytes available, characters from the start position to the end of the BLOB are returned.

Parameters
startPos:long

the ordinal position of the first byte in the BLOB value to be extracted; the first byte is at position 1

length:int

is the number of consecutive bytes to be copied

Returns:byte[]

a byte array containing up to length consecutive bytes from the BLOB value designated by this Blob object, starting with the byte at position startPos.

Exceptions
SQLException:
if there is an error accessing the BLOB NOTE: If the starting position is the length of the BLOB plus 1, zero bytess are returned regardless of the length requested.
getLocatorback to summary
public int getLocator()

Implements org.apache.derby.iapi.jdbc.EngineLOB.getLocator.

Return locator for this lob.

Returns:int

The locator identifying this lob.

handleMyExceptionsback to summary
private SQLException handleMyExceptions(Throwable t) throws SQLException
handleStreamValueback to summary
private int handleStreamValue(InputStream dvdStream, EmbedConnection con) throws StandardException, SQLException

Constructs a Blob object on top of a stream.

Parameters
dvdStream:InputStream

the source stream

con:EmbedConnection

the connection owning the Blob

Returns:int

The offset into the stream where the user data begins (used if resetting the stream).

Exceptions
StandardException:
if accessing the stream fails, or if writing data to temporary storage fails
isMaterializedback to summary
pack-priv boolean isMaterialized()

Returns if blob data is stored locally (using LOBStreamControl).

Returns:boolean

true if materialized else false

lengthback to summary
public long length() throws SQLException

Implements java.sql.Blob.length.

Returns the number of bytes in the BLOB value designated by this Blob object.

Returns:long

length of the BLOB in bytes

Exceptions
SQLException:
if there is an error accessing the length of the BLOB
positionback to summary
public long position(byte[] pattern, long start) throws SQLException

Implements java.sql.Blob.position.

Determines the byte position at which the specified byte pattern begins within the BLOB value that this Blob object represents. The search for pattern. begins at position start

Parameters
pattern:byte[]

the byte array for which to search

start:long

the position at which to begin searching; the first position is 1

Returns:long

the position at which the pattern appears, else -1.

Exceptions
SQLException:
if there is an error accessing the BLOB
positionback to summary
public long position(Blob pattern, long start) throws SQLException

Implements java.sql.Blob.position.

Determines the byte position in the BLOB value designated by this Blob object at which pattern begins. The search begins at position start.

Parameters
pattern:Blob

the Blob object designating the BLOB value for which to search

start:long

the position in the BLOB value at which to begin searching; the first position is 1

Returns:long

the position at which the pattern begins, else -1

Exceptions
SQLException:
if there is an error accessing the BLOB
readback to summary
private int read(long pos) throws IOException, StandardException

Reads one byte from the Blob at the specified position.

Depending on the representation, this might result in a read from a byte array, a temporary file on disk or from a Derby store stream.

Returns:int

the byte at the current position, or -1 if end of file has been reached

Exceptions
IOException:
if reading from the underlying data representation fails
setBinaryStreamback to summary
public OutputStream setBinaryStream(long pos) throws SQLException

Implements java.sql.Blob.setBinaryStream.

JDBC 3.0 Retrieves a stream that can be used to write to the BLOB value that this Blob object represents. The stream begins at position pos.

Parameters
pos:long

- the position in the BLOB object at which to start writing

Returns:OutputStream

a java.io.OutputStream object to which data can be written

Exceptions
SQLException:
Feature not implemented for now.
setBlobPositionback to summary
private long setBlobPosition(long logicalPos) throws StandardException, IOException

Sets the position of the Blob to logicalPos, where position 0 is the beginning of the Blob content.

The position is only guaranteed to be valid from the time this method is invoked until the synchronization monitor is released, or until the next invokation of this method.

The position is logical in the sense that it specifies the requested position in the Blob content. This position might be at a different position in the underlying representation, for instance the Derby store stream prepends the Blob content with a length field.

Parameters
logicalPos:long

requested Blob position, 0-based

Returns:long

The new position, which will be equal to the requested position.

Exceptions
StandardException:
throws BLOB_POSITION_TOO_LARGE if the requested position is larger than the Blob length, throws other SQL states if resetting the stream fails
IOException:
if reading/accessing the Blob fails
setBytesback to summary
public int setBytes(long pos, byte[] bytes) throws SQLException

Implements java.sql.Blob.setBytes.

Writes the given array of bytes to the BLOB value that this Blob object represents, starting at position pos, and returns the number of bytes written.

Parameters
pos:long

the position in the BLOB object at which to start writing

bytes:byte[]

the array of bytes to be written to the BLOB value that this Blob object represents

Returns:int

The number of bytes written to the BLOB.

Exceptions
SQLException:
if writing the bytes to the BLOB fails
Since
1.4
setBytesback to summary
public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException

Implements java.sql.Blob.setBytes.

Writes all or part of the given array of byte array to the BLOB value that this Blob object represents and returns the number of bytes written. Writing starts at position pos in the BLOB value; len bytes from the given byte array are written.

Parameters
pos:long

the position in the BLOB object at which to start writing

bytes:byte[]

the array of bytes to be written to the BLOB value that this Blob object represents

offset:int

the offset into the byte array at which to start reading the bytes to be written

len:int

the number of bytes to be written to the BLOB value from the array of bytes bytes

Returns:int

The number of bytes written to the BLOB.

Exceptions
SQLException:
if writing the bytes to the BLOB fails
IndexOutOfBoundsException:
if len is larger than bytes.length - offset
Since
1.4
truncateback to summary
public void truncate(long len) throws SQLException

Implements java.sql.Blob.truncate.

JDBC 3.0 Truncates the BLOB value that this Blob object represents to be len bytes in length.

Parameters
len:long

- the length, in bytes, to which the BLOB value that this Blob object represents should be truncated

Exceptions
SQLException:
Feature not implemented for now.