KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > Clob


1 /*
2    Copyright (C) 2002 MySQL AB
3
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8
9       This program is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12       GNU General Public License for more details.
13
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18  */

19 package com.mysql.jdbc;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.io.Writer JavaDoc;
27
28 import java.sql.SQLException JavaDoc;
29
30
31 /**
32  * Simplistic implementation of java.sql.Clob for MySQL Connector/J
33  *
34  * @version $Id: Clob.java,v 1.5.2.3 2003/12/24 05:16:25 mmatthew Exp $
35  * @author Mark Matthews
36  */

37 public class Clob implements java.sql.Clob JavaDoc, OutputStreamWatcher, WriterWatcher {
38     private String JavaDoc charData;
39
40     Clob(String JavaDoc charData) {
41         this.charData = charData;
42     }
43
44     /**
45      * @see java.sql.Clob#setAsciiStream(long)
46      */

47     public OutputStream JavaDoc setAsciiStream(long indexToWriteAt)
48         throws SQLException JavaDoc {
49         if (indexToWriteAt < 1) {
50             throw new SQLException JavaDoc("indexToWriteAt must be >= 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
51         }
52
53         WatchableOutputStream bytesOut = new WatchableOutputStream();
54         bytesOut.setWatcher(this);
55
56         if (indexToWriteAt > 0) {
57             bytesOut.write(this.charData.getBytes(), 0,
58                 (int) (indexToWriteAt - 1));
59         }
60
61         return bytesOut;
62     }
63
64     /**
65      * @see java.sql.Clob#getAsciiStream()
66      */

67     public InputStream JavaDoc getAsciiStream() throws SQLException JavaDoc {
68         if (this.charData != null) {
69             return new ByteArrayInputStream JavaDoc(this.charData.getBytes());
70         } else {
71             return null;
72         }
73     }
74
75     /**
76      * @see java.sql.Clob#setCharacterStream(long)
77      */

78     public Writer JavaDoc setCharacterStream(long indexToWriteAt)
79         throws SQLException JavaDoc {
80         if (indexToWriteAt < 1) {
81             throw new SQLException JavaDoc("indexToWriteAt must be >= 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
82         }
83
84         WatchableWriter writer = new WatchableWriter();
85         writer.setWatcher(this);
86
87         //
88
// Don't call write() if nothing to write...
89
//
90

91         if (indexToWriteAt > 1) {
92             writer.write(this.charData, 0, (int) (indexToWriteAt - 1));
93         }
94
95         return writer;
96     }
97
98     /**
99      * @see java.sql.Clob#getCharacterStream()
100      */

101     public Reader JavaDoc getCharacterStream() throws SQLException JavaDoc {
102         if (this.charData != null) {
103             return new StringReader JavaDoc(this.charData);
104         } else {
105             return null;
106         }
107     }
108
109     /**
110      * @see java.sql.Clob#setString(long, String)
111      */

112     public int setString(long pos, String JavaDoc str) throws SQLException JavaDoc {
113         if (pos < 1) {
114             throw new SQLException JavaDoc("Starting position can not be < 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
115         }
116
117         if (str == null) {
118             throw new SQLException JavaDoc("String to set can not be NULL", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
119         }
120
121         StringBuffer JavaDoc charBuf = new StringBuffer JavaDoc(this.charData);
122
123         pos--;
124
125         int strLength = str.length();
126
127         charBuf.replace((int) pos, (int) (pos + strLength), str);
128
129         this.charData = charBuf.toString();
130
131         return strLength;
132     }
133
134     /**
135      * @see java.sql.Clob#setString(long, String, int, int)
136      */

137     public int setString(long pos, String JavaDoc str, int offset, int len)
138         throws SQLException JavaDoc {
139         if (pos < 1) {
140             throw new SQLException JavaDoc("Starting position can not be < 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
141         }
142
143         if (str == null) {
144             throw new SQLException JavaDoc("String to set can not be NULL", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
145         }
146
147         StringBuffer JavaDoc charBuf = new StringBuffer JavaDoc(this.charData);
148
149         pos--;
150
151         String JavaDoc replaceString = str.substring(offset, len);
152
153         charBuf.replace((int) pos, (int) (pos + replaceString.length()),
154             replaceString);
155
156         this.charData = charBuf.toString();
157
158         return len;
159     }
160
161     /**
162      * @see java.sql.Clob#getSubString(long, int)
163      */

164     public String JavaDoc getSubString(long startPos, int length)
165         throws SQLException JavaDoc {
166         if (startPos < 1) {
167             throw new SQLException JavaDoc("CLOB start position can not be < 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
168         }
169
170         if (this.charData != null) {
171             if (((startPos - 1) + length) > charData.length()) {
172                 throw new SQLException JavaDoc("CLOB start position + length can not be > length of CLOB",
173                     SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
174             }
175
176             return this.charData.substring((int) (startPos - 1), length);
177         } else {
178             return null;
179         }
180     }
181
182     /**
183      * @see java.sql.Clob#length()
184      */

185     public long length() throws SQLException JavaDoc {
186         if (this.charData != null) {
187             return this.charData.length();
188         } else {
189             return 0;
190         }
191     }
192
193     /**
194      * @see java.sql.Clob#position(String, long)
195      */

196     public long position(String JavaDoc stringToFind, long startPos)
197         throws SQLException JavaDoc {
198         if (startPos < 1) {
199             throw new SQLException JavaDoc("Illegal starting position for search, '"
200                 + startPos + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
201         }
202
203         if (this.charData != null) {
204             if ((startPos - 1) > this.charData.length()) {
205                 throw new SQLException JavaDoc("Starting position for search is past end of CLOB",
206                     SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
207             }
208
209             int pos = this.charData.indexOf(stringToFind, (int) (startPos - 1));
210
211             return (pos == -1) ? (-1) : (pos + 1);
212         } else {
213             return -1;
214         }
215     }
216
217     /**
218      * @see java.sql.Clob#position(Clob, long)
219      */

220     public long position(java.sql.Clob JavaDoc arg0, long arg1)
221         throws SQLException JavaDoc {
222         return position(arg0.getSubString(0L, (int) arg0.length()), arg1);
223     }
224
225     /**
226      * @see com.mysql.jdbc.OutputStreamWatcher#streamClosed(byte[])
227      */

228     public void streamClosed(byte[] byteData) {
229         this.charData = StringUtils.toAsciiString(byteData);
230     }
231
232     /**
233      * @see java.sql.Clob#truncate(long)
234      */

235     public void truncate(long length) throws SQLException JavaDoc {
236         if (length > this.charData.length()) {
237             throw new SQLException JavaDoc("Cannot truncate CLOB of length "
238             + this.charData.length()
239             + " to length of "
240             + length
241             + ".");
242         }
243         
244         this.charData = this.charData.substring(0, (int) length);
245     }
246
247     /**
248      * @see com.mysql.jdbc.WriterWatcher#writerClosed(char[])
249      */

250     public void writerClosed(char[] charData) {
251         this.charData = new String JavaDoc(charData);
252     }
253 }
254
Popular Tags