001: /*
002: * $Id: ClobSource.java,v 1.6 2007/11/13 19:04:02 rwald Exp $
003: * =======================================================================
004: * Copyright (c) 2002 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.BufferedReader;
044: import java.io.InputStream;
045: import java.io.InputStreamReader;
046: import java.io.OutputStream;
047: import java.io.Reader;
048: import java.io.UnsupportedEncodingException;
049: import java.sql.SQLException;
050:
051: import org.axiondb.AxionException;
052: import org.axiondb.jdbc.AbstractAxionClob;
053: import org.axiondb.util.ExceptionConverter;
054: import org.axiondb.util.Utf8InputStreamConverter;
055:
056: /**
057: * An {@link Clob} wrapping a {@link LobSource}.
058: * @version $Revision: 1.6 $ $Date: 2007/11/13 19:04:02 $
059: * @author Rodney Waldhoff
060: */
061: public class ClobSource extends AbstractAxionClob {
062: public ClobSource(LobSource lob) {
063: setLobSource(lob);
064: }
065:
066: public void setLobSource(LobSource lob) {
067: _lob = lob;
068: }
069:
070: protected InputStream getUtf8Stream() throws AxionException {
071: return _lob.getInputStream();
072: }
073:
074: /**
075: * This method is guaranteed to return an ASCII strream.
076: * The stream returned will throw an IOException
077: * if it encounters non-ASCII characters
078: * in the underlying Clob.
079: */
080: public InputStream getAsciiStream() throws SQLException {
081: try {
082: return new Utf8InputStreamConverter(getUtf8Stream(),
083: "US-ASCII");
084: } catch (AxionException e) {
085: throw ExceptionConverter.convert(e);
086: } catch (UnsupportedEncodingException e) {
087: throw new SQLException(e.toString());
088: }
089: }
090:
091: public Reader getCharacterStream() throws SQLException {
092: try {
093: return new BufferedReader(new InputStreamReader(
094: getUtf8Stream(), "UTF8"));
095: } catch (UnsupportedEncodingException e) {
096: throw new SQLException(e.toString());
097: } catch (AxionException e) {
098: throw ExceptionConverter.convert(e);
099: }
100: }
101:
102: /**
103: * The behaviour of this method is undefined for non-ASCII
104: * input. Make sure to limit input to ASCII only.
105: */
106: public OutputStream setAsciiStream(long pos) throws SQLException {
107: return setUtf8Stream(pos);
108: }
109:
110: public OutputStream setUtf8Stream(long pos) throws SQLException {
111: try {
112: return _lob.setOutputStream(pos);
113: } catch (AxionException e) {
114: throw ExceptionConverter.convert(e);
115: }
116: }
117:
118: public long length() throws SQLException {
119: try {
120: return _lob.length();
121: } catch (AxionException e) {
122: throw ExceptionConverter.convert(e);
123: }
124: }
125:
126: public void truncate(long length) throws SQLException {
127: try {
128: _lob.truncate(length);
129: } catch (AxionException e) {
130: throw ExceptionConverter.convert(e);
131: }
132: }
133:
134: private LobSource _lob = null;
135:
136: @Override
137: public void free() throws SQLException {
138: throw new SQLException("Not supported");
139: }
140:
141: @Override
142: public Reader getCharacterStream(long arg0, long arg1)
143: throws SQLException {
144: throw new SQLException("Not supported");
145: }
146: }
|