Debian Codegraph @

src/main/org/h2/jdbc/JdbcBlob.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.jdbc;
7
8 import java.io.BufferedInputStream;
9 import java.io.BufferedOutputStream;
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.io.PipedInputStream;
15 import java.sql.Blob;
16 import java.sql.SQLException;
17
18 import org.h2.engine.Constants;
19 import org.h2.message.DbException;
20 import org.h2.message.TraceObject;
21 import org.h2.util.IOUtils;
22 import org.h2.util.Task;
23 import org.h2.value.Value;
24
25 /**
26 * Represents a BLOB value.
27 */
28 public final class JdbcBlob extends JdbcLob implements Blob {
29
30 /**
31 * INTERNAL
32 * @param conn it belongs to
33 * @param value of
34 * @param state of the LOB
35 * @param id of the trace object
36 */
37 public JdbcBlob(JdbcConnection conn, Value value, State state, int id) {
38 super(conn, value, state, TraceObject.BLOB, id);
39 }
40
41 /**
42 * Returns the length.
43 *
44 * @return the length
45 */
46 @Override
47 public long length() throws SQLException {
48 try {
49 debugCodeCall("length");
50 checkReadable();
51 if (value.getValueType() == Value.BLOB) {
52 long precision = value.getType().getPrecision();
53 if (precision > 0) {
54 return precision;
55 }
56 }
57 return IOUtils.copyAndCloseInput(value.getInputStream(), null);
58 } catch (Exception e) {
59 throw logAndConvert(e);
60 }
61 }
62
63 /**
64 * [Not supported] Truncates the object.
65 *
66 * @param len the new length
67 */
68 @Override
69 public void truncate(long len) throws SQLException {
70 throw unsupported("LOB update");
71 }
72
73 /**
74 * Returns some bytes of the object.
75 *
76 * @param pos the index, the first byte is at position 1
77 * @param length the number of bytes
78 * @return the bytes, at most length bytes
79 */
80 @Override
81 public byte[] getBytes(long pos, int length) throws SQLException {
82 try {
83 if (isDebugEnabled()) {
84 debugCode("getBytes(" + pos + ", " + length + ')');
85 }
86 checkReadable();
87 ByteArrayOutputStream out = new ByteArrayOutputStream();
88 try (InputStream in = value.getInputStream()) {
89 IOUtils.skipFully(in, pos - 1);
90 IOUtils.copy(in, out, length);
91 }
92 return out.toByteArray();
93 } catch (Exception e) {
94 throw logAndConvert(e);
95 }
96 }
97
98 /**
99 * Fills the Blob. This is only supported for new, empty Blob objects that
100 * were created with Connection.createBlob(). The position
101 * must be 1, meaning the whole Blob data is set.
102 *
103 * @param pos where to start writing (the first byte is at position 1)
104 * @param bytes the bytes to set
105 * @return the length of the added data
106 */
107 @Override
108 public int setBytes(long pos, byte[] bytes) throws SQLException {
109 if (bytes == null) {
110 throw new NullPointerException();
111 }
112 try {
113 if (isDebugEnabled()) {
114 debugCode("setBytes(" + pos + ", " + quoteBytes(bytes) + ')');
115 }
116 checkEditable();
117 if (pos != 1) {
118 throw DbException.getInvalidValueException("pos", pos);
119 }
120 completeWrite(conn.createBlob(new ByteArrayInputStream(bytes), -1));
121 return bytes.length;
122 } catch (Exception e) {
123 throw logAndConvert(e);
124 }
125 }
126
127 /**
128 * Sets some bytes of the object.
129 *
130 * @param pos the write position
131 * @param bytes the bytes to set
132 * @param offset the bytes offset
133 * @param len the number of bytes to write
134 * @return how many bytes have been written
135 */
136 @Override
137 public int setBytes(long pos, byte[] bytes, int offset, int len)
138 throws SQLException {
139 if (bytes == null) {
140 throw new NullPointerException();
141 }
142 try {
143 if (isDebugEnabled()) {
144 debugCode("setBytes(" + pos + ", " + quoteBytes(bytes) + ", " + offset + ", " + len + ')');
145 }
146 checkEditable();
147 if (pos != 1) {
148 throw DbException.getInvalidValueException("pos", pos);
149 }
150 completeWrite(conn.createBlob(new ByteArrayInputStream(bytes, offset, len), -1));
151 return (int) value.getType().getPrecision();
152 } catch (Exception e) {
153 throw logAndConvert(e);
154 }
155 }
156
157 @Override
158 public InputStream getBinaryStream() throws SQLException {
159 return super.getBinaryStream();
160 }
161
162 /**
163 * Get a writer to update the Blob. This is only supported for new, empty
164 * Blob objects that were created with Connection.createBlob(). The Blob is
165 * created in a separate thread, and the object is only updated when
166 * OutputStream.close() is called. The position must be 1, meaning the whole
167 * Blob data is set.
168 *
169 * @param pos where to start writing (the first byte is at position 1)
170 * @return an output stream
171 */
172 @Override
173 public OutputStream setBinaryStream(long pos) throws SQLException {
174 try {
175 if (isDebugEnabled()) {
176 debugCodeCall("setBinaryStream", pos);
177 }
178 checkEditable();
179 if (pos != 1) {
180 throw DbException.getInvalidValueException("pos", pos);
181 }
182 final PipedInputStream in = new PipedInputStream();
183 final Task task = new Task() {
184 @Override
185 public void call() {
186 completeWrite(conn.createBlob(in, -1));
187 }
188 };
189 LobPipedOutputStream out = new LobPipedOutputStream(in, task);
190 task.execute();
191 state = State.SET_CALLED;
192 return new BufferedOutputStream(out);
193 } catch (Exception e) {
194 throw logAndConvert(e);
195 }
196 }
197
198 /**
199 * [Not supported] Searches a pattern and return the position.
200 *
201 * @param pattern the pattern to search
202 * @param start the index, the first byte is at position 1
203 * @return the position (first byte is at position 1), or -1 for not found
204 */
205 @Override
206 public long position(byte[] pattern, long start) throws SQLException {
207 if (isDebugEnabled()) {
208 debugCode("position(" + quoteBytes(pattern) + ", " + start + ')');
209 }
210 if (Constants.BLOB_SEARCH) {
211 try {
212 checkReadable();
213 if (pattern == null) {
214 return -1;
215 }
216 if (pattern.length == 0) {
217 return 1;
218 }
219 // TODO performance: blob pattern search is slow
220 BufferedInputStream in = new BufferedInputStream(value.getInputStream());
221 IOUtils.skipFully(in, start - 1);
222 int pos = 0;
223 int patternPos = 0;
224 while (true) {
225 int x = in.read();
226 if (x < 0) {
227 break;
228 }
229 if (x == (pattern[patternPos] & 0xff)) {
230 if (patternPos == 0) {
231 in.mark(pattern.length);
232 }
233 if (patternPos == pattern.length) {
234 return pos - patternPos;
235 }
236 patternPos++;
237 } else {
238 if (patternPos > 0) {
239 in.reset();
240 pos -= patternPos;
241 }
242 }
243 pos++;
244 }
245 return -1;
246 } catch (Exception e) {
247 throw logAndConvert(e);
248 }
249 }
250 throw unsupported("LOB search");
251 }
252
253 /**
254 * [Not supported] Searches a pattern and return the position.
255 *
256 * @param blobPattern the pattern to search
257 * @param start the index, the first byte is at position 1
258 * @return the position (first byte is at position 1), or -1 for not found
259 */
260 @Override
261 public long position(Blob blobPattern, long start) throws SQLException {
262 if (isDebugEnabled()) {
263 debugCode("position(blobPattern, " + start + ')');
264 }
265 if (Constants.BLOB_SEARCH) {
266 try {
267 checkReadable();
268 if (blobPattern == null) {
269 return -1;
270 }
271 ByteArrayOutputStream out = new ByteArrayOutputStream();
272 InputStream in = blobPattern.getBinaryStream();
273 while (true) {
274 int x = in.read();
275 if (x < 0) {
276 break;
277 }
278 out.write(x);
279 }
280 return position(out.toByteArray(), start);
281 } catch (Exception e) {
282 throw logAndConvert(e);
283 }
284 }
285 throw unsupported("LOB subset");
286 }
287
288 /**
289 * Returns the input stream, starting from an offset.
290 *
291 * @param pos where to start reading
292 * @param length the number of bytes that will be read
293 * @return the input stream to read
294 */
295 @Override
296 public InputStream getBinaryStream(long pos, long length) throws SQLException {
297 try {
298 if (isDebugEnabled()) {
299 debugCode("getBinaryStream(" + pos + ", " + length + ')');
300 }
301 checkReadable();
302 if (state == State.NEW) {
303 if (pos != 1) {
304 throw DbException.getInvalidValueException("pos", pos);
305 }
306 if (length != 0) {
307 throw DbException.getInvalidValueException("length", pos);
308 }
309 }
310 return value.getInputStream(pos, length);
311 } catch (Exception e) {
312 throw logAndConvert(e);
313 }
314 }
315
316 }