Path: blob/master/src/java.sql/share/classes/java/sql/Blob.java
41153 views
/*1* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.sql;2627import java.io.InputStream;2829/**30* The representation (mapping) in31* the Java programming language of an SQL32* {@code BLOB} value. An SQL {@code BLOB} is a built-in type33* that stores a Binary Large Object as a column value in a row of34* a database table. By default drivers implement {@code Blob} using35* an SQL {@code locator(BLOB)}, which means that a36* {@code Blob} object contains a logical pointer to the37* SQL {@code BLOB} data rather than the data itself.38* A {@code Blob} object is valid for the duration of the39* transaction in which is was created.40*41* <P>Methods in the interfaces {@link ResultSet},42* {@link CallableStatement}, and {@link PreparedStatement}, such as43* {@code getBlob} and {@code setBlob} allow a programmer to44* access an SQL {@code BLOB} value.45* The {@code Blob} interface provides methods for getting the46* length of an SQL {@code BLOB} (Binary Large Object) value,47* for materializing a {@code BLOB} value on the client, and for48* determining the position of a pattern of bytes within a49* {@code BLOB} value. In addition, this interface has methods for updating50* a {@code BLOB} value.51* <p>52* All methods on the {@code Blob} interface must be fully implemented if the53* JDBC driver supports the data type.54*55* @since 1.256*/5758public interface Blob {5960/**61* Returns the number of bytes in the {@code BLOB} value62* designated by this {@code Blob} object.63*64* @return length of the {@code BLOB} in bytes65* @throws SQLException if there is an error accessing the66* length of the {@code BLOB}67* @throws SQLFeatureNotSupportedException if the JDBC driver68* does not support this method69* @since 1.270*/71long length() throws SQLException;7273/**74* Retrieves all or part of the {@code BLOB}75* value that this {@code Blob} object represents, as an array of76* bytes. This {@code byte} array contains up to {@code length}77* consecutive bytes starting at position {@code pos}.78*79* @param pos the ordinal position of the first byte in the80* {@code BLOB} value to be extracted; the first byte is at81* position 182* @param length the number of consecutive bytes to be copied; the value83* for length must be 0 or greater84* @return a byte array containing up to {@code length}85* consecutive bytes from the {@code BLOB} value designated86* by this {@code Blob} object, starting with the87* byte at position {@code pos}88* @throws SQLException if there is an error accessing the89* {@code BLOB} value; if pos is less than 1 or length is90* less than 091* @throws SQLFeatureNotSupportedException if the JDBC driver92* does not support this method93* @see #setBytes94* @since 1.295*/96byte[] getBytes(long pos, int length) throws SQLException;9798/**99* Retrieves the {@code BLOB} value designated by this100* {@code Blob} instance as a stream.101*102* @return a stream containing the {@code BLOB} data103* @throws SQLException if there is an error accessing the104* {@code BLOB} value105* @throws SQLFeatureNotSupportedException if the JDBC driver106* does not support this method107* @see #setBinaryStream108* @since 1.2109*/110java.io.InputStream getBinaryStream () throws SQLException;111112/**113* Retrieves the byte position at which the specified byte array114* {@code pattern} begins within the {@code BLOB}115* value that this {@code Blob} object represents.116* The search for {@code pattern} begins at position117* {@code start}.118*119* @param pattern the byte array for which to search120* @param start the position at which to begin searching; the121* first position is 1122* @return the position at which the pattern appears, else -1123* @throws SQLException if there is an error accessing the124* {@code BLOB} or if start is less than 1125* @throws SQLFeatureNotSupportedException if the JDBC driver126* does not support this method127* @since 1.2128*/129long position(byte pattern[], long start) throws SQLException;130131/**132* Retrieves the byte position in the {@code BLOB} value133* designated by this {@code Blob} object at which134* {@code pattern} begins. The search begins at position135* {@code start}.136*137* @param pattern the {@code Blob} object designating138* the {@code BLOB} value for which to search139* @param start the position in the {@code BLOB} value140* at which to begin searching; the first position is 1141* @return the position at which the pattern begins, else -1142* @throws SQLException if there is an error accessing the143* {@code BLOB} value or if start is less than 1144* @throws SQLFeatureNotSupportedException if the JDBC driver145* does not support this method146* @since 1.2147*/148long position(Blob pattern, long start) throws SQLException;149150// -------------------------- JDBC 3.0 -----------------------------------151152/**153* Writes the given array of bytes to the {@code BLOB} value that154* this {@code Blob} object represents, starting at position155* {@code pos}, and returns the number of bytes written.156* The array of bytes will overwrite the existing bytes157* in the {@code Blob} object starting at the position158* {@code pos}. If the end of the {@code Blob} value is reached159* while writing the array of bytes, then the length of the {@code Blob}160* value will be increased to accommodate the extra bytes.161* <p>162* <b>Note:</b> If the value specified for {@code pos}163* is greater than the length+1 of the {@code BLOB} value then the164* behavior is undefined. Some JDBC drivers may throw an165* {@code SQLException} while other drivers may support this166* operation.167*168* @param pos the position in the {@code BLOB} object at which169* to start writing; the first position is 1170* @param bytes the array of bytes to be written to the {@code BLOB}171* value that this {@code Blob} object represents172* @return the number of bytes written173* @throws SQLException if there is an error accessing the174* {@code BLOB} value or if pos is less than 1175* @throws SQLFeatureNotSupportedException if the JDBC driver176* does not support this method177* @see #getBytes178* @since 1.4179*/180int setBytes(long pos, byte[] bytes) throws SQLException;181182/**183* Writes all or part of the given {@code byte} array to the184* {@code BLOB} value that this {@code Blob} object represents185* and returns the number of bytes written.186* Writing starts at position {@code pos} in the {@code BLOB}187* value; {@code len} bytes from the given byte array are written.188* The array of bytes will overwrite the existing bytes189* in the {@code Blob} object starting at the position190* {@code pos}. If the end of the {@code Blob} value is reached191* while writing the array of bytes, then the length of the {@code Blob}192* value will be increased to accommodate the extra bytes.193* <p>194* <b>Note:</b> If the value specified for {@code pos}195* is greater than the length+1 of the {@code BLOB} value then the196* behavior is undefined. Some JDBC drivers may throw an197* {@code SQLException} while other drivers may support this198* operation.199*200* @param pos the position in the {@code BLOB} object at which201* to start writing; the first position is 1202* @param bytes the array of bytes to be written to this {@code BLOB}203* object204* @param offset the offset into the array {@code bytes} at which205* to start reading the bytes to be set206* @param len the number of bytes to be written to the {@code BLOB}207* value from the array of bytes {@code bytes}208* @return the number of bytes written209* @throws SQLException if there is an error accessing the210* {@code BLOB} value or if pos is less than 1211* @throws SQLFeatureNotSupportedException if the JDBC driver212* does not support this method213* @see #getBytes214* @since 1.4215*/216int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException;217218/**219* Retrieves a stream that can be used to write to the {@code BLOB}220* value that this {@code Blob} object represents. The stream begins221* at position {@code pos}.222* The bytes written to the stream will overwrite the existing bytes223* in the {@code Blob} object starting at the position224* {@code pos}. If the end of the {@code Blob} value is reached225* while writing to the stream, then the length of the {@code Blob}226* value will be increased to accommodate the extra bytes.227* <p>228* <b>Note:</b> If the value specified for {@code pos}229* is greater than the length+1 of the {@code BLOB} value then the230* behavior is undefined. Some JDBC drivers may throw an231* {@code SQLException} while other drivers may support this232* operation.233*234* @param pos the position in the {@code BLOB} value at which235* to start writing; the first position is 1236* @return a {@code java.io.OutputStream} object to which data can237* be written238* @throws SQLException if there is an error accessing the239* {@code BLOB} value or if pos is less than 1240* @throws SQLFeatureNotSupportedException if the JDBC driver241* does not support this method242* @see #getBinaryStream243* @since 1.4244*/245java.io.OutputStream setBinaryStream(long pos) throws SQLException;246247/**248* Truncates the {@code BLOB} value that this {@code Blob}249* object represents to be {@code len} bytes in length.250* <p>251* <b>Note:</b> If the value specified for {@code pos}252* is greater than the length+1 of the {@code BLOB} value then the253* behavior is undefined. Some JDBC drivers may throw an254* {@code SQLException} while other drivers may support this255* operation.256*257* @param len the length, in bytes, to which the {@code BLOB} value258* that this {@code Blob} object represents should be truncated259* @throws SQLException if there is an error accessing the260* {@code BLOB} value or if len is less than 0261* @throws SQLFeatureNotSupportedException if the JDBC driver262* does not support this method263* @since 1.4264*/265void truncate(long len) throws SQLException;266267/**268* This method frees the {@code Blob} object and releases the resources that269* it holds. The object is invalid once the {@code free}270* method is called.271* <p>272* After {@code free} has been called, any attempt to invoke a273* method other than {@code free} will result in an {@code SQLException}274* being thrown. If {@code free} is called multiple times, the subsequent275* calls to {@code free} are treated as a no-op.276*277* @throws SQLException if an error occurs releasing278* the Blob's resources279* @throws SQLFeatureNotSupportedException if the JDBC driver280* does not support this method281* @since 1.6282*/283void free() throws SQLException;284285/**286* Returns an {@code InputStream} object that contains287* a partial {@code Blob} value, starting with the byte288* specified by pos, which is length bytes in length.289*290* @param pos the offset to the first byte of the partial value to be291* retrieved. The first byte in the {@code Blob} is at position 1.292* @param length the length in bytes of the partial value to be retrieved293* @return {@code InputStream} through which294* the partial {@code Blob} value can be read.295* @throws SQLException if pos is less than 1 or if pos is greater296* than the number of bytes in the {@code Blob} or if297* pos + length is greater than the number of bytes298* in the {@code Blob}299*300* @throws SQLFeatureNotSupportedException if the JDBC driver301* does not support this method302* @since 1.6303*/304InputStream getBinaryStream(long pos, long length) throws SQLException;305}306307308