📄 serialblob.java
字号:
int i = 0; long patlen = pattern.length; while (pos < len) { if (pattern[i] == buf[pos]) { if (i + 1 == patlen) { return (pos + 1) - (patlen - 1); } i++; pos++; // increment pos, and i } else if (pattern[i] != buf[pos]) { pos++; // increment pos only } } return -1; // not found } /** * Returns the position in this <code>SerialBlob</code> object where * the given <code>Blob</code> object begins, starting the search at the * specified position. * * @param pattern the <code>Blob</code> object for which to search; * @param start the position of the byte in this * <code>SerialBlob</code> object from which to begin * the search; the first position is <code>1</code>; * must not be less than <code>1</code> nor greater than * the length of this <code>SerialBlob</code> object * @return the position in this <code>SerialBlob</code> object * where the given <code>Blob</code> object begins, starting * at the specified position; <code>-1</code> if the pattern is * not found or the given starting position is out of bounds; * position numbering for the return value starts at <code>1</code> * @throws SerialException if an error occurs when serializing the blob * @throws SQLException if there is an error accessing the <code>BLOB</code> * value from the database */ public long position(Blob pattern, long start) throws SerialException, SQLException { return position(pattern.getBytes(1, (int)(pattern.length())), start); } /** * Writes the given array of bytes to the <code>BLOB</code> value that * this <code>Blob</code> object represents, starting at position * <code>pos</code>, and returns the number of bytes written. * * @param pos the position in the SQL <code>BLOB</code> value at which * to start writing. The first position is <code>1</code>; * must not be less than <code>1</code> nor greater than * the length of this <code>SerialBlob</code> object. * @param bytes the array of bytes to be written to the <code>BLOB</code> * value that this <code>Blob</code> object represents * @return the number of bytes written * @throws SerialException if there is an error accessing the * <code>BLOB</code> value; or if an invalid position is set; if an * invalid offset value is set * @throws SQLException if there is an error accessing the <code>BLOB</code> * value from the database * @see #getBytes */ public int setBytes(long pos, byte[] bytes) throws SerialException, SQLException { return (setBytes(pos, bytes, 0, bytes.length)); } /** * Writes all or part of the given <code>byte</code> array to the * <code>BLOB</code> value that this <code>Blob</code> object represents * and returns the number of bytes written. * Writing starts at position <code>pos</code> in the <code>BLOB</code> * value; <i>len</i> bytes from the given byte array are written. * * @param pos the position in the <code>BLOB</code> object at which * to start writing. The first position is <code>1</code>; * must not be less than <code>1</code> nor greater than * the length of this <code>SerialBlob</code> object. * @param bytes the array of bytes to be written to the <code>BLOB</code> * value * @param offset the offset in the <code>byte</code> array at which * to start reading the bytes. The first offset position is * <code>0</code>; must not be less than <code>0</code> nor greater * than the length of the <code>byte</code> array * @param length the number of bytes to be written to the * <code>BLOB</code> value from the array of bytes <i>bytes</i>. * * @return the number of bytes written * @throws SerialException if there is an error accessing the * <code>BLOB</code> value; if an invalid position is set; if an * invalid offset value is set; if number of bytes to be written * is greater than the <code>SerialBlob</code> length; or the combined * values of the length and offset is greater than the Blob buffer * @throws SQLException if there is an error accessing the <code>BLOB</code> * value from the database. * @see #getBytes */ public int setBytes(long pos, byte[] bytes, int offset, int length) throws SerialException, SQLException { if (offset < 0 || offset > bytes.length) { throw new SerialException("Invalid offset in byte array set"); } if (pos < 1 || pos > this.length()) { throw new SerialException("Invalid position in BLOB object set"); } if ((long)(length) > origLen) { throw new SerialException("Buffer is not sufficient to hold the value"); } if ((length + offset) > bytes.length) { throw new SerialException("Invalid OffSet. Cannot have combined offset " + "and length that is greater that the Blob buffer"); } int i = 0; pos--; // correct to array indexing while ( i < length || (offset + i +1) < (bytes.length-offset) ) { this.buf[(int)pos + i] = bytes[offset + i ]; i++; } return i; } /** * Retrieves a stream that can be used to write to the <code>BLOB</code> * value that this <code>Blob</code> object represents. The stream begins * at position <code>pos</code>. This method forwards the * <code>setBinaryStream()</code> call to the underlying <code>Blob</code> in * the event that this <code>SerialBlob</code> object is instantiated with a * <code>Blob</code>. If this <code>SerialBlob</code> is instantiated with * a <code>byte</code> array, a <code>SerialException</code> is thrown. * * @param pos the position in the <code>BLOB</code> value at which * to start writing * @return a <code>java.io.OutputStream</code> object to which data can * be written * @throws SQLException if there is an error accessing the * <code>BLOB</code> value * @throws SerialException if the SerialBlob in not instantiated with a * <code>Blob</code> object that supports <code>setBinaryStream()</code> * @see #getBinaryStream */ public java.io.OutputStream setBinaryStream(long pos) throws SerialException, SQLException { if (this.blob.setBinaryStream(pos) != null) { return this.blob.setBinaryStream(pos); } else { throw new SerialException("Unsupported operation. SerialBlob cannot " + "return a writable binary stream, unless instantiated with a Blob object " + "that provides a setBinaryStream() implementation"); } } /** * Truncates the <code>BLOB</code> value that this <code>Blob</code> * object represents to be <code>len</code> bytes in length. * * @param length the length, in bytes, to which the <code>BLOB</code> * value that this <code>Blob</code> object represents should be * truncated * @throws SerialException if there is an error accessing the Blob value; * or the length to truncate is greater that the SerialBlob length */ public void truncate(long length) throws SerialException { if (length > len) { throw new SerialException ("Length more than what can be truncated"); } else if((int)length == 0) { buf = new byte[0]; len = length; } else { len = length; buf = this.getBytes(1, (int)len); } } /** * Returns an <code>InputStream</code> object that contains a partial <code>Blob</code> value, * starting with the byte specified by pos, which is length bytes in length. * * @param pos the offset to the first byte of the partial value to be retrieved. * The first byte in the <code>Blob</code> is at position 1 * @param length the length in bytes of the partial value to be retrieved * @return <code>InputStream</code> through which the partial <code>Blob</code> value can be read. * @throws SQLException if pos is less than 1 or if pos is greater than the number of bytes * in the <code>Blob</code> or if pos + length is greater than the number of bytes * in the <code>Blob</code> * * @since 1.6 */ public InputStream getBinaryStream(long pos,long length) throws SQLException { throw new java.lang.UnsupportedOperationException("Not supported"); } /** * This method frees the <code>Blob</code> object and releases the resources that it holds. * <code>Blob</code> object. The object is invalid once the <code>free</code> * method is called. If <code>free</code> is called multiple times, the subsequent * calls to <code>free</code> are treated as a no-op. * * @throws SQLException if an error occurs releasing * the Blob's resources * @since 1.6 */ public void free() throws SQLException { throw new java.lang.UnsupportedOperationException("Not supported"); } /** * The identifier that assists in the serialization of this <code>SerialBlob</code> * object. */ static final long serialVersionUID = -8144641928112860441L;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -