001: /**
002: * com.mckoi.database.jdbc.MStreamableBlob 22 Jan 2003
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database.jdbc;
024:
025: import java.io.*;
026: import java.sql.Blob;
027: import java.sql.SQLException;
028:
029: /**
030: * A Blob that is a large object that may be streamed from the server directly
031: * to this object. A blob that is streamable is only alive for the lifetime of
032: * the result set it is part of. If the underlying result set that contains
033: * this streamable blob is closed then this blob is no longer valid.
034: *
035: * @author Tobias Downer
036: */
037:
038: class MStreamableBlob extends AbstractStreamableObject implements Blob {
039:
040: /**
041: * Constructs the blob.
042: */
043: MStreamableBlob(MConnection connection, int result_set_id,
044: byte type, long streamable_object_id, long size) {
045: super (connection, result_set_id, type, streamable_object_id,
046: size);
047: }
048:
049: // ---------- Implemented from Blob ----------
050:
051: public long length() throws SQLException {
052: return rawSize();
053: }
054:
055: public byte[] getBytes(long pos, int length) throws SQLException {
056: // First byte is at position 1 according to JDBC Spec.
057: --pos;
058: if (pos < 0 || pos + length > length()) {
059: throw new SQLException("Out of bounds.");
060: }
061:
062: // The buffer we are reading into
063: byte[] buf = new byte[length];
064: InputStream i_stream = getBinaryStream();
065: try {
066: i_stream.skip(pos);
067: for (int i = 0; i < length; ++i) {
068: buf[i] = (byte) i_stream.read();
069: }
070: } catch (IOException e) {
071: e.printStackTrace(System.err);
072: throw new SQLException("IO Error: " + e.getMessage());
073: }
074:
075: return buf;
076: }
077:
078: public InputStream getBinaryStream() throws SQLException {
079: return new StreamableObjectInputStream(rawSize());
080: }
081:
082: public long position(byte[] pattern, long start)
083: throws SQLException {
084: throw MSQLException.unsupported();
085: }
086:
087: public long position(Blob pattern, long start) throws SQLException {
088: throw MSQLException.unsupported();
089: }
090:
091: //#IFDEF(JDBC3.0)
092:
093: // -------------------------- JDBC 3.0 -----------------------------------
094:
095: public int setBytes(long pos, byte[] bytes) throws SQLException {
096:
097: throw MSQLException.unsupported();
098: }
099:
100: public int setBytes(long pos, byte[] bytes, int offset, int len)
101: throws SQLException {
102: throw MSQLException.unsupported();
103: }
104:
105: public java.io.OutputStream setBinaryStream(long pos)
106: throws SQLException {
107: throw MSQLException.unsupported();
108: }
109:
110: public void truncate(long len) throws SQLException {
111: throw MSQLException.unsupported();
112: }
113:
114: //#ENDIF
115:
116: }
|