📄 serialblob.java
字号:
/* * @(#)SerialBlob.java 1.11 06/04/07 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.sql.rowset.serial;import java.sql.*;import java.io.*;import java.lang.reflect.*;/** * A serialized mapping in the Java programming language of an SQL * <code>BLOB</code> value. * <P> * The <code>SerialBlob</code> class provides a constructor for creating * an instance from a <code>Blob</code> object. Note that the * <code>Blob</code> * object should have brought the SQL <code>BLOB</code> value's data over * to the client before a <code>SerialBlob</code> object * is constructed from it. The data of an SQL <code>BLOB</code> value can * be materialized on the client as an array of bytes (using the method * <code>Blob.getBytes</code>) or as a stream of uninterpreted bytes * (using the method <code>Blob.getBinaryStream</code>). * <P> * <code>SerialBlob</code> methods make it possible to make a copy of a * <code>SerialBlob</code> object as an array of bytes or as a stream. * They also make it possible to locate a given pattern of bytes or a * <code>Blob</code> object within a <code>SerialBlob</code> object * and to update or truncate a <code>Blob</code> object. * * @author Jonathan Bruce */public class SerialBlob implements Blob, Serializable, Cloneable { /** * A serialized array of uninterpreted bytes representing the * value of this <code>SerialBlob</code> object. * @serial */ private byte buf[]; /** * The internal representation of the <code>Blob</code> object on which this * <code>SerialBlob</code> object is based. */ private Blob blob; /** * The number of bytes in this <code>SerialBlob</code> object's * array of bytes. * @serial */ private long len; /** * The orginal number of bytes in this <code>SerialBlob</code> object's * array of bytes when it was first established. * @serial */ private long origLen; /** * Constructs a <code>SerialBlob</code> object that is a serialized version of * the given <code>byte</code> array. * <p> * The new <code>SerialBlob</code> object is initialized with the data from the * <code>byte</code> array, thus allowing disconnected <code>RowSet</code> * objects to establish serialized <code>Blob</code> objects without * touching the data source. * * @param b the <code>byte</code> array containing the data for the * <code>Blob</code> object to be serialized * @throws SerialException if an error occurs during serialization * @throws SQLException if a SQL errors occurs */ public SerialBlob(byte[] b) throws SerialException, SQLException { len = b.length; buf = new byte[(int)len]; for(int i = 0; i < len; i++) { buf[i] = b[i]; } origLen = len; } /** * Constructs a <code>SerialBlob</code> object that is a serialized * version of the given <code>Blob</code> object. * <P> * The new <code>SerialBlob</code> object is initialized with the * data from the <code>Blob</code> object; therefore, the * <code>Blob</code> object should have previously brought the * SQL <code>BLOB</code> value's data over to the client from * the database. Otherwise, the new <code>SerialBlob</code> object * will contain no data. * * @param blob the <code>Blob</code> object from which this * <code>SerialBlob</code> object is to be constructed; * cannot be null. * @throws SerialException if an error occurs during serialization * @throws SQLException if the <code>Blob</code> passed to this * to this constructor is a <code>null</code>. * @see java.sql.Blob */ public SerialBlob (Blob blob) throws SerialException, SQLException { if (blob == null) { throw new SQLException("Cannot instantiate a SerialBlob " + "object with a null Blob object"); } len = blob.length(); buf = blob.getBytes(1, (int)len ); this.blob = blob; //if ( len < 10240000) // len = 10240000; origLen = len; } /** * Copies the specified number of bytes, starting at the given * position, from this <code>SerialBlob</code> object to * another array of bytes. * <P> * Note that if the given number of bytes to be copied is larger than * the length of this <code>SerialBlob</code> object's array of * bytes, the given number will be shortened to the array's length. * * @param pos the ordinal position of the first byte in this * <code>SerialBlob</code> object to be copied; * numbering starts at <code>1</code>; must not be less * than <code>1</code> and must be less than or equal * to the length of this <code>SerialBlob</code> object * @param length the number of bytes to be copied * @return an array of bytes that is a copy of a region of this * <code>SerialBlob</code> object, starting at the given * position and containing the given number of consecutive bytes * @throws SerialException if the given starting position is out of bounds */ public byte[] getBytes(long pos, int length) throws SerialException { if (length > len) { length = (int)len; } if (pos < 1 || length - pos < 0 ) { throw new SerialException("Invalid arguments: position cannot be less that 1"); } pos--; // correct pos to array index byte[] b = new byte[length]; for (int i = 0; i < length; i++) { b[i] = this.buf[(int)pos]; pos++; } return b; } /** * Retrieves the number of bytes in this <code>SerialBlob</code> * object's array of bytes. * * @return a <code>long</code> indicating the length in bytes of this * <code>SerialBlob</code> object's array of bytes * @throws SerialException if an error occurs */ public long length() throws SerialException { return len; } /** * Returns this <code>SerialBlob</code> object as an input stream. * Unlike the related method, <code>setBinaryStream</code>, * a stream is produced regardless of whether the <code>SerialBlob</code> * was created with a <code>Blob</code> object or a <code>byte</code> array. * * @return a <code>java.io.InputStream</code> object that contains * this <code>SerialBlob</code> object's array of bytes * @throws SerialException if an error occurs * @see #setBinaryStream */ public java.io.InputStream getBinaryStream() throws SerialException { InputStream stream = new ByteArrayInputStream(buf); return (java.io.InputStream)stream; } /** * Returns the position in this <code>SerialBlob</code> object where * the given pattern of bytes begins, starting the search at the * specified position. * * @param pattern the pattern of bytes 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 pattern 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(byte[] pattern, long start) throws SerialException, SQLException { if (start < 1 || start > len) { return -1; } int pos = (int)start-1; // internally Blobs are stored as arrays.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -