Here you can find the source of getBlobBytes(ResultSet rs, int iColumn)
Parameter | Description |
---|---|
rs | a parameter |
iColumn | a parameter |
Parameter | Description |
---|---|
SQLException | an exception |
IOException | an exception |
public static byte[] getBlobBytes(ResultSet rs, int iColumn) throws SQLException, IOException
//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(); } }