public class BlobBuffer
extends java.lang.Object
lobBuffer
URL 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:
PAGE_SIZE
governs 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 the PAGE_MASK
and
BYTE_MASK
fields are also adjusted to match.
MAX_BUF_INC
value. Every time the buffer is
expanded the existing contents are copied and this may get expensive
with very large BLOBs.
Modifier and Type | Class and Description |
---|---|
private class |
BlobBuffer.AsciiInputStream
An ASCII
InputStream over the CLOB buffer. |
private class |
BlobBuffer.AsciiOutputStream
Implements an ASCII
OutputStream for CLOB data. |
private class |
BlobBuffer.BlobInputStream
An
InputStream over the BLOB buffer. |
private class |
BlobBuffer.BlobOutputStream
Implements an
OutputStream for BLOB data. |
private class |
BlobBuffer.UnicodeInputStream
A Big Endian Unicode
InputStream over the CLOB buffer. |
Modifier and Type | Field and Description |
---|---|
private java.io.File |
blobFile
The name of the temporary BLOB disk file.
|
private byte[] |
buffer
The BLOB buffer or the current page buffer.
|
private java.io.File |
bufferDir
The directory to buffer data to.
|
private boolean |
bufferDirty
Indicates page in memory must be saved.
|
private static int |
BYTE_MASK
Mask for page offset component of R/W pointer.
|
private int |
currentPage
The number of the current page in memory.
|
private static byte[] |
EMPTY_BUFFER
Default zero length buffer.
|
private static int |
INVALID_PAGE
Invalid page marker.
|
private boolean |
isMemOnly
True if attempts to create a BLOB file have failed or the buffer is
created without specifying a buffer directory.
|
private int |
length
The total length of the valid data in buffer.
|
private static int |
MAX_BUF_INC
Maximum buffer increment.
|
private int |
maxMemSize
The maximum size of an in memory buffer.
|
private int |
openCount
Count of callers that have opened the BLOB file.
|
private static int |
PAGE_MASK
Mask for page component of read/write pointer.
|
private static int |
PAGE_SIZE
Default page size (must be power of 2).
|
private java.io.RandomAccessFile |
raFile
The RA file object reference or null if closed.
|
Constructor and Description |
---|
BlobBuffer(java.io.File bufferDir,
long maxMemSize)
Creates a blob buffer.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Logically closes the file or physically close it if the open count is
now zero.
|
void |
createBlobFile()
Creates a random access disk file to use as backing storage for the LOB
data.
|
protected void |
finalize()
Finalizes this object by deleting any work files.
|
java.io.InputStream |
getBinaryStream(boolean ascii)
Retrieve the BLOB data as an
InputStream . |
byte[] |
getBytes(long pos,
int len)
Returns the BLOB data as a byte array.
|
long |
getLength()
Retrieves the length of this BLOB buffer in bytes.
|
java.io.InputStream |
getUnicodeStream()
Retrieve the BLOB data as an Big Endian Unicode
InputStream . |
void |
growBuffer(int minSize)
Increases the size of the in memory buffer for situations where disk
storage of BLOB is not possible.
|
void |
open()
Opens the BLOB disk file.
|
int |
position(byte[] pattern,
long start)
Provides support for pattern searching methods.
|
int |
read(int readPtr)
Reads byte from the BLOB buffer at the specified location.
|
int |
read(int readPtr,
byte[] bytes,
int offset,
int len)
Reads bytes from the BLOB buffer at the specified location.
|
void |
readPage(int page)
Reads in the specified page from the disk buffer.
|
java.io.OutputStream |
setBinaryStream(long pos,
boolean ascii)
Creates an
OutputStream that can be used to update the
BLOB. |
void |
setBuffer(byte[] bytes,
boolean copy)
Sets the initial buffer to an existing byte array.
|
int |
setBytes(long pos,
byte[] bytes,
int offset,
int len,
boolean copy)
Sets the content of the BLOB to the supplied byte array value.
|
void |
setLength(long length)
Retrieves the length of the BLOB buffer (in memory version only).
|
void |
truncate(long len)
Truncates the BLOB buffer to the specified size.
|
(package private) void |
write(int writePtr,
byte[] bytes,
int offset,
int len)
Inserts bytes into the buffer at the specified location.
|
void |
write(int writePtr,
int b)
Inserts a byte into the buffer at the specified location.
|
void |
writePage(int page)
Writes the specified page to the disk buffer.
|
private static final byte[] EMPTY_BUFFER
private static final int PAGE_SIZE
private static final int PAGE_MASK
private static final int BYTE_MASK
private static final int MAX_BUF_INC
private static final int INVALID_PAGE
private byte[] buffer
private int length
private int currentPage
private java.io.File blobFile
private java.io.RandomAccessFile raFile
private boolean bufferDirty
private int openCount
private boolean isMemOnly
private final java.io.File bufferDir
private final int maxMemSize
public BlobBuffer(java.io.File bufferDir, long maxMemSize)
bufferDir
- maxMemSize
- the maximum size of the in memory bufferprotected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public void createBlobFile()
public void open() throws java.io.IOException
java.io.IOException
- if an I/O error occurspublic int read(int readPtr) throws java.io.IOException
readPtr
- the offset in the buffer of the required byteint
or -1 if at EOFjava.io.IOException
- if an I/O error occurspublic int read(int readPtr, byte[] bytes, int offset, int len) throws java.io.IOException
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 readjava.io.IOException
- if an I/O error occurspublic void write(int writePtr, int b) throws java.io.IOException
writePtr
- the offset in the buffer of the required byteb
- the byte value to writejava.io.IOException
- if an I/O error occursvoid write(int writePtr, byte[] bytes, int offset, int len) throws java.io.IOException
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 writejava.io.IOException
- if an I/O error occurspublic void readPage(int page) throws java.io.IOException
page
- the page numberjava.io.IOException
- if an I/O error occurspublic void writePage(int page) throws java.io.IOException
page
- the page numberjava.io.IOException
- if an I/O error occurspublic void close() throws java.io.IOException
java.io.IOException
- if an I/O error occurspublic void growBuffer(int minSize)
minSize
- the minimum size of buffer requiredpublic void setBuffer(byte[] bytes, boolean copy)
bytes
- the byte array containing the BLOB datacopy
- true if a local copy of the data is requiredpublic byte[] getBytes(long pos, int len) throws java.sql.SQLException
pos
- the start position in the BLOB buffer (from 1)len
- the number of bytes to copybyte[]
java.sql.SQLException
public java.io.InputStream getBinaryStream(boolean ascii) throws java.sql.SQLException
InputStream
.ascii
- true if an ASCII input stream should be returnedInputStream
built over the BLOB datajava.sql.SQLException
- if an error occurspublic java.io.InputStream getUnicodeStream() throws java.sql.SQLException
InputStream
.InputStream
built over the BLOB datajava.sql.SQLException
- if an error occurspublic java.io.OutputStream setBinaryStream(long pos, boolean ascii) throws java.sql.SQLException
OutputStream
that 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.pos
- the start position in the buffer (from 1)ascii
- true if an ASCII output stream is requiredOutputStream
to be used to update the BLOBjava.sql.SQLException
- if an error occurspublic int setBytes(long pos, byte[] bytes, int offset, int len, boolean copy) throws java.sql.SQLException
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 requiredjava.sql.SQLException
- if an error occurspublic long getLength()
public void setLength(long length)
length
- the length of the valid data in the bufferpublic void truncate(long len) throws java.sql.SQLException
len
- the required lengthjava.sql.SQLException
- if an error occurspublic int position(byte[] pattern, long start) throws java.sql.SQLException
pattern
- the byte array containg the search patternstart
- the start position in the BLOB (from 1)int
start index for the pattern (from 1) or -1
if the pattern is not found.java.sql.SQLException
- if an error occursGenerated on June 8 2013