Package net.sourceforge.jtds.util
Class BlobBuffer
- java.lang.Object
-
- net.sourceforge.jtds.util.BlobBuffer
-
public class BlobBuffer extends java.lang.ObjectManages a buffer (backed by optional disk storage) for use as a data store by the CLOB and BLOB objects. The data can be purely memory based until the size exceeds the value dictated by thelobBufferURL property after which it will be written to disk. The disk array is accessed randomly one page (1024 bytes) at a time. This class is not synchronized and concurrent open input and output streams can conflict. Tuning hints:- The
PAGE_SIZEgoverns how much data is buffered when reading or writing data a byte at a time. 1024 bytes seems to work well but if very large objects are being written a byte at a time 4096 may be better. NB. ensure that thePAGE_MASKandBYTE_MASKfields are also adjusted to match. - Reading or writing byte arrays that are greater than or equal to the page size will go directly to or from the random access file cutting out an ArrayCopy operation.
- If BLOBs are being buffered exclusively in memory you may wish to
adjust the
MAX_BUF_INCvalue. Every time the buffer is expanded the existing contents are copied and this may get expensive with very large BLOBs. - The BLOB file will be kept open for as long as there are open input or output streams. Therefore BLOB streams should be explicitly closed as soon as they are finished with.
- Version:
- $Id: BlobBuffer.java,v 1.4.2.1 2009-08-03 12:31:00 ickzon Exp $
- Author:
- Mike Hutchinson
- The
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classBlobBuffer.AsciiInputStreamAn ASCIIInputStreamover the CLOB buffer.private classBlobBuffer.AsciiOutputStreamImplements an ASCIIOutputStreamfor CLOB data.private classBlobBuffer.BlobInputStreamAnInputStreamover the BLOB buffer.private classBlobBuffer.BlobOutputStreamImplements anOutputStreamfor BLOB data.private classBlobBuffer.UnicodeInputStreamA Big Endian UnicodeInputStreamover the CLOB buffer.
-
Field Summary
Fields Modifier and Type Field Description private java.io.FileblobFileThe name of the temporary BLOB disk file.private byte[]bufferThe BLOB buffer or the current page buffer.private java.io.FilebufferDirThe directory to buffer data to.private booleanbufferDirtyIndicates page in memory must be saved.private static intBYTE_MASKMask for page offset component of R/W pointer.private intcurrentPageThe number of the current page in memory.private static byte[]EMPTY_BUFFERDefault zero length buffer.private static intINVALID_PAGEInvalid page marker.private booleanisMemOnlyTrue if attempts to create a BLOB file have failed or the buffer is created without specifying a buffer directory.private intlengthThe total length of the valid data in buffer.private static intMAX_BUF_INCMaximum buffer increment.private intmaxMemSizeThe maximum size of an in memory buffer.private intopenCountCount of callers that have opened the BLOB file.private static intPAGE_MASKMask for page component of read/write pointer.private static intPAGE_SIZEDefault page size (must be power of 2).private java.io.RandomAccessFileraFileThe RA file object reference or null if closed.
-
Constructor Summary
Constructors Constructor Description BlobBuffer(java.io.File bufferDir, long maxMemSize)Creates a blob buffer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Logically closes the file or physically close it if the open count is now zero.voidcreateBlobFile()Creates a random access disk file to use as backing storage for the LOB data.protected voidfinalize()Finalizes this object by deleting any work files.java.io.InputStreamgetBinaryStream(boolean ascii)Retrieve the BLOB data as anInputStream.byte[]getBytes(long pos, int len)Returns the BLOB data as a byte array.longgetLength()Retrieves the length of this BLOB buffer in bytes.java.io.InputStreamgetUnicodeStream()Retrieve the BLOB data as an Big Endian UnicodeInputStream.voidgrowBuffer(int minSize)Increases the size of the in memory buffer for situations where disk storage of BLOB is not possible.voidopen()Opens the BLOB disk file.intposition(byte[] pattern, long start)Provides support for pattern searching methods.intread(int readPtr)Reads byte from the BLOB buffer at the specified location.intread(int readPtr, byte[] bytes, int offset, int len)Reads bytes from the BLOB buffer at the specified location.voidreadPage(int page)Reads in the specified page from the disk buffer.java.io.OutputStreamsetBinaryStream(long pos, boolean ascii)Creates anOutputStreamthat can be used to update the BLOB.voidsetBuffer(byte[] bytes, boolean copy)Sets the initial buffer to an existing byte array.intsetBytes(long pos, byte[] bytes, int offset, int len, boolean copy)Sets the content of the BLOB to the supplied byte array value.voidsetLength(long length)Retrieves the length of the BLOB buffer (in memory version only).voidtruncate(long len)Truncates the BLOB buffer to the specified size.(package private) voidwrite(int writePtr, byte[] bytes, int offset, int len)Inserts bytes into the buffer at the specified location.voidwrite(int writePtr, int b)Inserts a byte into the buffer at the specified location.voidwritePage(int page)Writes the specified page to the disk buffer.
-
-
-
Field Detail
-
EMPTY_BUFFER
private static final byte[] EMPTY_BUFFER
Default zero length buffer.
-
PAGE_SIZE
private static final int PAGE_SIZE
Default page size (must be power of 2).- See Also:
- Constant Field Values
-
PAGE_MASK
private static final int PAGE_MASK
Mask for page component of read/write pointer.- See Also:
- Constant Field Values
-
BYTE_MASK
private static final int BYTE_MASK
Mask for page offset component of R/W pointer.- See Also:
- Constant Field Values
-
MAX_BUF_INC
private static final int MAX_BUF_INC
Maximum buffer increment.- See Also:
- Constant Field Values
-
INVALID_PAGE
private static final int INVALID_PAGE
Invalid page marker.- See Also:
- Constant Field Values
-
buffer
private byte[] buffer
The BLOB buffer or the current page buffer.
-
length
private int length
The total length of the valid data in buffer.
-
currentPage
private int currentPage
The number of the current page in memory.
-
blobFile
private java.io.File blobFile
The name of the temporary BLOB disk file.
-
raFile
private java.io.RandomAccessFile raFile
The RA file object reference or null if closed.
-
bufferDirty
private boolean bufferDirty
Indicates page in memory must be saved.
-
openCount
private int openCount
Count of callers that have opened the BLOB file.
-
isMemOnly
private boolean isMemOnly
True if attempts to create a BLOB file have failed or the buffer is created without specifying a buffer directory.
-
bufferDir
private final java.io.File bufferDir
The directory to buffer data to.
-
maxMemSize
private final int maxMemSize
The maximum size of an in memory buffer.
-
-
Method Detail
-
finalize
protected void finalize() throws java.lang.ThrowableFinalizes this object by deleting any work files.- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
createBlobFile
public void createBlobFile()
Creates a random access disk file to use as backing storage for the LOB data. This method may fail due to security exceptions or local disk problems, in which case the blob storage will remain entirely in memory.
-
open
public void open() throws java.io.IOExceptionOpens the BLOB disk file. A count of open and close requests is kept so that the file may be closed when no longer required thus keeping the number of open files to a minimum.- Throws:
java.io.IOException- if an I/O error occurs
-
read
public int read(int readPtr) throws java.io.IOExceptionReads byte from the BLOB buffer at the specified location. The read pointer is partitioned into a page number and an offset within the page. This routine will read new pages as required. The page size must be a power of 2 and is currently set to 1024 bytes.- Parameters:
readPtr- the offset in the buffer of the required byte- Returns:
- the byte value as an
intor -1 if at EOF - Throws:
java.io.IOException- if an I/O error occurs
-
read
public int read(int readPtr, byte[] bytes, int offset, int len) throws java.io.IOExceptionReads bytes from the BLOB buffer at the specified location.- Parameters:
readPtr- the offset in the buffer of the required bytebytes- the byte array to filloffset- the start position in the byte arraylen- the number of bytes to read- Returns:
- the number of bytes read or -1 if at end of file
- Throws:
java.io.IOException- if an I/O error occurs
-
write
public void write(int writePtr, int b) throws java.io.IOExceptionInserts a byte into the buffer at the specified location. The write pointer is partitioned into a page number and an offset within the page. This routine will write new pages as required. The page size must be a power of 2 and is currently set to 1024 bytes.- Parameters:
writePtr- the offset in the buffer of the required byteb- the byte value to write- Throws:
java.io.IOException- if an I/O error occurs
-
write
void write(int writePtr, byte[] bytes, int offset, int len) throws java.io.IOExceptionInserts bytes into the buffer at the specified location.- Parameters:
writePtr- the offset in the buffer of the required bytebytes- the byte array value to writeoffset- the start position in the byte arraylen- the number of bytes to write- Throws:
java.io.IOException- if an I/O error occurs
-
readPage
public void readPage(int page) throws java.io.IOExceptionReads in the specified page from the disk buffer. Any existing dirty page is first saved to disk.- Parameters:
page- the page number- Throws:
java.io.IOException- if an I/O error occurs
-
writePage
public void writePage(int page) throws java.io.IOExceptionWrites the specified page to the disk buffer.- Parameters:
page- the page number- Throws:
java.io.IOException- if an I/O error occurs
-
close
public void close() throws java.io.IOExceptionLogically closes the file or physically close it if the open count is now zero. Any updated buffer in memory is flushed to disk before the file is closed.- Throws:
java.io.IOException- if an I/O error occurs
-
growBuffer
public void growBuffer(int minSize)
Increases the size of the in memory buffer for situations where disk storage of BLOB is not possible.- Parameters:
minSize- the minimum size of buffer required
-
setBuffer
public void setBuffer(byte[] bytes, boolean copy)Sets the initial buffer to an existing byte array.- Parameters:
bytes- the byte array containing the BLOB datacopy- true if a local copy of the data is required
-
getBytes
public byte[] getBytes(long pos, int len) throws java.sql.SQLExceptionReturns the BLOB data as a byte array.- Parameters:
pos- the start position in the BLOB buffer (from 1)len- the number of bytes to copy- Returns:
- the requested data as a
byte[] - Throws:
java.sql.SQLException
-
getBinaryStream
public java.io.InputStream getBinaryStream(boolean ascii) throws java.sql.SQLExceptionRetrieve the BLOB data as anInputStream.- Parameters:
ascii- true if an ASCII input stream should be returned- Returns:
- the
InputStreambuilt over the BLOB data - Throws:
java.sql.SQLException- if an error occurs
-
getUnicodeStream
public java.io.InputStream getUnicodeStream() throws java.sql.SQLExceptionRetrieve the BLOB data as an Big Endian UnicodeInputStream.- Returns:
- the
InputStreambuilt over the BLOB data - Throws:
java.sql.SQLException- if an error occurs
-
setBinaryStream
public java.io.OutputStream setBinaryStream(long pos, boolean ascii) throws java.sql.SQLExceptionCreates anOutputStreamthat can be used to update the BLOB. Given that we cannot know the final size of a BLOB created by the caller of this method, we assume the worst and create a disk BLOB by default.- Parameters:
pos- the start position in the buffer (from 1)ascii- true if an ASCII output stream is required- Returns:
- the
OutputStreamto be used to update the BLOB - Throws:
java.sql.SQLException- if an error occurs
-
setBytes
public int setBytes(long pos, byte[] bytes, int offset, int len, boolean copy) throws java.sql.SQLExceptionSets the content of the BLOB to the supplied byte array value. If the following conditions are met:- The start position is 1
- The existing BLOB length is smaller or the same as the length of the new data
- The new data length does not exceed the in memory limit
- Parameters:
pos- the start position in the buffer (from 1)bytes- the byte array containing the data to copyoffset- the start position in the byte array (from 0)len- the number of bytes to copycopy- true if a local copy of the byte array is required- Returns:
- the number of bytes copied
- Throws:
java.sql.SQLException- if an error occurs
-
getLength
public long getLength()
Retrieves the length of this BLOB buffer in bytes.- Returns:
- the length of the BLOB data in bytes
-
setLength
public void setLength(long length)
Retrieves the length of the BLOB buffer (in memory version only).- Parameters:
length- the length of the valid data in the buffer
-
truncate
public void truncate(long len) throws java.sql.SQLExceptionTruncates the BLOB buffer to the specified size.- Parameters:
len- the required length- Throws:
java.sql.SQLException- if an error occurs
-
position
public int position(byte[] pattern, long start) throws java.sql.SQLExceptionProvides support for pattern searching methods.- Parameters:
pattern- the byte array containg the search patternstart- the start position in the BLOB (from 1)- Returns:
- the
intstart index for the pattern (from 1) or -1 if the pattern is not found. - Throws:
java.sql.SQLException- if an error occurs
-
-