KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbc > MStreamableBlob


1 /**
2  * com.mckoi.database.jdbc.MStreamableBlob 22 Jan 2003
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.jdbc;
26
27 import java.io.*;
28 import java.sql.Blob JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 /**
32  * A Blob that is a large object that may be streamed from the server directly
33  * to this object. A blob that is streamable is only alive for the lifetime of
34  * the result set it is part of. If the underlying result set that contains
35  * this streamable blob is closed then this blob is no longer valid.
36  *
37  * @author Tobias Downer
38  */

39
40 class MStreamableBlob extends AbstractStreamableObject implements Blob JavaDoc {
41
42   /**
43    * Constructs the blob.
44    */

45   MStreamableBlob(MConnection connection, int result_set_id, byte type,
46                  long streamable_object_id, long size) {
47     super(connection, result_set_id, type, streamable_object_id, size);
48   }
49
50   // ---------- Implemented from Blob ----------
51

52   public long length() throws SQLException JavaDoc {
53     return rawSize();
54   }
55
56   public byte[] getBytes(long pos, int length) throws SQLException JavaDoc {
57     // First byte is at position 1 according to JDBC Spec.
58
--pos;
59     if (pos < 0 || pos + length > length()) {
60       throw new SQLException JavaDoc("Out of bounds.");
61     }
62
63     // The buffer we are reading into
64
byte[] buf = new byte[length];
65     InputStream i_stream = getBinaryStream();
66     try {
67       i_stream.skip(pos);
68       for (int i = 0; i < length; ++i) {
69         buf[i] = (byte) i_stream.read();
70       }
71     }
72     catch (IOException e) {
73       e.printStackTrace(System.err);
74       throw new SQLException JavaDoc("IO Error: " + e.getMessage());
75     }
76
77     return buf;
78   }
79
80   public InputStream getBinaryStream() throws SQLException JavaDoc {
81     return new StreamableObjectInputStream(rawSize());
82   }
83
84   public long position(byte[] pattern, long start) throws SQLException JavaDoc {
85     throw MSQLException.unsupported();
86   }
87
88   public long position(Blob JavaDoc pattern, long start) throws SQLException JavaDoc {
89     throw MSQLException.unsupported();
90   }
91
92   //#IFDEF(JDBC3.0)
93

94   // -------------------------- JDBC 3.0 -----------------------------------
95

96   public int setBytes(long pos, byte[] bytes) throws SQLException JavaDoc {
97     
98     throw MSQLException.unsupported();
99   }
100
101   public int setBytes(long pos, byte[] bytes, int offset, int len)
102                                                         throws SQLException JavaDoc {
103     throw MSQLException.unsupported();
104   }
105
106   public java.io.OutputStream JavaDoc setBinaryStream(long pos) throws SQLException JavaDoc {
107     throw MSQLException.unsupported();
108   }
109
110   public void truncate(long len) throws SQLException JavaDoc {
111     throw MSQLException.unsupported();
112   }
113
114   //#ENDIF
115

116 }
117
118
Popular Tags