Package org.apache.derby.impl.jdbc
Class LOBStreamControl
- java.lang.Object
-
- org.apache.derby.impl.jdbc.LOBStreamControl
-
final class LOBStreamControl extends java.lang.ObjectThis class acts as a layer of blob/clob repository (in memory or file). The max bytes of data stored in memory depends on the way this class is created. If the class is created with initial data, the buffer size is set to the size of the byte array supplied, but no larger than MAX_BUF_SIZE. If no initial data is supplied, or if the initial data size is less than DEFAULT_BUF_SIZE, the buffer size is set to DEFAULT_BUF_SIZE. When write increases the data beyond this value a temporary file is created and data is moved into that. If truncate reduces the size of the file below initial buffer size, the data is moved into memory. This class also creates InputStream and OutputStream which can be used to access blob data irrespective of if its in memory or in file.
-
-
Field Summary
Fields Modifier and Type Field Description private intbufferSizeprivate EmbedConnectionconnprivate byte[]dataBytesprivate static intDEFAULT_BUF_SIZEprivate booleanisBytesprivate static intMAX_BUF_SIZEprivate LOBFiletmpFileprivate longupdateCount
-
Constructor Summary
Constructors Constructor Description LOBStreamControl(EmbedConnection conn)Creates an empty LOBStreamControl.LOBStreamControl(EmbedConnection conn, byte[] data)Creates a LOBStreamControl and initializes with a bytes array.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidcopyData(java.io.InputStream inStream, long length)Copies bytes from stream to local storage.(package private) longcopyUtf8Data(java.io.InputStream utf8Stream, long charLength)Copies UTF-8 encoded chars from a stream to local storage.private voiddeleteFile(StorageFile file)protected voidfinalize()private static java.lang.ObjectfindService(java.lang.String factoryInterface, java.lang.String serviceName)Privileged service lookup.private static java.lang.ObjectfindServiceModule(java.lang.Object serviceModule, java.lang.String factoryInterface)Privileged startup.(package private) voidfree()Invalidates all the variables and closes file handle if open.(package private) java.io.InputStreamgetInputStream(long pos)returns input stream linked with this object.(package private) longgetLength()Returns length of data.(package private) java.io.OutputStreamgetOutputStream(long pos)returns output stream linked with this object(package private) longgetUpdateCount()Returns the running sequence number to check if the lob is updated since last access.private voidinit(byte[] b, long len)private voidisValidOffset(int off, int length)private voidisValidPostion(long pos)(package private) intread(byte[] buff, int off, int len, long pos)Reads bytes starting from 'position' into bytes array.(package private) intread(long pos)Reads one byte.private intreadBytes(byte[] b, int off, int len, long pos)private voidreleaseTempFile(LOBFile file)Close and release all resources held by a temporary file.(package private) longreplaceBytes(byte[] buf, long stPos, long endPos)Replaces a block of bytes in the middle of the LOB with a another block of bytes, which may be of a different size.(package private) voidtruncate(long size)Resets the size.private longupdateData(byte[] bytes, int offset, int len, long pos)(package private) longwrite(byte[] b, int off, int len, long pos)Writeslenbytes from the specified byte array to the LOB.(package private) longwrite(int b, long pos)Writes one byte.
-
-
-
Field Detail
-
tmpFile
private LOBFile tmpFile
-
dataBytes
private byte[] dataBytes
-
isBytes
private boolean isBytes
-
bufferSize
private final int bufferSize
-
conn
private final EmbedConnection conn
-
updateCount
private long updateCount
-
DEFAULT_BUF_SIZE
private static final int DEFAULT_BUF_SIZE
- See Also:
- Constant Field Values
-
MAX_BUF_SIZE
private static final int MAX_BUF_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
LOBStreamControl
LOBStreamControl(EmbedConnection conn)
Creates an empty LOBStreamControl.- Parameters:
conn- Connection for this lob
-
LOBStreamControl
LOBStreamControl(EmbedConnection conn, byte[] data) throws java.io.IOException, StandardException
Creates a LOBStreamControl and initializes with a bytes array.- Parameters:
conn- Connection for this lobdata- initial value- Throws:
java.io.IOExceptionStandardException
-
-
Method Detail
-
init
private void init(byte[] b, long len) throws java.io.IOException, StandardException- Throws:
java.io.IOExceptionStandardException
-
updateData
private long updateData(byte[] bytes, int offset, int len, long pos) throws StandardException- Throws:
StandardException
-
isValidPostion
private void isValidPostion(long pos) throws java.io.IOException, StandardException- Throws:
java.io.IOExceptionStandardException
-
isValidOffset
private void isValidOffset(int off, int length) throws StandardException- Throws:
StandardException
-
write
long write(int b, long pos) throws java.io.IOException, StandardExceptionWrites one byte.- Parameters:
b- bytepos-- Returns:
- new position
- Throws:
java.io.IOException- if writing to the LOB file failsStandardException- if encrypting/decrypting the LOB file fails
-
write
long write(byte[] b, int off, int len, long pos) throws java.io.IOException, StandardExceptionWriteslenbytes from the specified byte array to the LOB.- Parameters:
b- byte arrayoff- offset from where to read from the byte arraylen- number of bytes to be writtenpos- starting position- Returns:
- The position after the bytes have been written to the LOB.
- Throws:
java.io.IOException- if writing to the LOB failsStandardException- if writing to the LOB failsjava.lang.IndexOutOfBoundsException- iflenis larger thanb.length - off
-
read
int read(long pos) throws java.io.IOException, StandardExceptionReads one byte.- Parameters:
pos- position from where to read- Returns:
- byte
- Throws:
java.io.IOException- if reading the LOB file failsStandardException- if decrypting an encrypted LOB file fails
-
readBytes
private int readBytes(byte[] b, int off, int len, long pos)
-
read
int read(byte[] buff, int off, int len, long pos) throws java.io.IOException, StandardExceptionReads bytes starting from 'position' into bytes array. starting from 'offset'- Parameters:
buff- array into the bytes will be copiedoff- offset from where the array has to be populatedlen- number of bytes to readpos- initial position before reading- Returns:
- number new position
- Throws:
java.io.IOException- if reading the LOB file failsStandardException- if decrypting an encrypted LOB file fails
-
getInputStream
java.io.InputStream getInputStream(long pos)
returns input stream linked with this object.- Parameters:
pos- initial postion- Returns:
- InputStream
-
getOutputStream
java.io.OutputStream getOutputStream(long pos)
returns output stream linked with this object- Parameters:
pos- initial postion- Returns:
- OutputStream
-
getLength
long getLength() throws java.io.IOExceptionReturns length of data.- Returns:
- length
- Throws:
java.io.IOException
-
truncate
void truncate(long size) throws java.io.IOException, StandardExceptionResets the size.- Parameters:
size- new size should be smaller than existing size- Throws:
java.io.IOExceptionStandardException
-
copyData
void copyData(java.io.InputStream inStream, long length) throws java.io.IOException, StandardExceptionCopies bytes from stream to local storage.Note that specifying the length as
Long.MAX_VALUEresults in reading data from the stream until EOF is reached, but no length checking will be performed.- Parameters:
inStream- the stream to copy fromlength- number of bytes to be copied, orLong.MAX_VALUEto copy everything until EOF is reached- Throws:
java.io.IOException- if reading or writing a LOB file failsStandardException- if encrypting or decrypting a LOB file fails
-
copyUtf8Data
long copyUtf8Data(java.io.InputStream utf8Stream, long charLength) throws java.io.IOException, StandardExceptionCopies UTF-8 encoded chars from a stream to local storage.Note that specifying the length as
Long.MAX_VALUEresults in reading data from the stream until EOF is reached, but no length checking will be performed.- Parameters:
utf8Stream- the stream to copy fromcharLength- number of chars to be copied, orLong.MAX_VALUEto copy everything until EOF is reached- Returns:
- The number of characters copied.
- Throws:
java.io.EOFException- if EOF is reached prematurelyjava.io.IOException- thrown on a number of error conditionsStandardException- if reading, writing or truncating theLOBStreamControl-object failsjava.io.UTFDataFormatException- if an invalid UTF-8 encoding is detected
-
finalize
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
deleteFile
private void deleteFile(StorageFile file)
-
free
void free() throws java.io.IOException
Invalidates all the variables and closes file handle if open.- Throws:
java.io.IOException- if closing the file fails
-
releaseTempFile
private void releaseTempFile(LOBFile file) throws java.io.IOException
Close and release all resources held by a temporary file. The file will also be deleted from the file system and removed from the list ofLOBFiles inEmbedConnection.- Parameters:
file- the temporary file- Throws:
java.io.IOException- if the file cannot be closed or deleted
-
replaceBytes
long replaceBytes(byte[] buf, long stPos, long endPos) throws java.io.IOException, StandardExceptionReplaces a block of bytes in the middle of the LOB with a another block of bytes, which may be of a different size.The new byte array may not be be of same length as the original, thus it may result in resizing the total lob.
- Parameters:
buf- byte array which will be written inplace of old blockstPos- inclusive starting position of current blockendPos- exclusive end position of current block- Returns:
- Current position after write.
- Throws:
java.io.IOException- if writing to the temporary file failsStandardException
-
getUpdateCount
long getUpdateCount()
Returns the running sequence number to check if the lob is updated since last access.- Returns:
- The current update sequence number.
-
findServiceModule
private static java.lang.Object findServiceModule(java.lang.Object serviceModule, java.lang.String factoryInterface) throws StandardExceptionPrivileged startup. Must be private so that user code can't call this entry point.- Throws:
StandardException
-
findService
private static java.lang.Object findService(java.lang.String factoryInterface, java.lang.String serviceName)Privileged service lookup. Must be private so that user code can't call this entry point.
-
-