History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/javax/sql/rowset/serial/SerialClob.java (1 of 18352 files). Show all

Unified · Split

java/pkg/javax/sql/rowset/serial/SerialClob.java+522−0
@@ -0,0 +1,522 @@
1+/*
2+ * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package javax.sql.rowset.serial;
27+
28+import java.sql.*;
29+import java.io.*;
30+
31+/**
32+ * A serialized mapping in the Java programming language of an SQL
33+ * <code>CLOB</code> value.
34+ * <P>
35+ * The <code>SerialClob</code> class provides a constructor for creating
36+ * an instance from a <code>Clob</code> object. Note that the <code>Clob</code>
37+ * object should have brought the SQL <code>CLOB</code> value's data over
38+ * to the client before a <code>SerialClob</code> object
39+ * is constructed from it. The data of an SQL <code>CLOB</code> value can
40+ * be materialized on the client as a stream of Unicode characters.
41+ * <P>
42+ * <code>SerialClob</code> methods make it possible to get a substring
43+ * from a <code>SerialClob</code> object or to locate the start of
44+ * a pattern of characters.
45+ *
46+ * @author Jonathan Bruce
47+ */
48+public class SerialClob implements Clob, Serializable, Cloneable {
49+
50+ /**
51+ * A serialized array of characters containing the data of the SQL
52+ * <code>CLOB</code> value that this <code>SerialClob</code> object
53+ * represents.
54+ *
55+ * @serial
56+ */
57+ private char buf[];
58+
59+ /**
60+ * Internal Clob representation if SerialClob is initialized with a
61+ * Clob. Null if SerialClob is initialized with a char[].
62+ */
63+ private final Clob clob;
64+
65+ /**
66+ * The length in characters of this <code>SerialClob</code> object's
67+ * internal array of characters.
68+ *
69+ * @serial
70+ */
71+ private long len;
72+
73+ /**
74+ * The original length in characters of this <code>SerialClob</code>
75+ * object's internal array of characters.
76+ *
77+ * @serial
78+ */
79+ private final long origLen;
80+
81+ /**
82+ * Constructs a <code>SerialClob</code> object that is a serialized version of
83+ * the given <code>char</code> array.
84+ * <p>
85+ * The new <code>SerialClob</code> object is initialized with the data from the
86+ * <code>char</code> array, thus allowing disconnected <code>RowSet</code>
87+ * objects to establish a serialized <code>Clob</code> object without touching
88+ * the data source.
89+ *
90+ * @param ch the char array representing the <code>Clob</code> object to be
91+ * serialized
92+ * @throws SerialException if an error occurs during serialization
93+ * @throws SQLException if a SQL error occurs
94+ */
95+ public SerialClob(char ch[]) throws SerialException, SQLException {
96+
97+ // %%% JMB. Agreed. Add code here to throw a SQLException if no
98+ // support is available for locatorsUpdateCopy=false
99+ // Serializing locators is not supported.
100+
101+ len = ch.length;
102+ buf = new char[(int)len];
103+ for (int i = 0; i < len ; i++){
104+ buf[i] = ch[i];
105+ }
106+ origLen = len;
107+ clob = null;
108+ }
109+
110+ /**
111+ * Constructs a <code>SerialClob</code> object that is a serialized
112+ * version of the given <code>Clob</code> object.
113+ * <P>
114+ * The new <code>SerialClob</code> object is initialized with the
115+ * data from the <code>Clob</code> object; therefore, the
116+ * <code>Clob</code> object should have previously brought the
117+ * SQL <code>CLOB</code> value's data over to the client from
118+ * the database. Otherwise, the new <code>SerialClob</code> object
119+ * object will contain no data.
120+ * <p>
121+ * Note: The <code>Clob</code> object supplied to this constructor must
122+ * return non-null for both the <code>Clob.getCharacterStream()</code>
123+ * and <code>Clob.getAsciiStream</code> methods. This <code>SerialClob</code>
124+ * constructor cannot serialize a <code>Clob</code> object in this instance
125+ * and will throw an <code>SQLException</code> object.
126+ *
127+ * @param clob the <code>Clob</code> object from which this
128+ * <code>SerialClob</code> object is to be constructed; cannot be null
129+ * @throws SerialException if an error occurs during serialization
130+ * @throws SQLException if a SQL error occurs in capturing the CLOB;
131+ * if the <code>Clob</code> object is a null; or if either of the
132+ * <code>Clob.getCharacterStream()</code> and <code>Clob.getAsciiStream()</code>
133+ * methods on the <code>Clob</code> returns a null
134+ * @see java.sql.Clob
135+ */
136+ public SerialClob(Clob clob) throws SerialException, SQLException {
137+
138+ if (clob == null) {
139+ throw new SQLException("Cannot instantiate a SerialClob " +
140+ "object with a null Clob object");
141+ }
142+ len = clob.length();
143+ this.clob = clob;
144+ buf = new char[(int)len];
145+ int read = 0;
146+ int offset = 0;
147+
148+ try (Reader charStream = clob.getCharacterStream()) {
149+ if (charStream == null) {
150+ throw new SQLException("Invalid Clob object. The call to getCharacterStream " +
151+ "returned null which cannot be serialized.");
152+ }
153+
154+ // Note: get an ASCII stream in order to null-check it,
155+ // even though we don't do anything with it.
156+ try (InputStream asciiStream = clob.getAsciiStream()) {
157+ if (asciiStream == null) {
158+ throw new SQLException("Invalid Clob object. The call to getAsciiStream " +
159+ "returned null which cannot be serialized.");
160+ }
161+ }
162+
163+ try (Reader reader = new BufferedReader(charStream)) {
164+ do {
165+ read = reader.read(buf, offset, (int)(len - offset));
166+ offset += read;
167+ } while (read > 0);
168+ }
169+ } catch (java.io.IOException ex) {
170+ throw new SerialException("SerialClob: " + ex.getMessage());
171+ }
172+
173+ origLen = len;
174+ }
175+
176+ /**
177+ * Retrieves the number of characters in this <code>SerialClob</code>
178+ * object's array of characters.
179+ *
180+ * @return a <code>long</code> indicating the length in characters of this
181+ * <code>SerialClob</code> object's array of character
182+ * @throws SerialException if an error occurs
183+ */
184+ public long length() throws SerialException {
185+ return len;
186+ }
187+
188+ /**
189+ * Returns this <code>SerialClob</code> object's data as a stream
190+ * of Unicode characters. Unlike the related method, <code>getAsciiStream</code>,
191+ * a stream is produced regardless of whether the <code>SerialClob</code> object
192+ * was created with a <code>Clob</code> object or a <code>char</code> array.
193+ *
194+ * @return a <code>java.io.Reader</code> object containing this
195+ * <code>SerialClob</code> object's data
196+ * @throws SerialException if an error occurs
197+ */
198+ public java.io.Reader getCharacterStream() throws SerialException {
199+ return (java.io.Reader) new CharArrayReader(buf);
200+ }
201+
202+ /**
203+ * Retrieves the <code>CLOB</code> value designated by this <code>SerialClob</code>
204+ * object as an ascii stream. This method forwards the <code>getAsciiStream</code>
205+ * call to the underlying <code>Clob</code> object in the event that this
206+ * <code>SerialClob</code> object is instantiated with a <code>Clob</code>
207+ * object. If this <code>SerialClob</code> object is instantiated with
208+ * a <code>char</code> array, a <code>SerialException</code> object is thrown.
209+ *
210+ * @return a <code>java.io.InputStream</code> object containing
211+ * this <code>SerialClob</code> object's data
212+ * @throws SerialException if this <code>SerialClob</code> object was not instantiated
213+ * with a <code>Clob</code> object
214+ * @throws SQLException if there is an error accessing the
215+ * <code>CLOB</code> value represented by the <code>Clob</code> object that was
216+ * used to create this <code>SerialClob</code> object
217+ */
218+ public java.io.InputStream getAsciiStream() throws SerialException, SQLException {
219+ if (this.clob != null) {
220+ return this.clob.getAsciiStream();
221+ } else {
222+ throw new SerialException("Unsupported operation. SerialClob cannot " +
223+ "return a the CLOB value as an ascii stream, unless instantiated " +
224+ "with a fully implemented Clob object.");
225+ }
226+ }
227+
228+ /**
229+ * Returns a copy of the substring contained in this
230+ * <code>SerialClob</code> object, starting at the given position
231+ * and continuing for the specified number or characters.
232+ *
233+ * @param pos the position of the first character in the substring
234+ * to be copied; the first character of the
235+ * <code>SerialClob</code> object is at position
236+ * <code>1</code>; must not be less than <code>1</code>,
237+ * and the sum of the starting position and the length
238+ * of the substring must be less than the length of this
239+ * <code>SerialClob</code> object
240+ * @param length the number of characters in the substring to be
241+ * returned; must not be greater than the length of
242+ * this <code>SerialClob</code> object, and the
243+ * sum of the starting position and the length
244+ * of the substring must be less than the length of this
245+ * <code>SerialClob</code> object
246+ * @return a <code>String</code> object containing a substring of
247+ * this <code>SerialClob</code> object beginning at the
248+ * given position and containing the specified number of
249+ * consecutive characters
250+ * @throws SerialException if either of the arguments is out of bounds
251+ */
252+ public String getSubString(long pos, int length) throws SerialException {
253+
254+ if (pos < 1 || pos > this.length()) {
255+ throw new SerialException("Invalid position in BLOB object set");
256+ }
257+
258+ if ((pos-1) + length > this.length()) {
259+ throw new SerialException("Invalid position and substring length");
260+ }
261+
262+ try {
263+ return new String(buf, (int)pos - 1, length);
264+
265+ } catch (StringIndexOutOfBoundsException e) {
266+ throw new SerialException("StringIndexOutOfBoundsException: " +
267+ e.getMessage());
268+ }
269+
270+ }
271+
272+ /**
273+ * Returns the position in this <code>SerialClob</code> object
274+ * where the given <code>String</code> object begins, starting
275+ * the search at the specified position. This method returns
276+ * <code>-1</code> if the pattern is not found.
277+ *
278+ * @param searchStr the <code>String</code> object for which to
279+ * search
280+ * @param start the position in this <code>SerialClob</code> object
281+ * at which to start the search; the first position is
282+ * <code>1</code>; must not be less than <code>1</code> nor
283+ * greater than the length of this <code>SerialClob</code> object
284+ * @return the position at which the given <code>String</code> object
285+ * begins, starting the search at the specified position;
286+ * <code>-1</code> if the given <code>String</code> object is
287+ * not found or the starting position is out of bounds; position
288+ * numbering for the return value starts at <code>1</code>
289+ * @throws SerialException if an error occurs locating the String signature
290+ * @throws SQLException if there is an error accessing the Blob value
291+ * from the database.
292+ */
293+ public long position(String searchStr, long start)
294+ throws SerialException, SQLException {
295+
296+ if (start < 1 || start > len) {
297+ return -1;
298+ }
299+
300+ char pattern[] = searchStr.toCharArray();
301+
302+ int pos = (int)start-1;
303+ int i = 0;
304+ long patlen = pattern.length;
305+
306+ while (pos < len) {
307+ if (pattern[i] == buf[pos]) {
308+ if (i + 1 == patlen) {
309+ return (pos + 1) - (patlen - 1);
310+ }
311+ i++; pos++; // increment pos, and i
312+
313+ } else if (pattern[i] != buf[pos]) {
314+ pos++; // increment pos only
315+ }
316+ }
317+ return -1; // not found
318+ }
319+
320+ /**
321+ * Returns the position in this <code>SerialClob</code> object
322+ * where the given <code>Clob</code> signature begins, starting
323+ * the search at the specified position. This method returns
324+ * <code>-1</code> if the pattern is not found.
325+ *
326+ * @param searchStr the <code>Clob</code> object for which to search
327+ * @param start the position in this <code>SerialClob</code> object
328+ * at which to begin the search; the first position is
329+ * <code>1</code>; must not be less than <code>1</code> nor
330+ * greater than the length of this <code>SerialClob</code> object
331+ * @return the position at which the given <code>Clob</code>
332+ * object begins in this <code>SerialClob</code> object,
333+ * at or after the specified starting position
334+ * @throws SerialException if an error occurs locating the Clob signature
335+ * @throws SQLException if there is an error accessing the Blob value
336+ * from the database
337+ */
338+ public long position(Clob searchStr, long start)
339+ throws SerialException, SQLException {
340+
341+ return position(searchStr.getSubString(1,(int)searchStr.length()), start);
342+ }
343+
344+ /**
345+ * Writes the given Java <code>String</code> to the <code>CLOB</code>
346+ * value that this <code>SerialClob</code> object represents, at the position
347+ * <code>pos</code>.
348+ *
349+ * @param pos the position at which to start writing to the <code>CLOB</code>
350+ * value that this <code>SerialClob</code> object represents; the first
351+ * position is <code>1</code>; must not be less than <code>1</code> nor
352+ * greater than the length of this <code>SerialClob</code> object
353+ * @param str the string to be written to the <code>CLOB</code>
354+ * value that this <code>SerialClob</code> object represents
355+ * @return the number of characters written
356+ * @throws SerialException if there is an error accessing the
357+ * <code>CLOB</code> value; if an invalid position is set; if an
358+ * invalid offset value is set; if number of bytes to be written
359+ * is greater than the <code>SerialClob</code> length; or the combined
360+ * values of the length and offset is greater than the Clob buffer
361+ */
362+ public int setString(long pos, String str) throws SerialException {
363+ return (setString(pos, str, 0, str.length()));
364+ }
365+
366+ /**
367+ * Writes <code>len</code> characters of <code>str</code>, starting
368+ * at character <code>offset</code>, to the <code>CLOB</code> value
369+ * that this <code>Clob</code> represents.
370+ *
371+ * @param pos the position at which to start writing to the <code>CLOB</code>
372+ * value that this <code>SerialClob</code> object represents; the first
373+ * position is <code>1</code>; must not be less than <code>1</code> nor
374+ * greater than the length of this <code>SerialClob</code> object
375+ * @param str the string to be written to the <code>CLOB</code>
376+ * value that this <code>Clob</code> object represents
377+ * @param offset the offset into <code>str</code> to start reading
378+ * the characters to be written
379+ * @param length the number of characters to be written
380+ * @return the number of characters written
381+ * @throws SerialException if there is an error accessing the
382+ * <code>CLOB</code> value; if an invalid position is set; if an
383+ * invalid offset value is set; if number of bytes to be written
384+ * is greater than the <code>SerialClob</code> length; or the combined
385+ * values of the length and offset is greater than the Clob buffer
386+ */
387+ public int setString(long pos, String str, int offset, int length)
388+ throws SerialException {
389+ String temp = str.substring(offset);
390+ char cPattern[] = temp.toCharArray();
391+
392+ if (offset < 0 || offset > str.length()) {
393+ throw new SerialException("Invalid offset in byte array set");
394+ }
395+
396+ if (pos < 1 || pos > this.length()) {
397+ throw new SerialException("Invalid position in BLOB object set");
398+ }
399+
400+ if ((long)(length) > origLen) {
401+ throw new SerialException("Buffer is not sufficient to hold the value");
402+ }
403+
404+ if ((length + offset) > str.length()) {
405+ // need check to ensure length + offset !> bytes.length
406+ throw new SerialException("Invalid OffSet. Cannot have combined offset " +
407+ " and length that is greater that the Blob buffer");
408+ }
409+
410+ int i = 0;
411+ pos--; //values in the array are at position one less
412+ while ( i < length || (offset + i +1) < (str.length() - offset ) ) {
413+ this.buf[(int)pos + i ] = cPattern[offset + i ];
414+ i++;
415+ }
416+ return i;
417+ }
418+
419+ /**
420+ * Retrieves a stream to be used to write Ascii characters to the
421+ * <code>CLOB</code> value that this <code>SerialClob</code> object represents,
422+ * starting at position <code>pos</code>. This method forwards the
423+ * <code>setAsciiStream()</code> call to the underlying <code>Clob</code> object in
424+ * the event that this <code>SerialClob</code> object is instantiated with a
425+ * <code>Clob</code> object. If this <code>SerialClob</code> object is instantiated
426+ * with a <code>char</code> array, a <code>SerialException</code> object is thrown.
427+ *
428+ * @param pos the position at which to start writing to the
429+ * <code>CLOB</code> object
430+ * @return the stream to which ASCII encoded characters can be written
431+ * @throws SerialException if SerialClob is not instantiated with a
432+ * Clob object that supports <code>setAsciiStream</code>
433+ * @throws SQLException if there is an error accessing the
434+ * <code>CLOB</code> value
435+ * @see #getAsciiStream
436+ */
437+ public java.io.OutputStream setAsciiStream(long pos)
438+ throws SerialException, SQLException {
439+ if (this.clob.setAsciiStream(pos) != null) {
440+ return this.clob.setAsciiStream(pos);
441+ } else {
442+ throw new SerialException("Unsupported operation. SerialClob cannot " +
443+ "return a writable ascii stream\n unless instantiated with a Clob object " +
444+ "that has a setAsciiStream() implementation");
445+ }
446+ }
447+
448+ /**
449+ * Retrieves a stream to be used to write a stream of Unicode characters
450+ * to the <code>CLOB</code> value that this <code>SerialClob</code> object
451+ * represents, at position <code>pos</code>. This method forwards the
452+ * <code>setCharacterStream()</code> call to the underlying <code>Clob</code>
453+ * object in the event that this <code>SerialClob</code> object is instantiated with a
454+ * <code>Clob</code> object. If this <code>SerialClob</code> object is instantiated with
455+ * a <code>char</code> array, a <code>SerialException</code> is thrown.
456+ *
457+ * @param pos the position at which to start writing to the
458+ * <code>CLOB</code> value
459+ *
460+ * @return a stream to which Unicode encoded characters can be written
461+ * @throws SerialException if the SerialClob is not instantiated with
462+ * a Clob object that supports <code>setCharacterStream</code>
463+ * @throws SQLException if there is an error accessing the
464+ * <code>CLOB</code> value
465+ * @see #getCharacterStream
466+ */
467+ public java.io.Writer setCharacterStream(long pos)
468+ throws SerialException, SQLException {
469+ if (this.clob.setCharacterStream(pos) != null) {
470+ return this.clob.setCharacterStream(pos);
471+ } else {
472+ throw new SerialException("Unsupported operation. SerialClob cannot " +
473+ "return a writable character stream\n unless instantiated with a Clob object " +
474+ "that has a setCharacterStream implementation");
475+ }
476+ }
477+
478+ /**
479+ * Truncates the <code>CLOB</code> value that this <code>SerialClob</code>
480+ * object represents so that it has a length of <code>len</code>
481+ * characters.
482+ * <p>
483+ * Truncating a <code>SerialClob</code> object to length 0 has the effect of
484+ * clearing its contents.
485+ *
486+ * @param length the length, in bytes, to which the <code>CLOB</code>
487+ * value should be truncated
488+ * @throws SQLException if there is an error accessing the
489+ * <code>CLOB</code> value
490+ */
491+ public void truncate(long length) throws SerialException {
492+ if (length > len) {
493+ throw new SerialException
494+ ("Length more than what can be truncated");
495+ } else {
496+ len = length;
497+ // re-size the buffer
498+
499+ if (len == 0) {
500+ buf = new char[] {};
501+ } else {
502+ buf = (this.getSubString(1, (int)len)).toCharArray();
503+ }
504+
505+ }
506+ }
507+
508+
509+ public Reader getCharacterStream(long pos, long length) throws SQLException {
510+ throw new java.lang.UnsupportedOperationException("Not supported");
511+ }
512+
513+ public void free() throws SQLException {
514+ throw new java.lang.UnsupportedOperationException("Not supported");
515+ }
516+
517+ /**
518+ * The identifier that assists in the serialization of this <code>SerialClob</code>
519+ * object.
520+ */
521+ static final long serialVersionUID = -1662519690087375313L;
522+}