001: /*
002: * $Id: BlobSource.java,v 1.4 2003/05/14 15:38:12 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.ByteArrayOutputStream;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.OutputStream;
047: import java.sql.SQLException;
048:
049: import org.axiondb.AxionException;
050: import org.axiondb.jdbc.AbstractAxionBlob;
051: import org.axiondb.util.ExceptionConverter;
052:
053: /**
054: * A {@link Blob} wrapping a {@link LobSource}.
055: *
056: * @version $Revision: 1.4 $ $Date: 2003/05/14 15:38:12 $
057: * @author Rodney Waldhoff
058: */
059: public class BlobSource extends AbstractAxionBlob {
060:
061: public BlobSource(LobSource lob) {
062: setLobSource(lob);
063: }
064:
065: public void setLobSource(LobSource lob) {
066: _lob = lob;
067: }
068:
069: public long length() throws SQLException {
070: try {
071: return _lob.length();
072: } catch (AxionException e) {
073: throw ExceptionConverter.convert(e);
074: }
075: }
076:
077: public byte[] getBytes(long pos, int length) throws SQLException {
078: if (pos < 0) {
079: throw new SQLException("position " + pos + " is invalid.");
080: }
081: if (length < 0) {
082: throw new SQLException("length" + length + " is invalid.");
083: }
084: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
085: InputStream in = null;
086: try {
087: in = getBinaryStream();
088: // skip pos bytes
089: for (long i = 0; i < pos; i++) {
090: in.read();
091: }
092: // copy length bytes
093: for (int i = 0, b = in.read(); i < length && b != -1; i++, b = in
094: .read()) {
095: buffer.write((byte) b);
096: }
097: return buffer.toByteArray();
098: } catch (IOException e) {
099: throw ExceptionConverter.convert(e);
100: } finally {
101: try {
102: in.close();
103: } catch (Exception e) {
104: }
105: }
106: }
107:
108: public void truncate(long length) throws SQLException {
109: try {
110: _lob.truncate(length);
111: } catch (AxionException e) {
112: throw ExceptionConverter.convert(e);
113: }
114: }
115:
116: public InputStream getBinaryStream() throws SQLException {
117: try {
118: return _lob.getInputStream();
119: } catch (AxionException e) {
120: throw ExceptionConverter.convert(e);
121: }
122: }
123:
124: public OutputStream setBinaryStream(long pos) throws SQLException {
125: try {
126: return _lob.setOutputStream(pos);
127: } catch (AxionException e) {
128: throw ExceptionConverter.convert(e);
129: }
130: }
131:
132: private LobSource _lob = null;
133: }
|