Java SQL ResultSet Blob Read getBlobBytes(ResultSet rs, int iColumn)

Here you can find the source of getBlobBytes(ResultSet rs, int iColumn)

Description

Gets the binary content from a column and returns it as a byte array

License

Open Source License

Parameter

Parameter Description
rs a parameter
iColumn a parameter

Exception

Parameter Description
SQLException an exception
IOException an exception

Declaration

public static byte[] getBlobBytes(ResultSet rs, int iColumn) throws SQLException, IOException 

Method Source Code

//package com.java2s;
/** ***************************************************************
Util.java/*from   w w w  . j ava2 s.  co  m*/
Copyright (C) 2001  Brendon Upson 
http://www.wnc.net.au info@wnc.net.au
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    
 *************************************************************** */

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.sql.ResultSet;

import java.sql.SQLException;

public class Main {
    /**
     * Gets the binary content from a column and returns it as a byte array
     * @param rs
     * @param iColumn
     * @return
     * @throws SQLException
     * @throws IOException
     */
    public static byte[] getBlobBytes(ResultSet rs, int iColumn) throws SQLException, IOException {
        return readStreamToByteArray(rs.getBinaryStream(iColumn));
    }

    /**
     * Gets the binary content from a column and returns it as a byte array.
     * This is primarily for Oracle because it can't deal with 
     * ResultSet.getBytes() properly - always returns 86 bytes which is apparently the blob locator
     * @param rs
     * @param sColumnName
     * @return
     * @throws SQLException
     * @throws IOException
     */
    public static byte[] getBlobBytes(ResultSet rs, String sColumnName) throws SQLException, IOException {
        return readStreamToByteArray(rs.getBinaryStream(sColumnName));
    }

    /**
     * 
     * @param is
     * @return
     * @throws IOException
     */
    public static byte[] readStreamToByteArray(InputStream is) throws IOException {
        if (is == null)
            return null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); // ~ 2k
        byte[] buf = new byte[1024];
        try {
            for (;;) {
                int dataSize = is.read(buf);
                if (dataSize == -1)
                    break;
                baos.write(buf, 0, dataSize);
            }
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ex) {
                }
            }
        }
        return baos.toByteArray();
    }
}

Related

  1. getBlob(ResultSet results, int parameterIndex)
  2. getBlobAsByteArray(final ResultSet rs, final int ind)
  3. getBlobAsBytes(ResultSet rs, int col)
  4. getBlobValue(ResultSet result, String strField)
  5. readBlob(ResultSet rs, int index)
  6. readBlobUTF16BinaryStream(ResultSet rs, String fieldName)
  7. readBlobUTF16BinaryStream(ResultSet rs, String fieldName)