001: /*
002: * $Id: StringClob.java,v 1.7 2007/11/13 19:04:02 rwald Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.ByteArrayInputStream;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.OutputStream;
047: import java.io.Reader;
048: import java.io.StringReader;
049: import java.io.UnsupportedEncodingException;
050: import java.io.Writer;
051: import java.sql.Clob;
052: import java.sql.SQLException;
053:
054: import org.axiondb.jdbc.AxionClob;
055: import org.axiondb.util.ExceptionConverter;
056:
057: /**
058: * A {@link Clob} based upon a simple {@link String}.
059: * This type allows any string value to be treated as
060: * a {@link AxionClob}.
061: *
062: * @version $Revision: 1.7 $ $Date: 2007/11/13 19:04:02 $
063: * @author Rodney Waldhoff
064: */
065: public class StringClob implements AxionClob {
066:
067: public StringClob(String str) {
068: if (null == str) {
069: throw new NullPointerException();
070: }
071: _value = str;
072: }
073:
074: public InputStream getAsciiStream() throws SQLException {
075: try {
076: return new ByteArrayInputStream(_value.getBytes("ASCII"));
077: } catch (UnsupportedEncodingException e) {
078: throw ExceptionConverter.convert(e);
079: }
080: }
081:
082: public Reader getCharacterStream() throws SQLException {
083: return new StringReader(_value);
084: }
085:
086: public String getSubString(long pos, int len) throws SQLException {
087: return _value.substring((int) pos, ((int) pos + len));
088: }
089:
090: public long length() throws SQLException {
091: return _value.length();
092: }
093:
094: public long position(Clob searchstr, long start)
095: throws SQLException {
096: if (null == searchstr) {
097: throw new SQLException("search string was null");
098: }
099: StringBuffer buf = new StringBuffer();
100: Reader in = searchstr.getCharacterStream();
101: try {
102: for (int c = in.read(); c != -1; c = in.read()) {
103: buf.append((char) c);
104: }
105: } catch (IOException e) {
106: throw ExceptionConverter.convert(e);
107: }
108: return position(buf.toString(), start);
109: }
110:
111: public long position(String searchstr, long start)
112: throws SQLException {
113: if (null == searchstr) {
114: throw new SQLException("search string was null");
115: }
116: return (_value.indexOf(searchstr, (int) start));
117: }
118:
119: public OutputStream setAsciiStream(long pos) throws SQLException {
120: throw new SQLException("Unsupported");
121: }
122:
123: public Writer setCharacterStream(long pos) throws SQLException {
124: throw new SQLException("Unsupported");
125: }
126:
127: public int setString(long pos, String str) throws SQLException {
128: throw new SQLException("Unsupported");
129: }
130:
131: public int setString(long pos, String str, int offset, int length)
132: throws SQLException {
133: throw new SQLException("Unsupported");
134: }
135:
136: public String getString() {
137: return _value;
138: }
139:
140: public void truncate(long length) throws SQLException {
141: throw new SQLException("Unsupported.");
142: }
143:
144: private String _value = null;
145:
146: @Override
147: public void free() throws SQLException {
148: throw new SQLException("Not supported");
149: }
150:
151: @Override
152: public Reader getCharacterStream(long arg0, long arg1)
153: throws SQLException {
154: throw new SQLException("Not supported");
155: }
156: }
|