KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > ClobOutputStream


1 /*
2
3    Derby - Class org.apache.derby.client.am.ClobOutputStream
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (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
25 public class ClobOutputStream extends java.io.OutputStream JavaDoc {
26     private Clob clob_;
27     private long offset_;
28
29     public ClobOutputStream(Clob clob, long offset) throws SqlException {
30         clob_ = clob;
31         offset_ = offset;
32         
33         /*
34             offset_ starts from 1 while sqlLenth_=0
35             in the case of a empty Clob hence check from
36             offset_-1
37          */

38         if ((offset_-1) > clob_.sqlLength_) {
39             throw new IndexOutOfBoundsException JavaDoc();
40         }
41     }
42
43     public void write(int b) throws java.io.IOException JavaDoc {
44         byte[] newByte = new byte[1];
45         newByte[0] = (byte)b;
46         clob_.string_ = clob_.string_.substring(0, (int) offset_ - 1);
47         // Since this is an OutputStream returned by Clob.setAsciiStream
48
// use Ascii encoding when creating the String from bytes
49
clob_.string_ = clob_.string_.concat(new String JavaDoc(newByte, "US-ASCII"));
50         clob_.asciiStream_ = new java.io.StringBufferInputStream JavaDoc(clob_.string_);
51         clob_.unicodeStream_ = new java.io.StringBufferInputStream JavaDoc(clob_.string_);
52         clob_.characterStream_ = new java.io.StringReader JavaDoc(clob_.string_);
53         clob_.sqlLength_ = clob_.string_.length();
54         offset_++;
55     }
56
57
58     public void write(byte b[], int off, int len) throws java.io.IOException JavaDoc {
59         if (b == null) {
60             throw new NullPointerException JavaDoc();
61         } else if ((off < 0) || (off > b.length) || (len < 0) ||
62                 ((off + len) > b.length) || ((off + len) < 0)) {
63             throw new IndexOutOfBoundsException JavaDoc();
64         } else if (len == 0) {
65             return;
66         }
67
68         byte[] newByte = new byte[len];
69         System.arraycopy(b, off, newByte, 0, len);
70         // Since this is an OutputStream returned by Clob.setAsciiStream
71
// use Ascii encoding when creating the String from bytes
72
String JavaDoc str = new String JavaDoc(newByte, "US-ASCII");
73         clob_.string_ = clob_.string_.substring(0, (int) offset_ - 1);
74         clob_.string_ = clob_.string_.concat(str);
75         clob_.asciiStream_ = new java.io.StringBufferInputStream JavaDoc(clob_.string_);
76         clob_.unicodeStream_ = new java.io.StringBufferInputStream JavaDoc(clob_.string_);
77         clob_.characterStream_ = new java.io.StringReader JavaDoc(clob_.string_);
78         clob_.sqlLength_ = clob_.string_.length();
79         offset_ += len;
80     }
81 }
82
83
Popular Tags