author | lancea |
Thu, 01 Nov 2012 17:35:17 -0400 | |
changeset 14337 | a003c6a54cb6 |
parent 10323 | 48d695d1e966 |
child 14781 | 701d0765f75f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14337
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.sql.rowset.serial; |
|
27 |
||
28 |
import java.sql.*; |
|
29 |
import java.io.*; |
|
14337
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
30 |
import java.util.Arrays; |
2 | 31 |
|
32 |
/** |
|
33 |
* A serialized mapping in the Java programming language of an SQL |
|
34 |
* <code>CLOB</code> value. |
|
35 |
* <P> |
|
36 |
* The <code>SerialClob</code> class provides a constructor for creating |
|
37 |
* an instance from a <code>Clob</code> object. Note that the <code>Clob</code> |
|
38 |
* object should have brought the SQL <code>CLOB</code> value's data over |
|
39 |
* to the client before a <code>SerialClob</code> object |
|
40 |
* is constructed from it. The data of an SQL <code>CLOB</code> value can |
|
41 |
* be materialized on the client as a stream of Unicode characters. |
|
42 |
* <P> |
|
43 |
* <code>SerialClob</code> methods make it possible to get a substring |
|
44 |
* from a <code>SerialClob</code> object or to locate the start of |
|
45 |
* a pattern of characters. |
|
46 |
* |
|
47 |
* @author Jonathan Bruce |
|
48 |
*/ |
|
49 |
public class SerialClob implements Clob, Serializable, Cloneable { |
|
50 |
||
51 |
/** |
|
52 |
* A serialized array of characters containing the data of the SQL |
|
53 |
* <code>CLOB</code> value that this <code>SerialClob</code> object |
|
54 |
* represents. |
|
55 |
* |
|
56 |
* @serial |
|
57 |
*/ |
|
58 |
private char buf[]; |
|
59 |
||
60 |
/** |
|
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
61 |
* Internal Clob representation if SerialClob is initialized with a |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
62 |
* Clob. Null if SerialClob is initialized with a char[]. |
2 | 63 |
*/ |
14337
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
64 |
private Clob clob; |
2 | 65 |
|
66 |
/** |
|
67 |
* The length in characters of this <code>SerialClob</code> object's |
|
68 |
* internal array of characters. |
|
69 |
* |
|
70 |
* @serial |
|
71 |
*/ |
|
72 |
private long len; |
|
73 |
||
74 |
/** |
|
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
75 |
* The original length in characters of this <code>SerialClob</code> |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
76 |
* object's internal array of characters. |
2 | 77 |
* |
78 |
* @serial |
|
79 |
*/ |
|
14337
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
80 |
private long origLen; |
2 | 81 |
|
82 |
/** |
|
83 |
* Constructs a <code>SerialClob</code> object that is a serialized version of |
|
84 |
* the given <code>char</code> array. |
|
85 |
* <p> |
|
86 |
* The new <code>SerialClob</code> object is initialized with the data from the |
|
87 |
* <code>char</code> array, thus allowing disconnected <code>RowSet</code> |
|
88 |
* objects to establish a serialized <code>Clob</code> object without touching |
|
89 |
* the data source. |
|
90 |
* |
|
91 |
* @param ch the char array representing the <code>Clob</code> object to be |
|
92 |
* serialized |
|
93 |
* @throws SerialException if an error occurs during serialization |
|
94 |
* @throws SQLException if a SQL error occurs |
|
95 |
*/ |
|
96 |
public SerialClob(char ch[]) throws SerialException, SQLException { |
|
97 |
||
98 |
// %%% JMB. Agreed. Add code here to throw a SQLException if no |
|
99 |
// support is available for locatorsUpdateCopy=false |
|
100 |
// Serializing locators is not supported. |
|
101 |
||
102 |
len = ch.length; |
|
103 |
buf = new char[(int)len]; |
|
104 |
for (int i = 0; i < len ; i++){ |
|
105 |
buf[i] = ch[i]; |
|
106 |
} |
|
107 |
origLen = len; |
|
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
108 |
clob = null; |
2 | 109 |
} |
110 |
||
111 |
/** |
|
112 |
* Constructs a <code>SerialClob</code> object that is a serialized |
|
113 |
* version of the given <code>Clob</code> object. |
|
114 |
* <P> |
|
115 |
* The new <code>SerialClob</code> object is initialized with the |
|
116 |
* data from the <code>Clob</code> object; therefore, the |
|
117 |
* <code>Clob</code> object should have previously brought the |
|
118 |
* SQL <code>CLOB</code> value's data over to the client from |
|
119 |
* the database. Otherwise, the new <code>SerialClob</code> object |
|
120 |
* object will contain no data. |
|
121 |
* <p> |
|
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
122 |
* Note: The <code>Clob</code> object supplied to this constructor must |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
123 |
* return non-null for both the <code>Clob.getCharacterStream()</code> |
2 | 124 |
* and <code>Clob.getAsciiStream</code> methods. This <code>SerialClob</code> |
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
125 |
* constructor cannot serialize a <code>Clob</code> object in this instance |
2 | 126 |
* and will throw an <code>SQLException</code> object. |
127 |
* |
|
128 |
* @param clob the <code>Clob</code> object from which this |
|
129 |
* <code>SerialClob</code> object is to be constructed; cannot be null |
|
130 |
* @throws SerialException if an error occurs during serialization |
|
131 |
* @throws SQLException if a SQL error occurs in capturing the CLOB; |
|
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
132 |
* if the <code>Clob</code> object is a null; or if either of the |
2 | 133 |
* <code>Clob.getCharacterStream()</code> and <code>Clob.getAsciiStream()</code> |
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
134 |
* methods on the <code>Clob</code> returns a null |
2 | 135 |
* @see java.sql.Clob |
136 |
*/ |
|
137 |
public SerialClob(Clob clob) throws SerialException, SQLException { |
|
138 |
||
139 |
if (clob == null) { |
|
140 |
throw new SQLException("Cannot instantiate a SerialClob " + |
|
141 |
"object with a null Clob object"); |
|
142 |
} |
|
143 |
len = clob.length(); |
|
144 |
this.clob = clob; |
|
145 |
buf = new char[(int)len]; |
|
146 |
int read = 0; |
|
147 |
int offset = 0; |
|
148 |
||
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
149 |
try (Reader charStream = clob.getCharacterStream()) { |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
150 |
if (charStream == null) { |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
151 |
throw new SQLException("Invalid Clob object. The call to getCharacterStream " + |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
152 |
"returned null which cannot be serialized."); |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
153 |
} |
2 | 154 |
|
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
155 |
// Note: get an ASCII stream in order to null-check it, |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
156 |
// even though we don't do anything with it. |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
157 |
try (InputStream asciiStream = clob.getAsciiStream()) { |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
158 |
if (asciiStream == null) { |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
159 |
throw new SQLException("Invalid Clob object. The call to getAsciiStream " + |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
160 |
"returned null which cannot be serialized."); |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
161 |
} |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
162 |
} |
2 | 163 |
|
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
164 |
try (Reader reader = new BufferedReader(charStream)) { |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
165 |
do { |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
166 |
read = reader.read(buf, offset, (int)(len - offset)); |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
167 |
offset += read; |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
168 |
} while (read > 0); |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
169 |
} |
2 | 170 |
} catch (java.io.IOException ex) { |
171 |
throw new SerialException("SerialClob: " + ex.getMessage()); |
|
172 |
} |
|
173 |
||
174 |
origLen = len; |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Retrieves the number of characters in this <code>SerialClob</code> |
|
179 |
* object's array of characters. |
|
180 |
* |
|
181 |
* @return a <code>long</code> indicating the length in characters of this |
|
182 |
* <code>SerialClob</code> object's array of character |
|
183 |
* @throws SerialException if an error occurs |
|
184 |
*/ |
|
185 |
public long length() throws SerialException { |
|
186 |
return len; |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Returns this <code>SerialClob</code> object's data as a stream |
|
191 |
* of Unicode characters. Unlike the related method, <code>getAsciiStream</code>, |
|
192 |
* a stream is produced regardless of whether the <code>SerialClob</code> object |
|
193 |
* was created with a <code>Clob</code> object or a <code>char</code> array. |
|
194 |
* |
|
195 |
* @return a <code>java.io.Reader</code> object containing this |
|
196 |
* <code>SerialClob</code> object's data |
|
197 |
* @throws SerialException if an error occurs |
|
198 |
*/ |
|
199 |
public java.io.Reader getCharacterStream() throws SerialException { |
|
200 |
return (java.io.Reader) new CharArrayReader(buf); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Retrieves the <code>CLOB</code> value designated by this <code>SerialClob</code> |
|
205 |
* object as an ascii stream. This method forwards the <code>getAsciiStream</code> |
|
206 |
* call to the underlying <code>Clob</code> object in the event that this |
|
207 |
* <code>SerialClob</code> object is instantiated with a <code>Clob</code> |
|
208 |
* object. If this <code>SerialClob</code> object is instantiated with |
|
209 |
* a <code>char</code> array, a <code>SerialException</code> object is thrown. |
|
210 |
* |
|
211 |
* @return a <code>java.io.InputStream</code> object containing |
|
212 |
* this <code>SerialClob</code> object's data |
|
213 |
* @throws SerialException if this <code>SerialClob</code> object was not instantiated |
|
214 |
* with a <code>Clob</code> object |
|
215 |
* @throws SQLException if there is an error accessing the |
|
216 |
* <code>CLOB</code> value represented by the <code>Clob</code> object that was |
|
217 |
* used to create this <code>SerialClob</code> object |
|
218 |
*/ |
|
219 |
public java.io.InputStream getAsciiStream() throws SerialException, SQLException { |
|
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
220 |
if (this.clob != null) { |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
221 |
return this.clob.getAsciiStream(); |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
222 |
} else { |
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
223 |
throw new SerialException("Unsupported operation. SerialClob cannot " + |
2 | 224 |
"return a the CLOB value as an ascii stream, unless instantiated " + |
225 |
"with a fully implemented Clob object."); |
|
8405
bafd7b441d80
7018385: update javax.sql classes to use try-with-resources
smarks
parents:
5506
diff
changeset
|
226 |
} |
2 | 227 |
} |
228 |
||
229 |
/** |
|
230 |
* Returns a copy of the substring contained in this |
|
231 |
* <code>SerialClob</code> object, starting at the given position |
|
232 |
* and continuing for the specified number or characters. |
|
233 |
* |
|
234 |
* @param pos the position of the first character in the substring |
|
235 |
* to be copied; the first character of the |
|
236 |
* <code>SerialClob</code> object is at position |
|
237 |
* <code>1</code>; must not be less than <code>1</code>, |
|
238 |
* and the sum of the starting position and the length |
|
239 |
* of the substring must be less than the length of this |
|
240 |
* <code>SerialClob</code> object |
|
241 |
* @param length the number of characters in the substring to be |
|
242 |
* returned; must not be greater than the length of |
|
243 |
* this <code>SerialClob</code> object, and the |
|
244 |
* sum of the starting position and the length |
|
245 |
* of the substring must be less than the length of this |
|
246 |
* <code>SerialClob</code> object |
|
247 |
* @return a <code>String</code> object containing a substring of |
|
248 |
* this <code>SerialClob</code> object beginning at the |
|
249 |
* given position and containing the specified number of |
|
250 |
* consecutive characters |
|
251 |
* @throws SerialException if either of the arguments is out of bounds |
|
252 |
*/ |
|
253 |
public String getSubString(long pos, int length) throws SerialException { |
|
254 |
||
255 |
if (pos < 1 || pos > this.length()) { |
|
256 |
throw new SerialException("Invalid position in BLOB object set"); |
|
257 |
} |
|
258 |
||
259 |
if ((pos-1) + length > this.length()) { |
|
260 |
throw new SerialException("Invalid position and substring length"); |
|
261 |
} |
|
262 |
||
263 |
try { |
|
264 |
return new String(buf, (int)pos - 1, length); |
|
265 |
||
266 |
} catch (StringIndexOutOfBoundsException e) { |
|
267 |
throw new SerialException("StringIndexOutOfBoundsException: " + |
|
268 |
e.getMessage()); |
|
269 |
} |
|
270 |
||
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Returns the position in this <code>SerialClob</code> object |
|
275 |
* where the given <code>String</code> object begins, starting |
|
276 |
* the search at the specified position. This method returns |
|
277 |
* <code>-1</code> if the pattern is not found. |
|
278 |
* |
|
279 |
* @param searchStr the <code>String</code> object for which to |
|
280 |
* search |
|
281 |
* @param start the position in this <code>SerialClob</code> object |
|
282 |
* at which to start the search; the first position is |
|
283 |
* <code>1</code>; must not be less than <code>1</code> nor |
|
284 |
* greater than the length of this <code>SerialClob</code> object |
|
285 |
* @return the position at which the given <code>String</code> object |
|
286 |
* begins, starting the search at the specified position; |
|
287 |
* <code>-1</code> if the given <code>String</code> object is |
|
288 |
* not found or the starting position is out of bounds; position |
|
289 |
* numbering for the return value starts at <code>1</code> |
|
290 |
* @throws SerialException if an error occurs locating the String signature |
|
291 |
* @throws SQLException if there is an error accessing the Blob value |
|
292 |
* from the database. |
|
293 |
*/ |
|
294 |
public long position(String searchStr, long start) |
|
295 |
throws SerialException, SQLException { |
|
296 |
||
297 |
if (start < 1 || start > len) { |
|
298 |
return -1; |
|
299 |
} |
|
300 |
||
301 |
char pattern[] = searchStr.toCharArray(); |
|
302 |
||
303 |
int pos = (int)start-1; |
|
304 |
int i = 0; |
|
305 |
long patlen = pattern.length; |
|
306 |
||
307 |
while (pos < len) { |
|
308 |
if (pattern[i] == buf[pos]) { |
|
309 |
if (i + 1 == patlen) { |
|
310 |
return (pos + 1) - (patlen - 1); |
|
311 |
} |
|
312 |
i++; pos++; // increment pos, and i |
|
313 |
||
314 |
} else if (pattern[i] != buf[pos]) { |
|
315 |
pos++; // increment pos only |
|
316 |
} |
|
317 |
} |
|
318 |
return -1; // not found |
|
319 |
} |
|
320 |
||
321 |
/** |
|
322 |
* Returns the position in this <code>SerialClob</code> object |
|
323 |
* where the given <code>Clob</code> signature begins, starting |
|
324 |
* the search at the specified position. This method returns |
|
325 |
* <code>-1</code> if the pattern is not found. |
|
326 |
* |
|
327 |
* @param searchStr the <code>Clob</code> object for which to search |
|
328 |
* @param start the position in this <code>SerialClob</code> object |
|
329 |
* at which to begin the search; the first position is |
|
330 |
* <code>1</code>; must not be less than <code>1</code> nor |
|
331 |
* greater than the length of this <code>SerialClob</code> object |
|
332 |
* @return the position at which the given <code>Clob</code> |
|
333 |
* object begins in this <code>SerialClob</code> object, |
|
334 |
* at or after the specified starting position |
|
335 |
* @throws SerialException if an error occurs locating the Clob signature |
|
336 |
* @throws SQLException if there is an error accessing the Blob value |
|
337 |
* from the database |
|
338 |
*/ |
|
339 |
public long position(Clob searchStr, long start) |
|
340 |
throws SerialException, SQLException { |
|
341 |
||
342 |
return position(searchStr.getSubString(1,(int)searchStr.length()), start); |
|
343 |
} |
|
344 |
||
345 |
/** |
|
346 |
* Writes the given Java <code>String</code> to the <code>CLOB</code> |
|
347 |
* value that this <code>SerialClob</code> object represents, at the position |
|
348 |
* <code>pos</code>. |
|
349 |
* |
|
350 |
* @param pos the position at which to start writing to the <code>CLOB</code> |
|
351 |
* value that this <code>SerialClob</code> object represents; the first |
|
352 |
* position is <code>1</code>; must not be less than <code>1</code> nor |
|
353 |
* greater than the length of this <code>SerialClob</code> object |
|
354 |
* @param str the string to be written to the <code>CLOB</code> |
|
355 |
* value that this <code>SerialClob</code> object represents |
|
356 |
* @return the number of characters written |
|
357 |
* @throws SerialException if there is an error accessing the |
|
358 |
* <code>CLOB</code> value; if an invalid position is set; if an |
|
359 |
* invalid offset value is set; if number of bytes to be written |
|
360 |
* is greater than the <code>SerialClob</code> length; or the combined |
|
361 |
* values of the length and offset is greater than the Clob buffer |
|
362 |
*/ |
|
363 |
public int setString(long pos, String str) throws SerialException { |
|
364 |
return (setString(pos, str, 0, str.length())); |
|
365 |
} |
|
366 |
||
367 |
/** |
|
368 |
* Writes <code>len</code> characters of <code>str</code>, starting |
|
369 |
* at character <code>offset</code>, to the <code>CLOB</code> value |
|
370 |
* that this <code>Clob</code> represents. |
|
371 |
* |
|
372 |
* @param pos the position at which to start writing to the <code>CLOB</code> |
|
373 |
* value that this <code>SerialClob</code> object represents; the first |
|
374 |
* position is <code>1</code>; must not be less than <code>1</code> nor |
|
375 |
* greater than the length of this <code>SerialClob</code> object |
|
376 |
* @param str the string to be written to the <code>CLOB</code> |
|
377 |
* value that this <code>Clob</code> object represents |
|
378 |
* @param offset the offset into <code>str</code> to start reading |
|
379 |
* the characters to be written |
|
380 |
* @param length the number of characters to be written |
|
381 |
* @return the number of characters written |
|
382 |
* @throws SerialException if there is an error accessing the |
|
383 |
* <code>CLOB</code> value; if an invalid position is set; if an |
|
384 |
* invalid offset value is set; if number of bytes to be written |
|
385 |
* is greater than the <code>SerialClob</code> length; or the combined |
|
386 |
* values of the length and offset is greater than the Clob buffer |
|
387 |
*/ |
|
388 |
public int setString(long pos, String str, int offset, int length) |
|
389 |
throws SerialException { |
|
390 |
String temp = str.substring(offset); |
|
391 |
char cPattern[] = temp.toCharArray(); |
|
392 |
||
393 |
if (offset < 0 || offset > str.length()) { |
|
394 |
throw new SerialException("Invalid offset in byte array set"); |
|
395 |
} |
|
396 |
||
397 |
if (pos < 1 || pos > this.length()) { |
|
398 |
throw new SerialException("Invalid position in BLOB object set"); |
|
399 |
} |
|
400 |
||
401 |
if ((long)(length) > origLen) { |
|
402 |
throw new SerialException("Buffer is not sufficient to hold the value"); |
|
403 |
} |
|
404 |
||
405 |
if ((length + offset) > str.length()) { |
|
406 |
// need check to ensure length + offset !> bytes.length |
|
407 |
throw new SerialException("Invalid OffSet. Cannot have combined offset " + |
|
408 |
" and length that is greater that the Blob buffer"); |
|
409 |
} |
|
410 |
||
411 |
int i = 0; |
|
412 |
pos--; //values in the array are at position one less |
|
413 |
while ( i < length || (offset + i +1) < (str.length() - offset ) ) { |
|
414 |
this.buf[(int)pos + i ] = cPattern[offset + i ]; |
|
415 |
i++; |
|
416 |
} |
|
417 |
return i; |
|
418 |
} |
|
419 |
||
420 |
/** |
|
421 |
* Retrieves a stream to be used to write Ascii characters to the |
|
422 |
* <code>CLOB</code> value that this <code>SerialClob</code> object represents, |
|
423 |
* starting at position <code>pos</code>. This method forwards the |
|
424 |
* <code>setAsciiStream()</code> call to the underlying <code>Clob</code> object in |
|
425 |
* the event that this <code>SerialClob</code> object is instantiated with a |
|
426 |
* <code>Clob</code> object. If this <code>SerialClob</code> object is instantiated |
|
427 |
* with a <code>char</code> array, a <code>SerialException</code> object is thrown. |
|
428 |
* |
|
429 |
* @param pos the position at which to start writing to the |
|
430 |
* <code>CLOB</code> object |
|
431 |
* @return the stream to which ASCII encoded characters can be written |
|
432 |
* @throws SerialException if SerialClob is not instantiated with a |
|
433 |
* Clob object that supports <code>setAsciiStream</code> |
|
434 |
* @throws SQLException if there is an error accessing the |
|
435 |
* <code>CLOB</code> value |
|
436 |
* @see #getAsciiStream |
|
437 |
*/ |
|
438 |
public java.io.OutputStream setAsciiStream(long pos) |
|
439 |
throws SerialException, SQLException { |
|
10323
48d695d1e966
7077451: SerialLob, SerialClob have the wrong checks for setStream methods
lancea
parents:
9035
diff
changeset
|
440 |
if (this.clob != null) { |
2 | 441 |
return this.clob.setAsciiStream(pos); |
442 |
} else { |
|
443 |
throw new SerialException("Unsupported operation. SerialClob cannot " + |
|
444 |
"return a writable ascii stream\n unless instantiated with a Clob object " + |
|
445 |
"that has a setAsciiStream() implementation"); |
|
446 |
} |
|
447 |
} |
|
448 |
||
449 |
/** |
|
450 |
* Retrieves a stream to be used to write a stream of Unicode characters |
|
451 |
* to the <code>CLOB</code> value that this <code>SerialClob</code> object |
|
452 |
* represents, at position <code>pos</code>. This method forwards the |
|
453 |
* <code>setCharacterStream()</code> call to the underlying <code>Clob</code> |
|
454 |
* object in the event that this <code>SerialClob</code> object is instantiated with a |
|
455 |
* <code>Clob</code> object. If this <code>SerialClob</code> object is instantiated with |
|
456 |
* a <code>char</code> array, a <code>SerialException</code> is thrown. |
|
457 |
* |
|
458 |
* @param pos the position at which to start writing to the |
|
459 |
* <code>CLOB</code> value |
|
460 |
* |
|
461 |
* @return a stream to which Unicode encoded characters can be written |
|
462 |
* @throws SerialException if the SerialClob is not instantiated with |
|
463 |
* a Clob object that supports <code>setCharacterStream</code> |
|
464 |
* @throws SQLException if there is an error accessing the |
|
465 |
* <code>CLOB</code> value |
|
466 |
* @see #getCharacterStream |
|
467 |
*/ |
|
468 |
public java.io.Writer setCharacterStream(long pos) |
|
469 |
throws SerialException, SQLException { |
|
10323
48d695d1e966
7077451: SerialLob, SerialClob have the wrong checks for setStream methods
lancea
parents:
9035
diff
changeset
|
470 |
if (this.clob != null) { |
2 | 471 |
return this.clob.setCharacterStream(pos); |
472 |
} else { |
|
473 |
throw new SerialException("Unsupported operation. SerialClob cannot " + |
|
474 |
"return a writable character stream\n unless instantiated with a Clob object " + |
|
475 |
"that has a setCharacterStream implementation"); |
|
476 |
} |
|
477 |
} |
|
478 |
||
479 |
/** |
|
480 |
* Truncates the <code>CLOB</code> value that this <code>SerialClob</code> |
|
481 |
* object represents so that it has a length of <code>len</code> |
|
482 |
* characters. |
|
483 |
* <p> |
|
484 |
* Truncating a <code>SerialClob</code> object to length 0 has the effect of |
|
485 |
* clearing its contents. |
|
486 |
* |
|
487 |
* @param length the length, in bytes, to which the <code>CLOB</code> |
|
488 |
* value should be truncated |
|
489 |
* @throws SQLException if there is an error accessing the |
|
490 |
* <code>CLOB</code> value |
|
491 |
*/ |
|
492 |
public void truncate(long length) throws SerialException { |
|
493 |
if (length > len) { |
|
494 |
throw new SerialException |
|
495 |
("Length more than what can be truncated"); |
|
496 |
} else { |
|
497 |
len = length; |
|
498 |
// re-size the buffer |
|
499 |
||
500 |
if (len == 0) { |
|
501 |
buf = new char[] {}; |
|
502 |
} else { |
|
503 |
buf = (this.getSubString(1, (int)len)).toCharArray(); |
|
504 |
} |
|
505 |
||
506 |
} |
|
507 |
} |
|
508 |
||
509 |
||
510 |
public Reader getCharacterStream(long pos, long length) throws SQLException { |
|
511 |
throw new java.lang.UnsupportedOperationException("Not supported"); |
|
512 |
} |
|
513 |
||
514 |
public void free() throws SQLException { |
|
515 |
throw new java.lang.UnsupportedOperationException("Not supported"); |
|
516 |
} |
|
517 |
||
518 |
/** |
|
14337
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
519 |
* Compares this SerialClob to the specified object. The result is {@code |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
520 |
* true} if and only if the argument is not {@code null} and is a {@code |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
521 |
* SerialClob} object that represents the same sequence of characters as this |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
522 |
* object. |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
523 |
* |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
524 |
* @param obj The object to compare this {@code SerialClob} against |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
525 |
* |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
526 |
* @return {@code true} if the given object represents a {@code SerialClob} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
527 |
* equivalent to this SerialClob, {@code false} otherwise |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
528 |
* |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
529 |
*/ |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
530 |
public boolean equals(Object obj) { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
531 |
if (this == obj) { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
532 |
return true; |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
533 |
} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
534 |
if (obj instanceof SerialClob) { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
535 |
SerialClob sc = (SerialClob)obj; |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
536 |
if (this.len == sc.len) { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
537 |
return Arrays.equals(buf, sc.buf); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
538 |
} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
539 |
} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
540 |
return false; |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
541 |
} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
542 |
|
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
543 |
/** |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
544 |
* Returns a hash code for this {@code SerialClob}. |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
545 |
* @return a hash code value for this object. |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
546 |
*/ |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
547 |
public int hashCode() { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
548 |
return ((31 + Arrays.hashCode(buf)) * 31 + (int)len) * 31 + (int)origLen; |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
549 |
} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
550 |
|
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
551 |
/** |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
552 |
* Returns a clone of this {@code SerialClob}. The copy will contain a |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
553 |
* reference to a clone of the internal character array, not a reference |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
554 |
* to the original internal character array of this {@code SerialClob} object. |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
555 |
* The underlying {@code Clob} object will be set to null. |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
556 |
* |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
557 |
* @return a clone of this SerialClob |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
558 |
*/ |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
559 |
public Object clone() { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
560 |
try { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
561 |
SerialClob sc = (SerialClob) super.clone(); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
562 |
sc.buf = Arrays.copyOf(buf, (int)len); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
563 |
sc.clob = null; |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
564 |
return sc; |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
565 |
} catch (CloneNotSupportedException ex) { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
566 |
// this shouldn't happen, since we are Cloneable |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
567 |
throw new InternalError(); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
568 |
} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
569 |
} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
570 |
|
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
571 |
/** |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
572 |
* readObject is called to restore the state of the SerialClob from |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
573 |
* a stream. |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
574 |
*/ |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
575 |
private void readObject(ObjectInputStream s) |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
576 |
throws IOException, ClassNotFoundException { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
577 |
|
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
578 |
ObjectInputStream.GetField fields = s.readFields(); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
579 |
char[] tmp = (char[])fields.get("buf", null); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
580 |
if (tmp == null) |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
581 |
throw new InvalidObjectException("buf is null and should not be!"); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
582 |
buf = tmp.clone(); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
583 |
len = fields.get("len", 0L); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
584 |
if (buf.length != len) |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
585 |
throw new InvalidObjectException("buf is not the expected size"); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
586 |
origLen = fields.get("origLen", 0L); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
587 |
clob = (Clob) fields.get("clob", null); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
588 |
} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
589 |
|
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
590 |
/** |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
591 |
* writeObject is called to save the state of the SerialClob |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
592 |
* to a stream. |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
593 |
*/ |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
594 |
private void writeObject(ObjectOutputStream s) |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
595 |
throws IOException, ClassNotFoundException { |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
596 |
|
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
597 |
ObjectOutputStream.PutField fields = s.putFields(); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
598 |
fields.put("buf", buf); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
599 |
fields.put("len", len); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
600 |
fields.put("origLen", origLen); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
601 |
// Note: this check to see if it is an instance of Serializable |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
602 |
// is for backwards compatibiity |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
603 |
fields.put("clob", clob instanceof Serializable ? clob : null); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
604 |
s.writeFields(); |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
605 |
} |
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
606 |
|
a003c6a54cb6
8001536: Added readObject,writeObject,clone, equals, hashcode to SerialXLob
lancea
parents:
10323
diff
changeset
|
607 |
/** |
2 | 608 |
* The identifier that assists in the serialization of this <code>SerialClob</code> |
609 |
* object. |
|
610 |
*/ |
|
611 |
static final long serialVersionUID = -1662519690087375313L; |
|
612 |
} |