01: /*
02:
03: Derby - Class org.apache.derby.client.am.ClobOutputStream
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to You under the Apache License, Version 2.0
09: (the "License"); you may not use this file except in compliance with
10: the License. You may obtain a copy of the License at
11:
12: http://www.apache.org/licenses/LICENSE-2.0
13:
14: Unless required by applicable law or agreed to in writing, software
15: distributed under the License is distributed on an "AS IS" BASIS,
16: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: See the License for the specific language governing permissions and
18: limitations under the License.
19:
20: */
21:
22: package org.apache.derby.client.am;
23:
24: public class ClobOutputStream extends java.io.OutputStream {
25: private Clob clob_;
26: private long offset_;
27:
28: public ClobOutputStream(Clob clob, long offset) throws SqlException {
29: clob_ = clob;
30: offset_ = offset;
31:
32: /*
33: offset_ starts from 1 while sqlLenth_=0
34: in the case of a empty Clob hence check from
35: offset_-1
36: */
37: if ((offset_ - 1) > clob_.sqlLength_) {
38: throw new IndexOutOfBoundsException();
39: }
40: }
41:
42: public void write(int b) throws java.io.IOException {
43: byte[] newByte = new byte[1];
44: newByte[0] = (byte) b;
45: clob_.string_ = clob_.string_.substring(0, (int) offset_ - 1);
46: // Since this is an OutputStream returned by Clob.setAsciiStream
47: // use Ascii encoding when creating the String from bytes
48: clob_.string_ = clob_.string_.concat(new String(newByte,
49: "US-ASCII"));
50: clob_.asciiStream_ = new java.io.StringBufferInputStream(
51: clob_.string_);
52: clob_.unicodeStream_ = new java.io.StringBufferInputStream(
53: clob_.string_);
54: clob_.characterStream_ = new java.io.StringReader(clob_.string_);
55: clob_.sqlLength_ = clob_.string_.length();
56: offset_++;
57: }
58:
59: public void write(byte b[], int off, int len)
60: throws java.io.IOException {
61: if (b == null) {
62: throw new NullPointerException();
63: } else if ((off < 0) || (off > b.length) || (len < 0)
64: || ((off + len) > b.length) || ((off + len) < 0)) {
65: throw new IndexOutOfBoundsException();
66: } else if (len == 0) {
67: return;
68: }
69:
70: byte[] newByte = new byte[len];
71: System.arraycopy(b, off, newByte, 0, len);
72: // Since this is an OutputStream returned by Clob.setAsciiStream
73: // use Ascii encoding when creating the String from bytes
74: String str = new String(newByte, "US-ASCII");
75: clob_.string_ = clob_.string_.substring(0, (int) offset_ - 1);
76: clob_.string_ = clob_.string_.concat(str);
77: clob_.asciiStream_ = new java.io.StringBufferInputStream(
78: clob_.string_);
79: clob_.unicodeStream_ = new java.io.StringBufferInputStream(
80: clob_.string_);
81: clob_.characterStream_ = new java.io.StringReader(clob_.string_);
82: clob_.sqlLength_ = clob_.string_.length();
83: offset_ += len;
84: }
85: }
|