001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package com.mysql.jdbc;
026:
027: import java.io.ByteArrayInputStream;
028: import java.io.InputStream;
029: import java.io.OutputStream;
030: import java.io.Reader;
031: import java.io.StringReader;
032: import java.io.Writer;
033:
034: import java.sql.SQLException;
035:
036: import com.mysql.jdbc.exceptions.NotYetImplementedException;
037:
038: /**
039: * Simplistic implementation of java.sql.Clob for MySQL Connector/J
040: *
041: * @author Mark Matthews
042: * @version $Id: Clob.java 6342 2007-03-09 21:45:01Z mmatthews $
043: */
044: public class Clob implements java.sql.Clob, OutputStreamWatcher,
045: WriterWatcher {
046: private String charData;
047:
048: Clob() {
049: this .charData = "";
050: }
051:
052: Clob(String charDataInit) {
053: this .charData = charDataInit;
054: }
055:
056: /**
057: * @see java.sql.Clob#getAsciiStream()
058: */
059: public InputStream getAsciiStream() throws SQLException {
060: if (this .charData != null) {
061: return new ByteArrayInputStream(this .charData.getBytes());
062: }
063:
064: return null;
065: }
066:
067: /**
068: * @see java.sql.Clob#getCharacterStream()
069: */
070: public Reader getCharacterStream() throws SQLException {
071: if (this .charData != null) {
072: return new StringReader(this .charData);
073: }
074:
075: return null;
076: }
077:
078: /**
079: * @see java.sql.Clob#getSubString(long, int)
080: */
081: public String getSubString(long startPos, int length)
082: throws SQLException {
083: if (startPos < 1) {
084: throw SQLError.createSQLException(Messages
085: .getString("Clob.6"), //$NON-NLS-1$
086: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
087: }
088:
089: int adjustedStartPos = (int) startPos - 1;
090: int adjustedEndIndex = adjustedStartPos + length;
091:
092: if (this .charData != null) {
093: if (adjustedEndIndex > this .charData.length()) {
094: throw SQLError.createSQLException(Messages
095: .getString("Clob.7"), //$NON-NLS-1$
096: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
097: }
098:
099: return this .charData.substring(adjustedStartPos,
100: adjustedEndIndex);
101: }
102:
103: return null;
104: }
105:
106: /**
107: * @see java.sql.Clob#length()
108: */
109: public long length() throws SQLException {
110: if (this .charData != null) {
111: return this .charData.length();
112: }
113:
114: return 0;
115: }
116:
117: /**
118: * @see java.sql.Clob#position(Clob, long)
119: */
120: public long position(java.sql.Clob arg0, long arg1)
121: throws SQLException {
122: return position(arg0.getSubString(0L, (int) arg0.length()),
123: arg1);
124: }
125:
126: /**
127: * @see java.sql.Clob#position(String, long)
128: */
129: public long position(String stringToFind, long startPos)
130: throws SQLException {
131: if (startPos < 1) {
132: throw SQLError
133: .createSQLException(
134: Messages.getString("Clob.8") //$NON-NLS-1$
135: + startPos
136: + Messages.getString("Clob.9"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
137: }
138:
139: if (this .charData != null) {
140: if ((startPos - 1) > this .charData.length()) {
141: throw SQLError.createSQLException(Messages
142: .getString("Clob.10"), //$NON-NLS-1$
143: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
144: }
145:
146: int pos = this .charData.indexOf(stringToFind,
147: (int) (startPos - 1));
148:
149: return (pos == -1) ? (-1) : (pos + 1);
150: }
151:
152: return -1;
153: }
154:
155: /**
156: * @see java.sql.Clob#setAsciiStream(long)
157: */
158: public OutputStream setAsciiStream(long indexToWriteAt)
159: throws SQLException {
160: if (indexToWriteAt < 1) {
161: throw SQLError.createSQLException(Messages
162: .getString("Clob.0"), //$NON-NLS-1$
163: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
164: }
165:
166: WatchableOutputStream bytesOut = new WatchableOutputStream();
167: bytesOut.setWatcher(this );
168:
169: if (indexToWriteAt > 0) {
170: bytesOut.write(this .charData.getBytes(), 0,
171: (int) (indexToWriteAt - 1));
172: }
173:
174: return bytesOut;
175: }
176:
177: /**
178: * @see java.sql.Clob#setCharacterStream(long)
179: */
180: public Writer setCharacterStream(long indexToWriteAt)
181: throws SQLException {
182: if (indexToWriteAt < 1) {
183: throw SQLError.createSQLException(Messages
184: .getString("Clob.1"), //$NON-NLS-1$
185: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
186: }
187:
188: WatchableWriter writer = new WatchableWriter();
189: writer.setWatcher(this );
190:
191: //
192: // Don't call write() if nothing to write...
193: //
194: if (indexToWriteAt > 1) {
195: writer.write(this .charData, 0, (int) (indexToWriteAt - 1));
196: }
197:
198: return writer;
199: }
200:
201: /**
202: * @see java.sql.Clob#setString(long, String)
203: */
204: public int setString(long pos, String str) throws SQLException {
205: if (pos < 1) {
206: throw SQLError.createSQLException(Messages
207: .getString("Clob.2"), //$NON-NLS-1$
208: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
209: }
210:
211: if (str == null) {
212: throw SQLError.createSQLException(Messages
213: .getString("Clob.3"), //$NON-NLS-1$
214: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
215: }
216:
217: StringBuffer charBuf = new StringBuffer(this .charData);
218:
219: pos--;
220:
221: int strLength = str.length();
222:
223: charBuf.replace((int) pos, (int) (pos + strLength), str);
224:
225: this .charData = charBuf.toString();
226:
227: return strLength;
228: }
229:
230: /**
231: * @see java.sql.Clob#setString(long, String, int, int)
232: */
233: public int setString(long pos, String str, int offset, int len)
234: throws SQLException {
235: if (pos < 1) {
236: throw SQLError.createSQLException(Messages
237: .getString("Clob.4"), //$NON-NLS-1$
238: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
239: }
240:
241: if (str == null) {
242: throw SQLError.createSQLException(Messages
243: .getString("Clob.5"), //$NON-NLS-1$
244: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
245: }
246:
247: StringBuffer charBuf = new StringBuffer(this .charData);
248:
249: pos--;
250:
251: String replaceString = str.substring(offset, len);
252:
253: charBuf.replace((int) pos,
254: (int) (pos + replaceString.length()), replaceString);
255:
256: this .charData = charBuf.toString();
257:
258: return len;
259: }
260:
261: /**
262: * @see com.mysql.jdbc.OutputStreamWatcher#streamClosed(byte[])
263: */
264: public void streamClosed(WatchableOutputStream out) {
265: int streamSize = out.size();
266:
267: if (streamSize < this .charData.length()) {
268: try {
269: out.write(StringUtils.getBytes(this .charData, null,
270: null, false, null), streamSize, this .charData
271: .length()
272: - streamSize);
273: } catch (SQLException ex) {
274: //
275: }
276: }
277:
278: this .charData = StringUtils.toAsciiString(out.toByteArray());
279: }
280:
281: /**
282: * @see java.sql.Clob#truncate(long)
283: */
284: public void truncate(long length) throws SQLException {
285: if (length > this .charData.length()) {
286: throw SQLError
287: .createSQLException(Messages.getString("Clob.11") //$NON-NLS-1$
288: + this .charData.length()
289: + Messages.getString("Clob.12") + length + Messages.getString("Clob.13")); //$NON-NLS-1$ //$NON-NLS-2$
290: }
291:
292: this .charData = this .charData.substring(0, (int) length);
293: }
294:
295: /**
296: * @see com.mysql.jdbc.WriterWatcher#writerClosed(char[])
297: */
298: public void writerClosed(char[] charDataBeingWritten) {
299: this .charData = new String(charDataBeingWritten);
300: }
301:
302: /**
303: * @see com.mysql.jdbc.WriterWatcher#writerClosed(char[])
304: */
305: public void writerClosed(WatchableWriter out) {
306: int dataLength = out.size();
307:
308: if (dataLength < this .charData.length()) {
309: out.write(this .charData, dataLength, this .charData.length()
310: - dataLength);
311: }
312:
313: this .charData = out.toString();
314: }
315:
316: public void free() throws SQLException {
317: throw new NotYetImplementedException();
318: }
319:
320: public Reader getCharacterStream(long pos, long length)
321: throws SQLException {
322: throw new NotYetImplementedException();
323: }
324: }
|