📄 serialclob.java
字号:
/* * @(#)SerialClob.java 1.17 07/06/25 * * 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.*;/** * A serialized mapping in the Java programming language of an SQL * <code>CLOB</code> value. * <P> * The <code>SerialClob</code> class provides a constructor for creating * an instance from a <code>Clob</code> object. Note that the <code>Clob</code> * object should have brought the SQL <code>CLOB</code> value's data over * to the client before a <code>SerialClob</code> object * is constructed from it. The data of an SQL <code>CLOB</code> value can * be materialized on the client as a stream of Unicode characters. * <P> * <code>SerialClob</code> methods make it possible to get a substring * from a <code>SerialClob</code> object or to locate the start of * a pattern of characters. * * @author Jonathan Bruce */public class SerialClob implements Clob, Serializable, Cloneable { /** * A serialized array of characters containing the data of the SQL * <code>CLOB</code> value that this <code>SerialClob</code> object * represents. * * @serial */ private char buf[]; /** * Internal Clob representation if SerialClob is intialized with a * Clob */ private Clob clob; /** * The length in characters of this <code>SerialClob</code> object's * internal array of characters. * * @serial */ private long len; /** * The original length in characters of tgus <code>SerialClob</code> * objects internal array of characters. * * @serial */ private long origLen; /** * Constructs a <code>SerialClob</code> object that is a serialized version of * the given <code>char</code> array. * <p> * The new <code>SerialClob</code> object is initialized with the data from the * <code>char</code> array, thus allowing disconnected <code>RowSet</code> * objects to establish a serialized <code>Clob</code> object without touching * the data source. * * @param ch the char array representing the <code>Clob</code> object to be * serialized * @throws SerialException if an error occurs during serialization * @throws SQLException if a SQL error occurs */ public SerialClob(char ch[]) throws SerialException, SQLException { // %%% JMB. Agreed. Add code here to throw a SQLException if no // support is available for locatorsUpdateCopy=false // Serializing locators is not supported. len = ch.length; buf = new char[(int)len]; for (int i = 0; i < len ; i++){ buf[i] = ch[i]; } origLen = len; } /** * Constructs a <code>SerialClob</code> object that is a serialized * version of the given <code>Clob</code> object. * <P> * The new <code>SerialClob</code> object is initialized with the * data from the <code>Clob</code> object; therefore, the * <code>Clob</code> object should have previously brought the * SQL <code>CLOB</code> value's data over to the client from * the database. Otherwise, the new <code>SerialClob</code> object * object will contain no data. * <p> * Note: The <code>Clob</code> object supplied to this constructor cannot * return <code>null</code> for the <code>Clob.getCharacterStream()</code> * and <code>Clob.getAsciiStream</code> methods. This <code>SerialClob</code> * constructor cannot serialize a <code>Clob</code> object in this instance * and will throw an <code>SQLException</code> object. * * @param clob the <code>Clob</code> object from which this * <code>SerialClob</code> object is to be constructed; cannot be null * @throws SerialException if an error occurs during serialization * @throws SQLException if a SQL error occurs in capturing the CLOB; * if the <code>Clob</code> object is a null; or if both the * <code>Clob.getCharacterStream()</code> and <code>Clob.getAsciiStream()</code> * methods on the <code>Clob</code> return a null * @see java.sql.Clob */ public SerialClob(Clob clob) throws SerialException, SQLException { if (clob == null) { throw new SQLException("Cannot instantiate a SerialClob " + "object with a null Clob object"); } len = clob.length(); this.clob = clob; buf = new char[(int)len]; int read = 0; int offset = 0; BufferedReader reader; if ( (((reader = new BufferedReader(clob.getCharacterStream())) == null)) && (clob.getAsciiStream() == null)) { throw new SQLException("Invalid Clob object. Calls to getCharacterStream " + "and getAsciiStream return null which cannot be serialized."); } try { do { read = reader.read(buf, offset, (int)(len - offset)); offset += read; } while (read > 0); } catch (java.io.IOException ex) { throw new SerialException("SerialClob: " + ex.getMessage()); } origLen = len; } /** * Retrieves the number of characters in this <code>SerialClob</code> * object's array of characters. * * @return a <code>long</code> indicating the length in characters of this * <code>SerialClob</code> object's array of character * @throws SerialException if an error occurs */ public long length() throws SerialException { return len; } /** * Returns this <code>SerialClob</code> object's data as a stream * of Unicode characters. Unlike the related method, <code>getAsciiStream</code>, * a stream is produced regardless of whether the <code>SerialClob</code> object * was created with a <code>Clob</code> object or a <code>char</code> array. * * @return a <code>java.io.Reader</code> object containing this * <code>SerialClob</code> object's data * @throws SerialException if an error occurs */ public java.io.Reader getCharacterStream() throws SerialException { return (java.io.Reader) new CharArrayReader(buf); } /** * Retrieves the <code>CLOB</code> value designated by this <code>SerialClob</code> * object as an ascii stream. This method forwards the <code>getAsciiStream</code> * call to the underlying <code>Clob</code> object in the event that this * <code>SerialClob</code> object is instantiated with a <code>Clob</code> * object. If this <code>SerialClob</code> object is instantiated with * a <code>char</code> array, a <code>SerialException</code> object is thrown. * * @return a <code>java.io.InputStream</code> object containing * this <code>SerialClob</code> object's data * @throws SerialException if this <code>SerialClob</code> object was not instantiated * with a <code>Clob</code> object * @throws SQLException if there is an error accessing the * <code>CLOB</code> value represented by the <code>Clob</code> object that was * used to create this <code>SerialClob</code> object */ public java.io.InputStream getAsciiStream() throws SerialException, SQLException { if (this.clob != null) { return this.clob.getAsciiStream(); } else { throw new SerialException("Unsupported operation. SerialClob cannot " + "return a the CLOB value as an ascii stream, unless instantiated " + "with a fully implemented Clob object."); } } /** * Returns a copy of the substring contained in this * <code>SerialClob</code> object, starting at the given position * and continuing for the specified number or characters. * * @param pos the position of the first character in the substring * to be copied; the first character of the * <code>SerialClob</code> object is at position * <code>1</code>; must not be less than <code>1</code>, * and the sum of the starting position and the length * of the substring must be less than the length of this * <code>SerialClob</code> object * @param length the number of characters in the substring to be * returned; must not be greater than the length of * this <code>SerialClob</code> object, and the * sum of the starting position and the length * of the substring must be less than the length of this * <code>SerialClob</code> object * @return a <code>String</code> object containing a substring of * this <code>SerialClob</code> object beginning at the * given position and containing the specified number of * consecutive characters * @throws SerialException if either of the arguments is out of bounds */ public String getSubString(long pos, int length) throws SerialException { if (pos < 1 || pos > this.length()) { throw new SerialException("Invalid position in BLOB object set"); } if ((pos-1) + length > this.length()) { throw new SerialException("Invalid position and substring length"); } try { return new String(buf, (int)pos - 1, length); } catch (StringIndexOutOfBoundsException e) { throw new SerialException("StringIndexOutOfBoundsException: " + e.getMessage()); } } /** * Returns the position in this <code>SerialClob</code> object * where the given <code>String</code> object begins, starting * the search at the specified position. This method returns
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -