Example usage for java.sql Blob getBinaryStream

List of usage examples for java.sql Blob getBinaryStream

Introduction

In this page you can find the example usage for java.sql Blob getBinaryStream.

Prototype

InputStream getBinaryStream(long pos, long length) throws SQLException;

Source Link

Document

Returns an InputStream object that contains a partial Blob value, starting with the byte specified by pos, which is length bytes in length.

Usage

From source file:com.butler.service.ProductDao.java

public InputStream getImage(String product) {
    String sql = "select pic from product where name='" + product + "'";
    return (InputStream) this.getJdbcTemplate().query(sql, new ResultSetExtractor() {
        public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
            rs.next();//from   w ww  .ja v  a  2s  .c  o m
            Blob imageBlob = rs.getBlob("pic");
            InputStream binaryStream = imageBlob.getBinaryStream(1, imageBlob.length());
            return binaryStream;
        }
    });

}

From source file:com.orangeandbronze.jblubble.jdbc.springframework.SpringJdbcBlobstoreService.java

protected void serveBlobInternal(BlobKey blobKey, OutputStream out, long start, long end, boolean useEnd) {
    try {//from  w ww .  j  a v a2  s  .c om
        jdbcTemplate.query(getSelectContentByIdSql(), (rs) -> {
            if (!rs.next()) {
                throw new BlobstoreException("Blob not found: " + blobKey);
            }
            Blob blob = rs.getBlob("content");
            try {
                long pos = start + 1;
                long length = useEnd ? (end - start + 1) : blob.length();
                try (InputStream in = new BufferedInputStream(blob.getBinaryStream(pos, length),
                        getBufferSize())) {
                    copy(in, out);
                } catch (IOException ioe) {
                    throw new BlobstoreException("Error while reading blob", ioe);
                }
                return blob.length();
            } finally {
                blob.free();
            }
        }, Long.valueOf(blobKey.stringValue()));
    } catch (DataAccessException e) {
        throw new BlobstoreException(e);
    }
}

From source file:com.orangeandbronze.jblubble.jdbc.JdbcBlobstoreService.java

protected void serveBlobInternal(BlobKey blobKey, OutputStream out, long start, long end, boolean useEnd)
        throws IOException, BlobstoreException {
    readBlobInternal(blobKey, new BlobHandler() {
        @Override/*from  w w w .j  a v a 2s .c  o  m*/
        public void handleBlob(Blob blob) throws SQLException, IOException {
            long pos = start + 1; // for java.sql.Blob the first byte is at position 1
            long length = useEnd ? (end - start + 1) : blob.length();
            try (InputStream in = new BufferedInputStream(blob.getBinaryStream(pos, length), getBufferSize())) {
                copy(in, out);
            }
        }
    });
}