- All Implemented Interfaces:
Serializable,Cloneable,Blob
public class SerialBlob extends Object implements Blob, Serializable, Cloneable
BLOB value.
The SerialBlob class provides a constructor for creating
an instance from a Blob object. Note that the
Blob
object should have brought the SQL BLOB value's data over
to the client before a SerialBlob object
is constructed from it. The data of an SQL BLOB value can
be materialized on the client as an array of bytes (using the method
Blob.getBytes) or as a stream of uninterpreted bytes
(using the method Blob.getBinaryStream).
SerialBlob methods make it possible to make a copy of a
SerialBlob object as an array of bytes or as a stream.
They also make it possible to locate a given pattern of bytes or a
Blob object within a SerialBlob object
and to update or truncate a Blob object.
Thread safety
A SerialBlob is not safe for use by multiple concurrent threads. If a SerialBlob is to be used by more than one thread then access to the SerialBlob should be controlled by appropriate synchronization.
- Since:
- 1.5
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description SerialBlob(byte[] b)Constructs aSerialBlobobject that is a serialized version of the givenbytearray.SerialBlob(Blob blob)Constructs aSerialBlobobject that is a serialized version of the givenBlobobject. -
Method Summary
Modifier and Type Method Description Objectclone()Returns a clone of thisSerialBlob.booleanequals(Object obj)Compares this SerialBlob to the specified object.voidfree()This method frees theSerialBlobobject and releases the resources that it holds.InputStreamgetBinaryStream()Returns thisSerialBlobobject as an input stream.InputStreamgetBinaryStream(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 pos, int length)Copies the specified number of bytes, starting at the given position, from thisSerialBlobobject to another array of bytes.inthashCode()Returns a hash code for thisSerialBlob.longlength()Retrieves the number of bytes in thisSerialBlobobject's array of bytes.longposition(byte[] pattern, long start)Returns the position in thisSerialBlobobject where the given pattern of bytes begins, starting the search at the specified position.longposition(Blob pattern, long start)Returns the position in thisSerialBlobobject where the givenBlobobject begins, starting the search at the specified position.OutputStreamsetBinaryStream(long pos)Retrieves a stream that can be used to write to theBLOBvalue that thisBlobobject represents.intsetBytes(long pos, byte[] bytes)Writes the given array of bytes to theBLOBvalue that thisBlobobject represents, starting at positionpos, and returns the number of bytes written.intsetBytes(long pos, byte[] bytes, int offset, int length)Writes all or part of the givenbytearray to theBLOBvalue that thisBlobobject represents and returns the number of bytes written.voidtruncate(long length)Truncates theBLOBvalue that thisBlobobject represents to belenbytes in length.
-
Constructor Details
-
SerialBlob
Constructs aSerialBlobobject that is a serialized version of the givenbytearray.The new
SerialBlobobject is initialized with the data from thebytearray, thus allowing disconnectedRowSetobjects to establish serializedBlobobjects without touching the data source.- Parameters:
b- thebytearray containing the data for theBlobobject to be serialized- Throws:
SerialException- if an error occurs during serializationSQLException- if a SQL errors occurs
-
SerialBlob
Constructs aSerialBlobobject that is a serialized version of the givenBlobobject.The new
SerialBlobobject is initialized with the data from theBlobobject; therefore, theBlobobject should have previously brought the SQLBLOBvalue's data over to the client from the database. Otherwise, the newSerialBlobobject will contain no data.- Parameters:
blob- theBlobobject from which thisSerialBlobobject is to be constructed; cannot be null.- Throws:
SerialException- if an error occurs during serializationSQLException- if theBlobpassed to this to this constructor is anull.- See Also:
Blob
-
-
Method Details
-
getBytes
Copies the specified number of bytes, starting at the given position, from thisSerialBlobobject to another array of bytes.Note that if the given number of bytes to be copied is larger than the length of this
SerialBlobobject's array of bytes, the given number will be shortened to the array's length.- Specified by:
getBytesin interfaceBlob- Parameters:
pos- the ordinal position of the first byte in thisSerialBlobobject to be copied; numbering starts at1; must not be less than1and must be less than or equal to the length of thisSerialBlobobjectlength- the number of bytes to be copied- Returns:
- an array of bytes that is a copy of a region of this
SerialBlobobject, starting at the given position and containing the given number of consecutive bytes - Throws:
SerialException- if the given starting position is out of bounds; iffreehad previously been called on this object- See Also:
Blob.setBytes(long, byte[])
-
length
Retrieves the number of bytes in thisSerialBlobobject's array of bytes.- Specified by:
lengthin interfaceBlob- Returns:
- a
longindicating the length in bytes of thisSerialBlobobject's array of bytes - Throws:
SerialException- if an error occurs; iffreehad previously been called on this object
-
getBinaryStream
Returns thisSerialBlobobject as an input stream. Unlike the related method,setBinaryStream, a stream is produced regardless of whether theSerialBlobwas created with aBlobobject or abytearray.- Specified by:
getBinaryStreamin interfaceBlob- Returns:
- a
java.io.InputStreamobject that contains thisSerialBlobobject's array of bytes - Throws:
SerialException- if an error occurs; iffreehad previously been called on this object- See Also:
setBinaryStream(long)
-
position
Returns the position in thisSerialBlobobject where the given pattern of bytes begins, starting the search at the specified position.- Specified by:
positionin interfaceBlob- Parameters:
pattern- the pattern of bytes for which to searchstart- the position of the byte in thisSerialBlobobject from which to begin the search; the first position is1; must not be less than1nor greater than the length of thisSerialBlobobject- Returns:
- the position in this
SerialBlobobject where the given pattern begins, starting at the specified position;-1if the pattern is not found or the given starting position is out of bounds; position numbering for the return value starts at1 - Throws:
SerialException- if an error occurs when serializing the blob; iffreehad previously been called on this objectSQLException- if there is an error accessing theBLOBvalue from the database
-
position
Returns the position in thisSerialBlobobject where the givenBlobobject begins, starting the search at the specified position.- Specified by:
positionin interfaceBlob- Parameters:
pattern- theBlobobject for which to search;start- the position of the byte in thisSerialBlobobject from which to begin the search; the first position is1; must not be less than1nor greater than the length of thisSerialBlobobject- Returns:
- the position in this
SerialBlobobject where the givenBlobobject begins, starting at the specified position;-1if the pattern is not found or the given starting position is out of bounds; position numbering for the return value starts at1 - Throws:
SerialException- if an error occurs when serializing the blob; iffreehad previously been called on this objectSQLException- if there is an error accessing theBLOBvalue from the database
-
setBytes
Writes the given array of bytes to theBLOBvalue that thisBlobobject represents, starting at positionpos, and returns the number of bytes written.- Specified by:
setBytesin interfaceBlob- Parameters:
pos- the position in the SQLBLOBvalue at which to start writing. The first position is1; must not be less than1nor greater than the length of thisSerialBlobobject.bytes- the array of bytes to be written to theBLOBvalue that thisBlobobject represents- Returns:
- the number of bytes written
- Throws:
SerialException- if there is an error accessing theBLOBvalue; or if an invalid position is set; if an invalid offset value is set; iffreehad previously been called on this objectSQLException- if there is an error accessing theBLOBvalue from the database- See Also:
getBytes(long, int)
-
setBytes
public int setBytes(long pos, byte[] bytes, int offset, int length) throws SerialException, SQLExceptionWrites all or part of the givenbytearray to theBLOBvalue that thisBlobobject represents and returns the number of bytes written. Writing starts at positionposin theBLOBvalue; len bytes from the given byte array are written.- Specified by:
setBytesin interfaceBlob- Parameters:
pos- the position in theBLOBobject at which to start writing. The first position is1; must not be less than1nor greater than the length of thisSerialBlobobject.bytes- the array of bytes to be written to theBLOBvalueoffset- the offset in thebytearray at which to start reading the bytes. The first offset position is0; must not be less than0nor greater than the length of thebytearraylength- the number of bytes to be written to theBLOBvalue from the array of bytes bytes.- Returns:
- the number of bytes written
- Throws:
SerialException- if there is an error accessing theBLOBvalue; if an invalid position is set; if an invalid offset value is set; if number of bytes to be written is greater than theSerialBloblength; or the combined values of the length and offset is greater than the Blob buffer; iffreehad previously been called on this objectSQLException- if there is an error accessing theBLOBvalue from the database.- See Also:
getBytes(long, int)
-
setBinaryStream
Retrieves a stream that can be used to write to theBLOBvalue that thisBlobobject represents. The stream begins at positionpos. This method forwards thesetBinaryStream()call to the underlyingBlobin the event that thisSerialBlobobject is instantiated with aBlob. If thisSerialBlobis instantiated with abytearray, aSerialExceptionis thrown.- Specified by:
setBinaryStreamin interfaceBlob- Parameters:
pos- the position in theBLOBvalue at which to start writing- Returns:
- a
java.io.OutputStreamobject to which data can be written - Throws:
SQLException- if there is an error accessing theBLOBvalueSerialException- if the SerialBlob in not instantiated with aBlobobject that supportssetBinaryStream(); iffreehad previously been called on this object- See Also:
getBinaryStream()
-
truncate
Truncates theBLOBvalue that thisBlobobject represents to belenbytes in length.- Specified by:
truncatein interfaceBlob- Parameters:
length- the length, in bytes, to which theBLOBvalue that thisBlobobject represents should be truncated- Throws:
SerialException- if there is an error accessing the Blob value; or the length to truncate is greater that the SerialBlob length; iffreehad previously been called on this object
-
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:
InputStreamthrough which the partialBlobvalue can be read.- Throws:
SQLException- if pos is less than 1 or if pos is greater than the number of bytes in theBlobor if pos + length is greater than the number of bytes in theBlobSerialException- if thefreemethod had been previously called on this object- Since:
- 1.6
-
free
This method frees theSerialBlobobject and releases the resources that it holds. The object is invalid once thefreemethod is called.If
freeis called multiple times, the subsequent calls tofreeare treated as a no-op.- Specified by:
freein interfaceBlob- Throws:
SQLException- if an error occurs releasing the Blob's resources- Since:
- 1.6
-
equals
Compares this SerialBlob to the specified object. The result istrueif and only if the argument is notnulland is aSerialBlobobject that represents the same sequence of bytes as this object.- Overrides:
equalsin classObject- Parameters:
obj- The object to compare thisSerialBlobagainst- Returns:
trueif the given object represents aSerialBlobequivalent to this SerialBlob,falseotherwise- See Also:
Object.hashCode(),HashMap
-
hashCode
public int hashCode()Returns a hash code for thisSerialBlob.- Overrides:
hashCodein classObject- Returns:
- a hash code value for this object.
- See Also:
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
-
clone
Returns a clone of thisSerialBlob. The copy will contain a reference to a clone of the internal byte array, not a reference to the original internal byte array of thisSerialBlobobject. The underlyingBlobobject will be set to null.
-