nizhikov commented on code in PR #11518: URL: https://github.com/apache/ignite/pull/11518#discussion_r1837669918
########## modules/core/src/main/java/org/apache/ignite/internal/jdbc2/JdbcBlob.java: ########## @@ -18,54 +18,79 @@ package org.apache.ignite.internal.jdbc2; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.util.Arrays; -import org.apache.ignite.internal.util.typedef.internal.U; /** - * Simple BLOB implementation. Actually there is no such entity as BLOB in Ignite. So using arrays is preferable way + * Simple BLOB implementation. Actually there is no such entity as BLOB in Ignite. So using arrays is a preferable way * to work with binary objects. * - * This implementation can be useful for reading binary fields of objects through JDBC. + * <p>This implementation can be useful for writting and reading binary fields of objects through JDBC. */ public class JdbcBlob implements Blob { - /** Byte array. */ - private byte[] arr; + /** Buffer to store actial data. */ + private JdbcBinaryBuffer buf; /** + * Create empty Blob. + * + * <p>It's supposed to be called when client application creates Blob calling the + * {@link java.sql.Connection#createBlob}. + */ + public JdbcBlob() { + buf = JdbcBinaryBuffer.createReadWrite(); + } + + /** + * Create Blob which wraps the existing data stored in the buffer. + * + * <p>It's supposed to be called to create Blob for query result in the {@link java.sql.ResultSet}. + * + * @param buf Existing buffer with data. + */ + public JdbcBlob(JdbcBinaryBuffer buf) { + this.buf = buf; + } + + /** + * Create Blob which wraps the existing byte array. + * * @param arr Byte array. */ public JdbcBlob(byte[] arr) { - this.arr = arr; + this(JdbcBinaryBuffer.createReadWrite(arr)); } /** {@inheritDoc} */ @Override public long length() throws SQLException { ensureNotClosed(); - return arr.length; + return buf.length(); } /** {@inheritDoc} */ @Override public byte[] getBytes(long pos, int len) throws SQLException { ensureNotClosed(); - if (pos < 1 || (arr.length - pos < 0 && arr.length > 0) || len < 0) + if (pos < 1 || (buf.length() - pos < 0 && buf.length() > 0) || len < 0) { throw new SQLException("Invalid argument. Position can't be less than 1 or " + - "greater than size of underlying byte array. Requested length also can't be negative " + "" + - "[pos=" + pos + ", len=" + len + ']'); + "greater than Blob length. Requested length also can't be negative " + + "[pos=" + pos + ", len=" + len + ", blobLen=" + buf.length() + "]"); + } int idx = (int)(pos - 1); - int size = len > arr.length - idx ? arr.length - idx : len; + int size = len > buf.length() - idx ? buf.length() - idx : len; Review Comment: int size = Math.min(len, buf.length() - idx); -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org