001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.sql;
024:
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030: import java.io.OutputStream;
031: import java.io.Reader;
032: import java.io.StringWriter;
033: import java.io.Writer;
034: import java.sql.Clob;
035: import java.sql.SQLException;
036:
037: import org.isqlviewer.util.BasicUtilities;
038:
039: /**
040: * Utility Implementation for dealing with CLOB objects in memory.
041: * <p>
042: * This is very easy implementation of a CLOB using the StringBuffer. This class is designed for easily creating CLOB
043: * objects at runtime using Strings.
044: *
045: * @see java.lang.StringBuffer
046: * @author Markus A. Kobold <mkobold at sprintpcs dot com>
047: */
048: public class ByteArrayClob implements Clob {
049:
050: protected StringWriter clobStorage = new StringWriter();
051: protected StringBuffer buff = new StringBuffer("");
052:
053: public ByteArrayClob(String clobData) throws IOException {
054:
055: this (clobData.toCharArray());
056: }
057:
058: public ByteArrayClob(char[] clobData) throws IOException {
059:
060: clobStorage.write(clobData);
061: buff = clobStorage.getBuffer();
062: }
063:
064: public ByteArrayClob(Reader reader) throws IOException {
065:
066: BasicUtilities.copyReader(reader, clobStorage);
067: buff = clobStorage.getBuffer();
068: }
069:
070: public long length() {
071:
072: return buff.length();
073: }
074:
075: public String getSubString(long pos, int length) {
076:
077: return buff.substring((int) pos, (int) pos + length);
078: }
079:
080: public Reader getCharacterStream() {
081:
082: return new InputStreamReader(getAsciiStream());
083: }
084:
085: public InputStream getAsciiStream() {
086:
087: String buffer = buff.toString();
088: return new ByteArrayInputStream(buffer.getBytes());
089: }
090:
091: public long position(String searchstr, long start) {
092:
093: String x = clobStorage.toString();
094: return x.indexOf(searchstr, (int) start);
095: }
096:
097: public long position(Clob searchstr, long start) {
098:
099: return 0;
100: }
101:
102: public int setString(long pos, String str) throws SQLException {
103:
104: return setString(pos, str, 0, str.length());
105: }
106:
107: public int setString(long pos, String str, int offset, int len)
108: throws SQLException {
109:
110: buff.insert((int) pos, str.toCharArray(), offset, len);
111: return len;
112: }
113:
114: public OutputStream setAsciiStream(long pos) {
115:
116: return new ByteArrayOutputStream();
117: }
118:
119: public Writer setCharacterStream(long pos) {
120:
121: return new StringWriter();
122: }
123:
124: public void truncate(long len) throws SQLException {
125:
126: buff.setLength((int) len);
127: }
128: }
|