Parent Directory | Revision Log | Patch
--- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java 2005/05/02 05:18:08 165584 +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java 2005/05/02 06:25:59 165585 @@ -20,257 +20,283 @@ package org.apache.derby.client.am; -public class Blob extends Lob implements java.sql.Blob -{ - //-----------------------------state------------------------------------------ - - byte[] binaryString_ = null; - - // Only used for input purposes. For output, each getBinaryStream call - // must generate an independent stream. - java.io.InputStream binaryStream_ = null; - int dataOffset_; - - //---------------------constructors/finalizer--------------------------------- - - public Blob (byte[] binaryString, - Agent agent, - int dataOffset) - { - super (agent); - binaryString_ = binaryString; - dataType_ |= BINARY_STRING; - sqlLength_ = binaryString.length - dataOffset; - lengthObtained_ = true; - dataOffset_ = dataOffset; - } - - // CTOR for input: - public Blob (Agent agent, - java.io.InputStream binaryStream, - int length) - { - super (agent); - binaryStream_ = binaryStream; - dataType_ |= BINARY_STREAM; - sqlLength_ = length; - lengthObtained_ = true; - } - - // ---------------------------jdbc 2------------------------------------------ - - public long length () throws SqlException - { - synchronized (agent_.connection_) { - if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, "length"); - long retVal = super.sqlLength(); - if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, "length", retVal); - return retVal; - } - } - - // can return an array that may be have a length shorter than the supplied - // length (no padding occurs) - public byte[] getBytes (long pos, int length) throws SqlException - { - synchronized (agent_.connection_) { - if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, "getBytes", (int) pos, length); - if ((pos <= 0) || (length < 0)) - throw new SqlException (agent_.logWriter_, "Invalid position " + pos +" or length " + length); - byte[] retVal = getBytesX (pos, length); - if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, "getBytes", retVal); - return retVal; - } - } - - private byte[] getBytesX (long pos, int length) throws SqlException - { - checkForClosedConnection (); - - // we may need to check for overflow on this cast - long actualLength = Math.min (this.length() - pos + 1, (long) length); - - byte[] retVal = new byte[(int) actualLength]; - System.arraycopy (binaryString_, (int) pos + dataOffset_ - 1, retVal, 0, (int) actualLength); - return retVal; - } - - - public java.io.InputStream getBinaryStream () throws SqlException - { - synchronized (agent_.connection_) { - if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, "getBinaryStream"); - java.io.InputStream retVal = getBinaryStreamX(); - if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, "getBinaryStream", retVal); - return retVal; - } - } - - private java.io.InputStream getBinaryStreamX () throws SqlException - { - checkForClosedConnection (); - - if (isBinaryStream ()) // this Lob is used for input - return binaryStream_; - - return new java.io.ByteArrayInputStream (binaryString_, dataOffset_, binaryString_.length - dataOffset_); - } - - public long position (byte[] pattern, long start) throws SqlException - { - synchronized (agent_.connection_) { - if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, "position(byte[], long)", pattern, start); - if (pattern == null) throw new SqlException (agent_.logWriter_, "Search pattern cannot be null."); - long pos = positionX (pattern, start); - if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, "position(byte[], long)", pos); - return pos; - } - } - - private long positionX (byte[] pattern, long start) throws SqlException - { - checkForClosedConnection (); - - return binaryStringPosition(pattern, start); - } - - public long position (java.sql.Blob pattern, long start) throws SqlException - { - synchronized (agent_.connection_) { - if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, "position(Blob, long)", pattern, start); - if (pattern == null) throw new SqlException (agent_.logWriter_, "Search pattern cannot be null."); - long pos = positionX (pattern, start); - if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, "position(Blob, long)", pos); - return pos; - } - } - - private long positionX (java.sql.Blob pattern, long start) throws SqlException - { - checkForClosedConnection (); +public class Blob extends Lob implements java.sql.Blob { + //-----------------------------state------------------------------------------ - try { - return binaryStringPosition(pattern.getBytes(1L, (int)pattern.length()) , start); - } - catch (java.sql.SQLException e) { - throw new SqlException (agent_.logWriter_, e.getMessage()); + byte[] binaryString_ = null; + + // Only used for input purposes. For output, each getBinaryStream call + // must generate an independent stream. + java.io.InputStream binaryStream_ = null; + int dataOffset_; + + //---------------------constructors/finalizer--------------------------------- + + public Blob(byte[] binaryString, + Agent agent, + int dataOffset) { + super(agent); + binaryString_ = binaryString; + dataType_ |= BINARY_STRING; + sqlLength_ = binaryString.length - dataOffset; + lengthObtained_ = true; + dataOffset_ = dataOffset; + } + + // CTOR for input: + public Blob(Agent agent, + java.io.InputStream binaryStream, + int length) { + super(agent); + binaryStream_ = binaryStream; + dataType_ |= BINARY_STREAM; + sqlLength_ = length; + lengthObtained_ = true; + } + + // ---------------------------jdbc 2------------------------------------------ + + public long length() throws SqlException { + synchronized (agent_.connection_) { + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceEntry(this, "length"); + } + long retVal = super.sqlLength(); + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceExit(this, "length", retVal); + } + return retVal; + } + } + + // can return an array that may be have a length shorter than the supplied + // length (no padding occurs) + public byte[] getBytes(long pos, int length) throws SqlException { + synchronized (agent_.connection_) { + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceEntry(this, "getBytes", (int) pos, length); + } + if ((pos <= 0) || (length < 0)) { + throw new SqlException(agent_.logWriter_, "Invalid position " + pos + " or length " + length); + } + byte[] retVal = getBytesX(pos, length); + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceExit(this, "getBytes", retVal); + } + return retVal; + } + } + + private byte[] getBytesX(long pos, int length) throws SqlException { + checkForClosedConnection(); + + // we may need to check for overflow on this cast + long actualLength = Math.min(this.length() - pos + 1, (long) length); + + byte[] retVal = new byte[(int) actualLength]; + System.arraycopy(binaryString_, (int) pos + dataOffset_ - 1, retVal, 0, (int) actualLength); + return retVal; + } + + + public java.io.InputStream getBinaryStream() throws SqlException { + synchronized (agent_.connection_) { + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceEntry(this, "getBinaryStream"); + } + java.io.InputStream retVal = getBinaryStreamX(); + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceExit(this, "getBinaryStream", retVal); + } + return retVal; + } + } + + private java.io.InputStream getBinaryStreamX() throws SqlException { + checkForClosedConnection(); + + if (isBinaryStream()) // this Lob is used for input + { + return binaryStream_; + } + + return new java.io.ByteArrayInputStream(binaryString_, dataOffset_, binaryString_.length - dataOffset_); + } + + public long position(byte[] pattern, long start) throws SqlException { + synchronized (agent_.connection_) { + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceEntry(this, "position(byte[], long)", pattern, start); + } + if (pattern == null) { + throw new SqlException(agent_.logWriter_, "Search pattern cannot be null."); + } + long pos = positionX(pattern, start); + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceExit(this, "position(byte[], long)", pos); + } + return pos; + } + } + + private long positionX(byte[] pattern, long start) throws SqlException { + checkForClosedConnection(); + + return binaryStringPosition(pattern, start); + } + + public long position(java.sql.Blob pattern, long start) throws SqlException { + synchronized (agent_.connection_) { + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceEntry(this, "position(Blob, long)", pattern, start); + } + if (pattern == null) { + throw new SqlException(agent_.logWriter_, "Search pattern cannot be null."); + } + long pos = positionX(pattern, start); + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceExit(this, "position(Blob, long)", pos); + } + return pos; + } + } + + private long positionX(java.sql.Blob pattern, long start) throws SqlException { + checkForClosedConnection(); + + try { + return binaryStringPosition(pattern.getBytes(1L, (int) pattern.length()), start); + } catch (java.sql.SQLException e) { + throw new SqlException(agent_.logWriter_, e.getMessage()); + } } - } // -------------------------- JDBC 3.0 ----------------------------------- - public int setBytes (long pos, byte[] bytes) throws SqlException - { - synchronized (agent_.connection_) { - if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, "setBytes", (int) pos, bytes); - int length = setBytesX (pos, bytes, 0, bytes.length); - if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, "setBytes", length); - return length; - } - } - - public int setBytes (long pos, byte[] bytes, int offset, int len) throws SqlException - { - synchronized (agent_.connection_) { - if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, "setBytes", (int) pos, bytes, offset, len); - int length = setBytesX (pos, bytes, offset, len); - if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, "setBytes", length); - return length; - } - } + public int setBytes(long pos, byte[] bytes) throws SqlException { + synchronized (agent_.connection_) { + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceEntry(this, "setBytes", (int) pos, bytes); + } + int length = setBytesX(pos, bytes, 0, bytes.length); + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceExit(this, "setBytes", length); + } + return length; + } + } + + public int setBytes(long pos, byte[] bytes, int offset, int len) throws SqlException { + synchronized (agent_.connection_) { + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceEntry(this, "setBytes", (int) pos, bytes, offset, len); + } + int length = setBytesX(pos, bytes, offset, len); + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceExit(this, "setBytes", length); + } + return length; + } + } + + public int setBytesX(long pos, byte[] bytes, int offset, int len) throws SqlException { + int length = 0; + if ((int) pos <= 0 || pos > binaryString_.length - dataOffset_) { + throw new SqlException(agent_.logWriter_, "Invalid position " + pos + + " , offset " + offset + " or length " + len); + } + if ((offset < 0) || offset > bytes.length || len < 0) { + throw new SqlException(agent_.logWriter_, "Invalid position " + pos + + " , offset " + offset + " or length " + len); + } + if (len == 0) { + return 0; + } + length = Math.min((bytes.length - offset), len); + if ((binaryString_.length - dataOffset_ - (int) pos + 1) < length) { + byte newbuf[] = new byte[(int) pos + length + dataOffset_ - 1]; + System.arraycopy(binaryString_, 0, newbuf, 0, binaryString_.length); + binaryString_ = newbuf; + } - public int setBytesX (long pos, byte[] bytes, int offset, int len) throws SqlException - { - int length = 0; - if ( (int) pos <= 0 || pos > binaryString_.length - dataOffset_) - throw new SqlException(agent_.logWriter_, "Invalid position " + pos - + " , offset " + offset + " or length " + len); - if ( (offset < 0) || offset > bytes.length || len < 0 ) - throw new SqlException(agent_.logWriter_, "Invalid position " + pos - + " , offset " + offset + " or length " + len); - if (len == 0) - return 0; - length = Math.min((bytes.length - offset ),len); - if ((binaryString_.length - dataOffset_ - (int) pos + 1) < length) { - byte newbuf[] = new byte[(int)pos + length + dataOffset_-1]; - System.arraycopy(binaryString_,0,newbuf,0,binaryString_.length); - binaryString_ = newbuf; - } - - System.arraycopy(bytes,offset, binaryString_, (int)pos + dataOffset_ -1, length); - binaryStream_ = new java.io.ByteArrayInputStream(binaryString_); - sqlLength_ = binaryString_.length - dataOffset_; - return length; - } - - public java.io.OutputStream setBinaryStream (long pos) throws SqlException - { - synchronized (agent_.connection_) { - if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, "setBinaryStream", (int) pos); - BlobOutputStream outStream = new BlobOutputStream (this,pos); - - if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, "setBinaryStream", outStream); - return outStream; - } - } - - public void truncate (long len) throws SqlException - { - synchronized (agent_.connection_) { - if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, " truncate", (int) len); - if (len < 0 || len > this.length()) - throw new SqlException (agent_.logWriter_, "Invalid length " + len); - if (len == this.length()) - return; - long newLength = (int)len + dataOffset_; - byte newbuf[] = new byte[(int)len + dataOffset_]; - System.arraycopy(binaryString_,0,newbuf,0,(int)newLength); - binaryString_ = newbuf; + System.arraycopy(bytes, offset, binaryString_, (int) pos + dataOffset_ - 1, length); binaryStream_ = new java.io.ByteArrayInputStream(binaryString_); sqlLength_ = binaryString_.length - dataOffset_; - } + return length; } - //------------------ Material layer event callback methods ------------------- + public java.io.OutputStream setBinaryStream(long pos) throws SqlException { + synchronized (agent_.connection_) { + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceEntry(this, "setBinaryStream", (int) pos); + } + BlobOutputStream outStream = new BlobOutputStream(this, pos); + + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceExit(this, "setBinaryStream", outStream); + } + return outStream; + } + } + + public void truncate(long len) throws SqlException { + synchronized (agent_.connection_) { + if (agent_.loggingEnabled()) { + agent_.logWriter_.traceEntry(this, " truncate", (int) len); + } + if (len < 0 || len > this.length()) { + throw new SqlException(agent_.logWriter_, "Invalid length " + len); + } + if (len == this.length()) { + return; + } + long newLength = (int) len + dataOffset_; + byte newbuf[] = new byte[(int) len + dataOffset_]; + System.arraycopy(binaryString_, 0, newbuf, 0, (int) newLength); + binaryString_ = newbuf; + binaryStream_ = new java.io.ByteArrayInputStream(binaryString_); + sqlLength_ = binaryString_.length - dataOffset_; + } + } + + //------------------ Material layer event callback methods ------------------- + + //---------------------------- helper methods -------------------------------- + public boolean isBinaryString() { + return ((dataType_ & BINARY_STRING) == BINARY_STRING); + } + + public boolean isBinaryStream() { + return ((dataType_ & BINARY_STREAM) == BINARY_STREAM); + } + + public byte[] getBinaryString() { + return binaryString_; + } + + protected long binaryStringPosition(byte[] pattern, long start) { + // perform a local byte string search, starting at start + // check that the range of comparison is valid + int index = (int) start + dataOffset_ - 1; // api start begins at 1 + + while (index + pattern.length <= binaryString_.length) { + if (isSubString(pattern, index)) { + return (long) (index - dataOffset_ + 1); // readjust for api indexing + } + index++; + } + return -1L; // not found + } + + // precondition: binaryString_ is long enough for the comparison + protected boolean isSubString(byte[] pattern, int index) { + for (int i = 0; i < pattern.length; i++, index++) { + if (pattern[i] != binaryString_[index]) { + return false; + } + } - //---------------------------- helper methods -------------------------------- - public boolean isBinaryString () - { - return ((dataType_ & BINARY_STRING) == BINARY_STRING); - } - - public boolean isBinaryStream () - { - return ((dataType_ & BINARY_STREAM) == BINARY_STREAM); - } - - public byte[] getBinaryString () - { - return binaryString_; - } - - protected long binaryStringPosition (byte[] pattern, long start) - { - // perform a local byte string search, starting at start - // check that the range of comparison is valid - int index = (int) start + dataOffset_ - 1; // api start begins at 1 - - while (index + pattern.length <= binaryString_.length) { - if (isSubString (pattern, index)) - return (long)(index - dataOffset_ + 1); // readjust for api indexing - index++; - } - return -1L; // not found - } - - // precondition: binaryString_ is long enough for the comparison - protected boolean isSubString (byte[] pattern, int index) - { - for (int i = 0; i < pattern.length; i++, index++) - if (pattern[i] != binaryString_[index]) - return false; - - return true; - } + return true; + } }
infrastructure at apache.org | ViewVC Help |
Powered by ViewVC 1.1.26 |