1/*
2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package java.sql;
27
28import java.io.InputStream;
29
30/**
31 * The representation (mapping) in
32 * the Java™ programming language of an SQL
33 * {@code BLOB} value.  An SQL {@code BLOB} is a built-in type
34 * that stores a Binary Large Object as a column value in a row of
35 * a database table. By default drivers implement {@code Blob} using
36 * an SQL {@code locator(BLOB)}, which means that a
37 * {@code Blob} object contains a logical pointer to the
38 * SQL {@code BLOB} data rather than the data itself.
39 * A {@code Blob} object is valid for the duration of the
40 * transaction in which is was created.
41 *
42 * <P>Methods in the interfaces {@link ResultSet},
43 * {@link CallableStatement}, and {@link PreparedStatement}, such as
44 * {@code getBlob} and {@code setBlob} allow a programmer to
45 * access an SQL {@code BLOB} value.
46 * The {@code Blob} interface provides methods for getting the
47 * length of an SQL {@code BLOB} (Binary Large Object) value,
48 * for materializing a {@code BLOB} value on the client, and for
49 * determining the position of a pattern of bytes within a
50 * {@code BLOB} value. In addition, this interface has methods for updating
51 * a {@code BLOB} value.
52 * <p>
53 * All methods on the {@code Blob} interface must be fully implemented if the
54 * JDBC driver supports the data type.
55 *
56 * @since 1.2
57 */
58
59public interface Blob {
60
61  /**
62   * Returns the number of bytes in the {@code BLOB} value
63   * designated by this {@code Blob} object.
64   *
65   * @return length of the {@code BLOB} in bytes
66   * @exception SQLException if there is an error accessing the
67   *            length of the {@code BLOB}
68   * @exception SQLFeatureNotSupportedException if the JDBC driver
69   *            does not support this method
70   * @since 1.2
71   */
72  long length() throws SQLException;
73
74  /**
75   * Retrieves all or part of the {@code BLOB}
76   * value that this {@code Blob} object represents, as an array of
77   * bytes.  This {@code byte} array contains up to {@code length}
78   * consecutive bytes starting at position {@code pos}.
79   *
80   * @param pos the ordinal position of the first byte in the
81   *        {@code BLOB} value to be extracted; the first byte is at
82   *        position 1
83   * @param length the number of consecutive bytes to be copied; the value
84   *        for length must be 0 or greater
85   * @return a byte array containing up to {@code length}
86   *         consecutive bytes from the {@code BLOB} value designated
87   *         by this {@code Blob} object, starting with the
88   *         byte at position {@code pos}
89   * @exception SQLException if there is an error accessing the
90   *            {@code BLOB} value; if pos is less than 1 or length is
91   *            less than 0
92   * @exception SQLFeatureNotSupportedException if the JDBC driver
93   *            does not support this method
94   * @see #setBytes
95   * @since 1.2
96   */
97  byte[] getBytes(long pos, int length) throws SQLException;
98
99  /**
100   * Retrieves the {@code BLOB} value designated by this
101   * {@code Blob} instance as a stream.
102   *
103   * @return a stream containing the {@code BLOB} data
104   * @exception SQLException if there is an error accessing the
105   *            {@code BLOB} value
106   * @exception SQLFeatureNotSupportedException if the JDBC driver
107   *            does not support this method
108   * @see #setBinaryStream
109   * @since 1.2
110   */
111  java.io.InputStream getBinaryStream () throws SQLException;
112
113  /**
114   * Retrieves the byte position at which the specified byte array
115   * {@code pattern} begins within the {@code BLOB}
116   * value that this {@code Blob} object represents.
117   * The search for {@code pattern} begins at position
118   * {@code start}.
119   *
120   * @param pattern the byte array for which to search
121   * @param start the position at which to begin searching; the
122   *        first position is 1
123   * @return the position at which the pattern appears, else -1
124   * @exception SQLException if there is an error accessing the
125   *            {@code BLOB} or if start is less than 1
126   * @exception SQLFeatureNotSupportedException if the JDBC driver
127   *            does not support this method
128   * @since 1.2
129   */
130  long position(byte pattern[], long start) throws SQLException;
131
132  /**
133   * Retrieves the byte position in the {@code BLOB} value
134   * designated by this {@code Blob} object at which
135   * {@code pattern} begins.  The search begins at position
136   * {@code start}.
137   *
138   * @param pattern the {@code Blob} object designating
139   *        the {@code BLOB} value for which to search
140   * @param start the position in the {@code BLOB} value
141   *        at which to begin searching; the first position is 1
142   * @return the position at which the pattern begins, else -1
143   * @exception SQLException if there is an error accessing the
144   *            {@code BLOB} value or if start is less than 1
145   * @exception SQLFeatureNotSupportedException if the JDBC driver
146   *            does not support this method
147   * @since 1.2
148   */
149  long position(Blob pattern, long start) throws SQLException;
150
151    // -------------------------- JDBC 3.0 -----------------------------------
152
153    /**
154     * Writes the given array of bytes to the {@code BLOB} value that
155     * this {@code Blob} object represents, starting at position
156     * {@code pos}, and returns the number of bytes written.
157     * The array of bytes will overwrite the existing bytes
158     * in the {@code Blob} object starting at the position
159     * {@code pos}.  If the end of the {@code Blob} value is reached
160     * while writing the array of bytes, then the length of the {@code Blob}
161     * value will be increased to accommodate the extra bytes.
162     * <p>
163     * <b>Note:</b> If the value specified for {@code pos}
164     * is greater than the length+1 of the {@code BLOB} value then the
165     * behavior is undefined. Some JDBC drivers may throw an
166     * {@code SQLException} while other drivers may support this
167     * operation.
168     *
169     * @param pos the position in the {@code BLOB} object at which
170     *        to start writing; the first position is 1
171     * @param bytes the array of bytes to be written to the {@code BLOB}
172     *        value that this {@code Blob} object represents
173     * @return the number of bytes written
174     * @exception SQLException if there is an error accessing the
175     *            {@code BLOB} value or if pos is less than 1
176     * @exception SQLFeatureNotSupportedException if the JDBC driver
177     *            does not support this method
178     * @see #getBytes
179     * @since 1.4
180     */
181    int setBytes(long pos, byte[] bytes) throws SQLException;
182
183    /**
184     * Writes all or part of the given {@code byte} array to the
185     * {@code BLOB} value that this {@code Blob} object represents
186     * and returns the number of bytes written.
187     * Writing starts at position {@code pos} in the {@code BLOB}
188     * value; {@code len} bytes from the given byte array are written.
189     * The array of bytes will overwrite the existing bytes
190     * in the {@code Blob} object starting at the position
191     * {@code pos}.  If the end of the {@code Blob} value is reached
192     * while writing the array of bytes, then the length of the {@code Blob}
193     * value will be increased to accommodate the extra bytes.
194     * <p>
195     * <b>Note:</b> If the value specified for {@code pos}
196     * is greater than the length+1 of the {@code BLOB} value then the
197     * behavior is undefined. Some JDBC drivers may throw an
198     * {@code SQLException} while other drivers may support this
199     * operation.
200     *
201     * @param pos the position in the {@code BLOB} object at which
202     *        to start writing; the first position is 1
203     * @param bytes the array of bytes to be written to this {@code BLOB}
204     *        object
205     * @param offset the offset into the array {@code bytes} at which
206     *        to start reading the bytes to be set
207     * @param len the number of bytes to be written to the {@code BLOB}
208     *        value from the array of bytes {@code bytes}
209     * @return the number of bytes written
210     * @exception SQLException if there is an error accessing the
211     *            {@code BLOB} value or if pos is less than 1
212     * @exception SQLFeatureNotSupportedException if the JDBC driver
213     *            does not support this method
214     * @see #getBytes
215     * @since 1.4
216     */
217    int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException;
218
219    /**
220     * Retrieves a stream that can be used to write to the {@code BLOB}
221     * value that this {@code Blob} object represents.  The stream begins
222     * at position {@code pos}.
223     * The  bytes written to the stream will overwrite the existing bytes
224     * in the {@code Blob} object starting at the position
225     * {@code pos}.  If the end of the {@code Blob} value is reached
226     * while writing to the stream, then the length of the {@code Blob}
227     * value will be increased to accommodate the extra bytes.
228     * <p>
229     * <b>Note:</b> If the value specified for {@code pos}
230     * is greater than the length+1 of the {@code BLOB} value then the
231     * behavior is undefined. Some JDBC drivers may throw an
232     * {@code SQLException} while other drivers may support this
233     * operation.
234     *
235     * @param pos the position in the {@code BLOB} value at which
236     *        to start writing; the first position is 1
237     * @return a {@code java.io.OutputStream} object to which data can
238     *         be written
239     * @exception SQLException if there is an error accessing the
240     *            {@code BLOB} value or if pos is less than 1
241     * @exception SQLFeatureNotSupportedException if the JDBC driver
242     *            does not support this method
243     * @see #getBinaryStream
244     * @since 1.4
245     */
246    java.io.OutputStream setBinaryStream(long pos) throws SQLException;
247
248    /**
249     * Truncates the {@code BLOB} value that this {@code Blob}
250     * object represents to be {@code len} bytes in length.
251     * <p>
252     * <b>Note:</b> If the value specified for {@code pos}
253     * is greater than the length+1 of the {@code BLOB} value then the
254     * behavior is undefined. Some JDBC drivers may throw an
255     * {@code SQLException} while other drivers may support this
256     * operation.
257     *
258     * @param len the length, in bytes, to which the {@code BLOB} value
259     *        that this {@code Blob} object represents should be truncated
260     * @exception SQLException if there is an error accessing the
261     *            {@code BLOB} value or if len is less than 0
262     * @exception SQLFeatureNotSupportedException if the JDBC driver
263     *            does not support this method
264     * @since 1.4
265     */
266    void truncate(long len) throws SQLException;
267
268    /**
269     * This method frees the {@code Blob} object and releases the resources that
270     * it holds. The object is invalid once the {@code free}
271     * method is called.
272     * <p>
273     * After {@code free} has been called, any attempt to invoke a
274     * method other than {@code free} will result in an {@code SQLException}
275     * being thrown.  If {@code free} is called multiple times, the subsequent
276     * calls to {@code free} are treated as a no-op.
277     *
278     * @throws SQLException if an error occurs releasing
279     *         the Blob's resources
280     * @exception SQLFeatureNotSupportedException if the JDBC driver
281     *            does not support this method
282     * @since 1.6
283     */
284    void free() throws SQLException;
285
286    /**
287     * Returns an {@code InputStream} object that contains
288     * a partial {@code Blob} value, starting with the byte
289     * specified by pos, which is length bytes in length.
290     *
291     * @param pos the offset to the first byte of the partial value to be
292     *        retrieved. The first byte in the {@code Blob} is at position 1.
293     * @param length the length in bytes of the partial value to be retrieved
294     * @return {@code InputStream} through which
295     *         the partial {@code Blob} value can be read.
296     * @throws SQLException if pos is less than 1 or if pos is greater
297     *         than the number of bytes in the {@code Blob} or if
298     *         pos + length is greater than the number of bytes
299     *         in the {@code Blob}
300     *
301     * @exception SQLFeatureNotSupportedException if the JDBC driver
302     *            does not support this method
303     * @since 1.6
304     */
305    InputStream getBinaryStream(long pos, long length) throws SQLException;
306}
307