@SuppressWarnings("resource") public ByteStreamParameter(final int type, final InputStream x, final long length) throws SQLException { this.type = type; this.length = length; final BufferedInputStream s = new BufferedInputStream(x); try { final ByteArrayOutputStream bos = new ByteArrayOutputStream((int) (length >= 0 ? length : 1024)); final byte buf[] = new byte[1024]; int br; while ((br = s.read(buf)) >= 0) { if (br > 0) { bos.write(buf, 0, br); } } this.value = bos.toByteArray(); // Adjust length to the amount of read bytes if the user provided // -1 as the length parameter if (this.length < 0) { this.length = this.value.length; } } catch (final IOException e) { throw new SQLException("InputStream conversion to byte-array failed"); } finally { StreamCloser.close(s); } }
public SerialBlob(final Blob other) throws SQLException { InputStream is = null; try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); is = other.getBinaryStream(); final byte[] buff = new byte[1024]; int len; while ((len = is.read(buff)) > 0) { baos.write(buff, 0, len); } data = baos.toByteArray(); other.free(); } catch (final IOException e) { throw new SQLException("Can't retrieve contents of Blob", e.toString()); } finally { StreamCloser.close(is); } }