Class EmbedBlob
Supports
- JSR169 - no subsetting for java.sql.Blob
- JDBC 2.0
- JDBC 3.0 - no new dependencies on new JDBC 3.0 or JDK 1.4 classes, new update methods can safely be added into implementation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate LOBStreamControlprivate booleanprivate intLocator value for this Blob, used as a handle by the client driver to map operations to the correct Blob on the server side.private booleanTells whether the Blob has been materialized or not.private PositionedStoreStreamThe underlying positionable store stream, if any.private longLength of the stream representing the Blob.private final intPosition offset for the stream representing the Blob, if any.Fields inherited from class ConnectionChild
factory, localConn -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)EmbedBlob(byte[] blobBytes, EmbedConnection con) This constructor is used to create a empty Blob object.protectedEmbedBlob(DataValueDescriptor dvd, EmbedConnection con) -
Method Summary
Modifier and TypeMethodDescriptionprivate booleancheckMatch(byte[] pattern, long pos) Checks if the pattern (starting from the second byte) appears inside the Blob content.private booleancheckMatch(Blob pattern, long pos) Checks if the pattern (starting from the second byte) appears inside the Blob content.private voidprotected voidfinalize()voidfree()This method frees theBlobobject and releases the resources that it holds.Retrieves theBLOBdesignated by thisBlobinstance as a stream.getBinaryStream(long pos, long length) Returns anInputStreamobject that contains a partialBlobvalue, starting with the byte specified by pos, which is length bytes in length.byte[]getBytes(long startPos, int length) Returns as an array of bytes part or all of theBLOBvalue that thisBlobobject designates.intReturn locator for this lob.private SQLExceptionprivate inthandleStreamValue(InputStream dvdStream, EmbedConnection con) Constructs a Blob object on top of a stream.(package private) booleanReturns if blob data is stored locally (using LOBStreamControl).longlength()Returns the number of bytes in theBLOBvalue designated by thisBlobobject.longposition(byte[] pattern, long start) Determines the byte position at which the specified bytepatternbegins within theBLOBvalue that thisBlobobject represents.longDetermines the byte position in theBLOBvalue designated by thisBlobobject at whichpatternbegins.private intread(long pos) Reads one byte from the Blob at the specified position.setBinaryStream(long pos) JDBC 3.0 Retrieves a stream that can be used to write to the BLOB value that this Blob object represents.private longsetBlobPosition(long logicalPos) Sets the position of the Blob tologicalPos, where position 0 is the beginning of the Blob content.intsetBytes(long pos, byte[] bytes) 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.intsetBytes(long pos, byte[] bytes, int offset, int len) 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.voidtruncate(long len) JDBC 3.0 Truncates the BLOB value that this Blob object represents to be len bytes in length.
-
Field Details
-
materialized
private boolean materializedTells 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.
-
myStream
The underlying positionable store stream, if any.If
materializedistrue, the stream isnull. -
locator
private int locatorLocator 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:
-
streamLength
private long streamLengthLength of the stream representing the Blob.Set to -1 when the stream has been
materializedor the length of the stream is not currently known. -
streamPositionOffset
private final int streamPositionOffsetPosition 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
posis atpos + streamPositionOffsetin the underlying stream. Set toInteger.MIN_VALUEif the Blob isn't represented by a store stream. -
isValid
private boolean isValid -
control
-
-
Constructor Details
-
EmbedBlob
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- A byte array containing the data to be stores in the Blob.con- The EmbedConnection object associated with this Blob object.- Throws:
SQLException
-
EmbedBlob
protected EmbedBlob(DataValueDescriptor dvd, EmbedConnection con) throws StandardException, SQLException - Throws:
StandardExceptionSQLException
-
-
Method Details
-
handleStreamValue
private int handleStreamValue(InputStream dvdStream, EmbedConnection con) throws StandardException, SQLException Constructs a Blob object on top of a stream.- Parameters:
dvdStream- the source streamcon- the connection owning the Blob- Returns:
- The offset into the stream where the user data begins (used if resetting the stream).
- Throws:
StandardException- if accessing the stream fails, or if writing data to temporary storage failsSQLException
-
setBlobPosition
Sets the position of the Blob tologicalPos, 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- requested Blob position, 0-based- Returns:
- The new position, which will be equal to the requested position.
- Throws:
IOException- if reading/accessing the Blob failsStandardException- throws BLOB_POSITION_TOO_LARGE if the requested position is larger than the Blob length, throws other SQL states if resetting the stream fails
-
read
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:
- the byte at the current position, or -1 if end of file has been reached
- Throws:
IOException- if reading from the underlying data representation failsStandardException
-
length
Returns the number of bytes in theBLOBvalue designated by thisBlobobject.- Specified by:
lengthin interfaceBlob- Returns:
- length of the
BLOBin bytes - Throws:
SQLException- if there is an error accessing the length of theBLOB
-
getBytes
Returns as an array of bytes part or all of theBLOBvalue that thisBlobobject designates. The byte array contains up tolengthconsecutive bytes starting at positionstartPos. 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.- Specified by:
getBytesin interfaceBlob- Parameters:
startPos- the ordinal position of the first byte in theBLOBvalue to be extracted; the first byte is at position 1length- is the number of consecutive bytes to be copied- Returns:
- a byte array containing up to
lengthconsecutive bytes from theBLOBvalue designated by thisBlobobject, starting with the byte at positionstartPos. - Throws:
SQLException- if there is an error accessing theBLOBNOTE: If the starting position is the length of the BLOB plus 1, zero bytess are returned regardless of the length requested.
-
getBinaryStream
Retrieves theBLOBdesignated by thisBlobinstance as a stream.- Specified by:
getBinaryStreamin interfaceBlob- Returns:
- a stream containing the
BLOBdata - Throws:
SQLException- if there is an error accessing theBLOB
-
position
Determines the byte position at which the specified bytepatternbegins within theBLOBvalue that thisBlobobject represents. The search forpattern. begins at positionstart- Specified by:
positionin interfaceBlob- Parameters:
pattern- the byte array for which to searchstart- the position at which to begin searching; the first position is 1- Returns:
- the position at which the pattern appears, else -1.
- Throws:
SQLException- if there is an error accessing theBLOB
-
checkMatch
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
posmust be pointing at the second byte to compare.- Parameters:
pattern- the byte array to search for, passed in by the userpos- the position in the Blob content to start searching from- Returns:
trueif a match is found,falseif not.- Throws:
IOExceptionStandardException
-
position
Determines the byte position in theBLOBvalue designated by thisBlobobject at whichpatternbegins. The search begins at positionstart.- Specified by:
positionin interfaceBlob- Parameters:
pattern- theBlobobject designating theBLOBvalue for which to searchstart- the position in theBLOBvalue at which to begin searching; the first position is 1- Returns:
- the position at which the pattern begins, else -1
- Throws:
SQLException- if there is an error accessing theBLOB
-
checkMatch
Checks if the pattern (starting from the second byte) appears inside the Blob content.- Parameters:
pattern- the Blob to search for, passed in by the userpos- the position in the Blob (this) content to start searching- Returns:
trueif a match is found,falseif not.- Throws:
IOExceptionStandardException
-
handleMyExceptions
- Throws:
SQLException
-
finalize
-
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.- Specified by:
setBytesin interfaceBlob- Parameters:
pos- the position in the BLOB object at which to start writingbytes- the array of bytes to be written to the BLOB value that this Blob object represents- Returns:
- The number of bytes written to the BLOB.
- Throws:
SQLException- if writing the bytes to the BLOB fails- Since:
- 1.4
-
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.- Specified by:
setBytesin interfaceBlob- Parameters:
pos- the position in the BLOB object at which to start writingbytes- the array of bytes to be written to the BLOB value that this Blob object representsoffset- the offset into the byte array at which to start reading the bytes to be writtenlen- the number of bytes to be written to the BLOB value from the array of bytes bytes- Returns:
- The number of bytes written to the BLOB.
- Throws:
SQLException- if writing the bytes to the BLOB failsIndexOutOfBoundsException- iflenis larger thanbytes.length - offset- Since:
- 1.4
-
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.- Specified by:
setBinaryStreamin interfaceBlob- Parameters:
pos- - the position in the BLOB object at which to start writing- Returns:
- a java.io.OutputStream object to which data can be written
- Throws:
SQLException- Feature not implemented for now.
-
truncate
JDBC 3.0 Truncates the BLOB value that this Blob object represents to be len bytes in length.- Specified by:
truncatein interfaceBlob- Parameters:
len- - the length, in bytes, to which the BLOB value that this Blob object represents should be truncated- Throws:
SQLException- Feature not implemented for now.
-
free
This method frees theBlobobject and releases the resources that it holds. The object is invalid once thefreemethod is called. Iffreeis called multiple times, the subsequent calls tofreeare treated as a no-op.- Specified by:
freein interfaceBlob- Specified by:
freein interfaceEngineLOB- Throws:
SQLException- if an error occurs releasing the Blob's resources
-
getBinaryStream
Returns anInputStreamobject that contains a partialBlobvalue, starting with the byte specified by pos, which is length bytes in length.- Specified by:
getBinaryStreamin interfaceBlob- Parameters:
pos- the offset to the first byte of the partial value to be retrieved. The first byte in theBlobis at position 1length- the length in bytes of the partial value to be retrieved- Returns:
- through which the partial
Blobvalue can be read. - Throws:
SQLException- if pos is less than 1 or if pos is greater than the number of bytes in theBlobor ifpos + lengthis greater thanBlob.length() +1
-
checkValidity
- Throws:
SQLException
-
isMaterialized
boolean isMaterialized()Returns if blob data is stored locally (using LOBStreamControl).- Returns:
- true if materialized else false
-
getLocator
public int getLocator()Return locator for this lob.- Specified by:
getLocatorin interfaceEngineLOB- Returns:
- The locator identifying this lob.
-