KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > jdbc > JdbcBlob


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.jdbc;
6
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.OutputStream JavaDoc;
10 import java.sql.Blob JavaDoc;
11 import java.sql.SQLException JavaDoc;
12
13 import org.h2.engine.Constants;
14 import org.h2.engine.SessionInterface;
15 import org.h2.message.Message;
16 import org.h2.message.TraceObject;
17 import org.h2.util.IOUtils;
18 import org.h2.value.Value;
19
20 /**
21  * Represents a BLOB value.
22  */

23 public class JdbcBlob extends TraceObject implements Blob JavaDoc {
24
25     private Value value;
26     private JdbcConnection conn;
27
28     /**
29      * INTERNAL
30      */

31     public JdbcBlob(SessionInterface session, JdbcConnection conn, Value value, int id) {
32         setTrace(session.getTrace(), TraceObject.BLOB, id);
33         this.conn = conn;
34         this.value = value;
35     }
36
37     /**
38      * Returns the length.
39      *
40      * @return the length
41      */

42     public long length() throws SQLException JavaDoc {
43         try {
44             debugCodeCall("length");
45             checkClosed();
46             if(value.getType() == Value.BLOB) {
47                 long precision = value.getPrecision();
48                 if(precision > 0) {
49                     return precision;
50                 }
51             }
52             long size = 0;
53             InputStream JavaDoc in = value.getInputStream();
54             try {
55                 byte[] buff = new byte[Constants.FILE_BLOCK_SIZE];
56                 while(true) {
57                     int len = in.read(buff, 0, Constants.FILE_BLOCK_SIZE);
58                     if(len <= 0) {
59                         break;
60                     }
61                     size += len;
62                 }
63             } finally {
64                 in.close();
65             }
66             return size;
67         } catch(Throwable JavaDoc e) {
68             throw Message.convert(e);
69         }
70     }
71
72     /**
73      * Truncates the object.
74      *
75      * @throws SQLException Unsupported Feature (SQL State 0A000)
76      */

77     public void truncate(long len) throws SQLException JavaDoc {
78         debugCodeCall("truncate", len);
79         throw Message.getUnsupportedException();
80     }
81
82     /**
83      * Returns some bytes of the object.
84      *
85      * @param pos the index, the first byte is at position 1
86      * @param length the number of bytes
87      * @return the bytes, at most length bytes
88      */

89     public byte[] getBytes(long pos, int length) throws SQLException JavaDoc {
90         try {
91             debugCode("getBytes("+pos+", "+length+");");
92             checkClosed();
93             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
94             InputStream JavaDoc in = value.getInputStream();
95             try {
96                 IOUtils.skipFully(in, pos - 1);
97                 while(length > 0) {
98                     int x = in.read();
99                     if(x<0) {
100                         break;
101                     }
102                     out.write(x);
103                     length--;
104                 }
105             } finally {
106                 in.close();
107             }
108             return out.toByteArray();
109         } catch(Throwable JavaDoc e) {
110             throw Message.convert(e);
111         }
112     }
113
114     /**
115      * Sets some bytes of the object.
116      *
117      * @throws SQLException Unsupported Feature (SQL State 0A000)
118      */

119     public int setBytes(long pos, byte[] bytes) throws SQLException JavaDoc {
120         debugCode("setBytes("+pos+", bytes);");
121         throw Message.getUnsupportedException();
122     }
123
124     /**
125      * Sets some bytes of the object.
126      *
127      * @throws SQLException Unsupported Feature (SQL State 0A000)
128      */

129     public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException JavaDoc {
130         debugCode("setBytes("+pos+", bytes, "+offset+", "+len+");");
131         throw Message.getUnsupportedException();
132     }
133
134     /**
135      * Returns the input stream.
136      *
137      * @return the input stream
138      */

139     public InputStream JavaDoc getBinaryStream() throws SQLException JavaDoc {
140         debugCodeCall("getBinaryStream");
141         return value.getInputStream();
142     }
143
144     /**
145      * Returns an output stream.
146      *
147      * @throws SQLException Unsupported Feature (SQL State 0A000)
148      */

149     public OutputStream JavaDoc setBinaryStream(long pos) throws SQLException JavaDoc {
150         debugCodeCall("setBinaryStream", pos);
151         throw Message.getUnsupportedException();
152     }
153
154     /**
155      * Searches a pattern and return the position.
156      *
157      * @throws SQLException Unsupported Feature (SQL State 0A000)
158      */

159     public long position(byte[] pattern, long start) throws SQLException JavaDoc {
160         debugCode("position(pattern, "+start+");");
161         throw Message.getUnsupportedException();
162         // TODO test
163
// *
164
// * @param pattern the pattern to search
165
// * @param start the index, the first byte is at position 1
166
// * @return the position (first byte is at position 1), or -1 for not found
167
// try {
168
// debugCode("position(pattern, "+start+");");
169
// if(pattern == null) {
170
// return -1;
171
// }
172
// if(pattern.length == 0) {
173
// return 1;
174
// }
175
// // TODO performance: blob pattern search is slow
176
// BufferedInputStream in = new BufferedInputStream(value.getInputStream());
177
// IOUtils.skipFully(in, start - 1);
178
// int pos = 0;
179
// int patternPos = 0;
180
// while(true) {
181
// int x = in.read();
182
// if(x<0) {
183
// break;
184
// }
185
// if(x == (pattern[patternPos] & 0xff)) {
186
// if(patternPos == 0) {
187
// in.mark(pattern.length);
188
// }
189
// if(patternPos == pattern.length) {
190
// return pos - patternPos;
191
// }
192
// patternPos++;
193
// } else {
194
// if(patternPos > 0) {
195
// in.reset();
196
// pos -= patternPos;
197
// }
198
// }
199
// pos++;
200
// }
201
// return -1;
202
// } catch(Throwable e) {
203
// throw Message.convert(e);
204
// }
205
}
206
207     /**
208      * Searches a pattern and return the position.
209      *
210      * @throws SQLException Unsupported Feature (SQL State 0A000)
211      */

212     public long position(Blob JavaDoc blobPattern, long start) throws SQLException JavaDoc {
213       debugCode("position(blobPattern, "+start+");");
214       throw Message.getUnsupportedException();
215
216 // *
217
// * @param pattern the pattern to search
218
// * @param start the index, the first byte is at position 1
219
// * @return the position (first byte is at position 1), or -1 for not found
220
// try {
221
// debugCode("position(blobPattern, "+start+");");
222
// if(blobPattern == null) {
223
// return -1;
224
// }
225
// ByteArrayOutputStream out = new ByteArrayOutputStream();
226
// InputStream in = blobPattern.getBinaryStream();
227
// while(true) {
228
// int x = in.read();
229
// if(x < 0) {
230
// break;
231
// }
232
// out.write(x);
233
// }
234
// return position(out.toByteArray(), start);
235
// } catch(Throwable e) {
236
// throw Message.convert(e);
237
// }
238
}
239
240     /**
241      * Release all resources of this object.
242      */

243     public void free() throws SQLException JavaDoc {
244         debugCodeCall("free");
245         value = null;
246     }
247
248     /**
249      * Returns the input stream, starting from an offset.
250      *
251      * @throws SQLException Unsupported Feature (SQL State 0A000)
252      */

253     public InputStream JavaDoc getBinaryStream(long pos, long length) throws SQLException JavaDoc {
254         debugCode("getBinaryStream("+pos+", "+length+");");
255         throw Message.getUnsupportedException();
256     }
257
258     private void checkClosed() throws SQLException JavaDoc {
259         conn.checkClosed();
260         if (value == null) {
261             throw Message.getSQLException(Message.OBJECT_CLOSED);
262         }
263     }
264
265 }
266
Popular Tags