org.apache.derby.impl.jdbc
Class LOBStreamControl

java.lang.Object
  extended by org.apache.derby.impl.jdbc.LOBStreamControl

 class LOBStreamControl
extends java.lang.Object

This 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. If no initial data is supplied or if the initial data size is less than DEFAULT_MAX_BUF_SIZE, The buffer size is set to DEFAULT_MAX_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 (max of DEFAULT_MAX_BUF_SIZE and initial byte array size) the data 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
private  int bufferSize
           
private  EmbedConnection conn
           
private  byte[] dataBytes
           
private static int DEFAULT_MAX_BUF_SIZE
           
private  boolean isBytes
           
private  LOBFile tmpFile
           
private  long updateCount
           
 
Constructor Summary
LOBStreamControl(EmbedConnection conn)
          Creates an empty LOBStreamControl.
LOBStreamControl(EmbedConnection conn, byte[] data)
          Creates a LOBStreamControl and initializes with a bytes array.
 
Method Summary
(package private)  void copyData(java.io.InputStream inStream, long length)
          Copies bytes from stream to local storage.
(package private)  long copyUtf8Data(java.io.InputStream utf8Stream, long charLength)
          Copies UTF-8 encoded chars from a stream to local storage.
private  void deleteFile(StorageFile file)
           
protected  void finalize()
           
(package private)  void free()
          Invalidates all the variables and closes file handle if open.
(package private)  java.io.InputStream getInputStream(long pos)
          returns input stream linked with this object.
(package private)  long getLength()
          Returns length of data.
(package private)  java.io.OutputStream getOutputStream(long pos)
          returns output stream linked with this object
(package private)  long getUpdateCount()
          Returns the running secquence number to check if the lob is updated since last access.
private  void init(byte[] b, long len)
           
private  void isValidOffset(int off, int length)
           
private  void isValidPostion(long pos)
           
(package private)  int read(byte[] buff, int off, int len, long pos)
          Reads bytes starting from 'position' into bytes array.
(package private)  int read(long pos)
          Reads one byte.
private  int readBytes(byte[] b, int off, int len, long pos)
           
private  void releaseTempFile(LOBFile file)
          Close and release all resources held by a temporary file.
(package private)  long replaceBytes(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)  void truncate(long size)
          Resets the size.
private  long updateData(byte[] bytes, int offset, int len, long pos)
           
(package private)  long write(byte[] b, int off, int len, long pos)
          Writes len bytes from the specified byte array to the LOB.
(package private)  long write(int b, long pos)
          Writes one byte.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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_MAX_BUF_SIZE

private static final int DEFAULT_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 lob
data - initial value
Throws:
java.io.IOException
StandardException
Method Detail

init

private void init(byte[] b,
                  long len)
           throws java.io.IOException,
                  StandardException
Throws:
java.io.IOException
StandardException

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.IOException
StandardException

isValidOffset

private void isValidOffset(int off,
                           int length)
                    throws StandardException
Throws:
StandardException

write

long write(int b,
           long pos)
     throws java.io.IOException,
            StandardException
Writes one byte.

Parameters:
b - byte
pos -
Returns:
new postion
Throws:
IOException, - StandardException
java.io.IOException
StandardException

write

long write(byte[] b,
           int off,
           int len,
           long pos)
     throws java.io.IOException,
            StandardException
Writes len bytes from the specified byte array to the LOB.

Parameters:
b - byte array
off - offset from where to read from the byte array
len - number of bytes to be written
pos - starting position
Returns:
The position after the bytes have been written to the LOB.
Throws:
java.io.IOException - if writing to the LOB fails
StandardException - if writing to the LOB fails
java.lang.IndexOutOfBoundsException - if len is larger than b.length - off

read

int read(long pos)
   throws java.io.IOException,
          StandardException
Reads one byte.

Parameters:
pos - postion from where to read
Returns:
byte
Throws:
IOException, - StandardException
java.io.IOException
StandardException

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,
          StandardException
Reads bytes starting from 'position' into bytes array. starting from 'offset'

Parameters:
buff - array into the bytes will be copied
off - offset from where the array has to be populated
len - number of bytes to read
pos - initial postion before reading
Returns:
number new postion
Throws:
IOException, - StandardException
java.io.IOException
StandardException

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.IOException
Returns length of data.

Returns:
length
Throws:
java.io.IOException

truncate

void truncate(long size)
        throws java.io.IOException,
               StandardException
Resets the size.

Parameters:
size - new size should be smaller than exisiting size
Throws:
java.io.IOException
StandardException

copyData

void copyData(java.io.InputStream inStream,
              long length)
        throws java.io.IOException,
               StandardException
Copies bytes from stream to local storage.

Note that specifying the length as Long.MAX_VALUE results in reading data from the stream until EOF is reached, but no length checking will be performed.

Parameters:
inStream - the stream to copy from
length - number of bytes to be copied, or Long.MAX_VALUE to copy everything until EOF is reached
Throws:
IOException, - StandardException
java.io.IOException
StandardException

copyUtf8Data

long copyUtf8Data(java.io.InputStream utf8Stream,
                  long charLength)
            throws java.io.IOException,
                   StandardException
Copies UTF-8 encoded chars from a stream to local storage.

Note that specifying the length as Long.MAX_VALUE results in reading data from the stream until EOF is reached, but no length checking will be performed.

Parameters:
utf8Stream - the stream to copy from
charLength - number of chars to be copied, or Long.MAX_VALUE to copy everything until EOF is reached
Returns:
The number of characters copied.
Throws:
java.io.EOFException - if EOF is reached prematurely
java.io.IOException - thrown on a number of error conditions
StandardException - if reading, writing or truncating the LOBStreamControl-object fails
java.io.UTFDataFormatException - if an invalid UTF-8 encoding is detected

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

deleteFile

private void deleteFile(StorageFile file)
                 throws java.io.IOException
Throws:
java.io.IOException

free

void free()
    throws java.io.IOException
Invalidates all the variables and closes file handle if open.

Throws:
IOexception
java.io.IOException

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 of LOBFiles in EmbedConnection.

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,
                   StandardException
Replaces 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 block
stPos - inclusive starting position of current block
endPos - exclusive end position of current block
Returns:
Current position after write.
Throws:
IOExcepton - if writing to temporary file fails
StandardException
java.io.IOException

getUpdateCount

long getUpdateCount()
Returns the running secquence number to check if the lob is updated since last access.

Returns:
The current update sequence number.

Built on Thu 2012-03-29 21:53:33+0000, from revision ???

Apache Derby V10.6 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.