Debian Codegraph @

src/main/org/h2/jdbc/JdbcLob.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.IOException;
9 import java.io.InputStream;
10 import java.io.PipedInputStream;
11 import java.io.PipedOutputStream;
12 import java.io.Reader;
13 import java.io.Writer;
14 import java.sql.SQLException;
15
16 import org.h2.api.ErrorCode;
17 import org.h2.message.DbException;
18 import org.h2.message.TraceObject;
19 import org.h2.mvstore.DataUtils;
20 import org.h2.util.IOUtils;
21 import org.h2.util.Task;
22 import org.h2.value.Value;
23
24 /**
25 * Represents a large object value.
26 */
27 public abstract class JdbcLob extends TraceObject {
28
29 static final class LobPipedOutputStream extends PipedOutputStream {
30 private final Task task;
31
32 LobPipedOutputStream(PipedInputStream snk, Task task) throws IOException {
33 super(snk);
34 this.task = task;
35 }
36
37 @Override
38 public void close() throws IOException {
39 super.close();
40 try {
41 task.get();
42 } catch (Exception e) {
43 throw DataUtils.convertToIOException(e);
44 }
45 }
46 }
47
48 /**
49 * State of the object.
50 */
51 public enum State {
52 /**
53 * New object without a value.
54 */
55 NEW,
56
57 /**
58 * One of setter methods is invoked, but stream is not closed yet.
59 */
60 SET_CALLED,
61
62 /**
63 * A value is set.
64 */
65 WITH_VALUE,
66
67 /**
68 * Object is closed.
69 */
70 CLOSED;
71 }
72
73 /**
74 * JDBC connection.
75 */
76 final JdbcConnection conn;
77
78 /**
79 * Value.
80 */
81 Value value;
82
83 /**
84 * State.
85 */
86 State state;
87
88 JdbcLob(JdbcConnection conn, Value value, State state, int type, int id) {
89 setTrace(conn.getSession().getTrace(), type, id);
90 this.conn = conn;
91 this.value = value;
92 this.state = state;
93 }
94
95 /**
96 * Check that connection and LOB is not closed, otherwise throws exception with
97 * error code {@link org.h2.api.ErrorCode#OBJECT_CLOSED}.
98 */
99 void checkClosed() {
100 conn.checkClosed();
101 if (state == State.CLOSED) {
102 throw DbException.get(ErrorCode.OBJECT_CLOSED);
103 }
104 }
105
106 /**
107 * Check the state of the LOB and throws the exception when check failed
108 * (set is supported only for a new LOB).
109 */
110 void checkEditable() {
111 checkClosed();
112 if (state != State.NEW) {
113 throw DbException.getUnsupportedException("Allocate a new object to set its value.");
114 }
115 }
116
117 /**
118 * Check the state of the LOB and throws the exception when check failed
119 * (the LOB must be set completely before read).
120 *
121 * @throws SQLException on SQL exception
122 * @throws IOException on I/O exception
123 */
124 void checkReadable() throws SQLException, IOException {
125 checkClosed();
126 if (state == State.SET_CALLED) {
127 throw DbException.getUnsupportedException("Stream setter is not yet closed.");
128 }
129 }
130
131 /**
132 * Change the state LOB state (LOB value is set completely and available to read).
133 * @param blob LOB value.
134 */
135 void completeWrite(Value blob) {
136 checkClosed();
137 state = State.WITH_VALUE;
138 value = blob;
139 }
140
141 /**
142 * Release all resources of this object.
143 */
144 public void free() {
145 debugCodeCall("free");
146 state = State.CLOSED;
147 value = null;
148 }
149
150 /**
151 * Returns the input stream.
152 *
153 * @return the input stream
154 * @throws SQLException on failure
155 */
156 InputStream getBinaryStream() throws SQLException {
157 try {
158 debugCodeCall("getBinaryStream");
159 checkReadable();
160 return value.getInputStream();
161 } catch (Exception e) {
162 throw logAndConvert(e);
163 }
164 }
165
166 /**
167 * Returns the reader.
168 *
169 * @return the reader
170 * @throws SQLException on failure
171 */
172 Reader getCharacterStream() throws SQLException {
173 try {
174 debugCodeCall("getCharacterStream");
175 checkReadable();
176 return value.getReader();
177 } catch (Exception e) {
178 throw logAndConvert(e);
179 }
180 }
181
182 /**
183 * Returns the writer.
184 *
185 * @return Writer.
186 * @throws IOException If an I/O error occurs.
187 */
188 Writer setCharacterStreamImpl() throws IOException {
189 return IOUtils.getBufferedWriter(setClobOutputStreamImpl());
190 }
191
192 /**
193 * Returns the writer stream.
194 *
195 * @return Output stream..
196 * @throws IOException If an I/O error occurs.
197 */
198 LobPipedOutputStream setClobOutputStreamImpl() throws IOException {
199 // PipedReader / PipedWriter are a lot slower
200 // than PipedInputStream / PipedOutputStream
201 // (Sun/Oracle Java 1.6.0_20)
202 final PipedInputStream in = new PipedInputStream();
203 final Task task = new Task() {
204 @Override
205 public void call() {
206 completeWrite(conn.createClob(IOUtils.getReader(in), -1));
207 }
208 };
209 LobPipedOutputStream out = new LobPipedOutputStream(in, task);
210 task.execute();
211 return out;
212 }
213
214 /**
215 * INTERNAL
216 */
217 @Override
218 public String toString() {
219 StringBuilder builder = new StringBuilder().append(getTraceObjectName()).append(": ");
220 if (state == State.SET_CALLED) {
221 builder.append("<setter_in_progress>");
222 } else if (state == State.CLOSED) {
223 builder.append("<closed>");
224 } else {
225 builder.append(value.getTraceSQL());
226 }
227 return builder.toString();
228 }
229
230 }