Java SQL ResultSet Byte Read getByteStreamList(ResultSet resultSet, String columnName)

Here you can find the source of getByteStreamList(ResultSet resultSet, String columnName)

Description

Returns a list of byte stream values using a given SQL result set and column name.

License

Open Source License

Parameter

Parameter Description
resultSet - Result set.
columnName - Column name.

Return

List - Column values.

Declaration

public static List<byte[]> getByteStreamList(ResultSet resultSet, String columnName)
        throws SQLException, IOException 

Method Source Code

//package com.java2s;
/*/* w  w w .j av  a 2  s . co  m*/
 * Copyright (C) 2015 Bryan W. Snipes
 * 
 * This file is part of the JDistil web application framework.
 * 
 * JDistil is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JDistil 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with JDistil.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayOutputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

import java.io.IOException;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
      Returns a list of byte stream values using a given SQL result set and column name.
      @param resultSet - Result set.
      @param columnName - Column name.
      @return List - Column values.
    */
    public static List<byte[]> getByteStreamList(ResultSet resultSet, String columnName)
            throws SQLException, IOException {

        // Initialize return value
        List<byte[]> values = null;

        if (resultSet != null && columnName != null) {

            // Create values list
            values = new ArrayList<byte[]>();

            // Populate list
            while (resultSet.next()) {
                values.add(getByteStream(resultSet, columnName));
            }
        }

        return values;
    }

    /**
      Returns a string value using a given SQL result set and column name.
      @param resultSet - Result set.
      @param columnName - Column name.
      @return byte[] - Column value.
    */
    public static byte[] getByteStream(ResultSet resultSet, String columnName) throws SQLException, IOException {

        // Initialize return value
        byte[] value = null;

        if (resultSet != null && columnName != null) {

            // Retrieve column value as input stream
            InputStream inputStream = resultSet.getBinaryStream(columnName);

            if (inputStream != null) {

                // Create output stream to store data
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                // Create buffered input stream
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

                // Create byte buffer
                byte[] buffer = new byte[1024];

                // Read first set of bytes
                int position = bufferedInputStream.read(buffer, 0, buffer.length);

                // Continue processing until all bytes are read
                while (position != -1) {

                    // Write bytes to output stream
                    outputStream.write(buffer, outputStream.size(), position);

                    // Read next set of bytes
                    position = bufferedInputStream.read(buffer, position, buffer.length);
                }

                // Set value
                value = outputStream.toByteArray();
            }
        }

        return value;
    }
}

Related

  1. getByte(ResultSet resultSet, String columnName)
  2. getByte(ResultSet rs, int column)
  3. getByteFromResultSet(ResultSet rs, String db_name)
  4. getByteList(ResultSet resultSet, String columnName)
  5. getBytes(ResultSet res, String name)