Here you can find the source of readBytes(Blob blob, byte[] defaultValue)
private static byte[] readBytes(Blob blob, byte[] defaultValue) throws SQLException, IOException
//package com.java2s; /*//from w ww .j a va2 s . co m * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.lang.reflect.InvocationTargetException; import java.sql.Blob; import java.sql.Clob; import java.sql.SQLException; public class Main { private static byte[] readBytes(Blob blob, byte[] defaultValue) throws SQLException, IOException { if (blob == null) { return defaultValue; } InputStream is = null; try { int length = (int) blob.length(); if (length == 0) { return defaultValue; } byte[] buffer = new byte[length]; is = blob.getBinaryStream(); is.read(buffer); return buffer; } finally { try { if (is != null) { is.close(); } } catch (IOException e) { /* ignored */ } close(blob); } } private static String read(Clob clob, String defaultValue) throws SQLException, IOException { if (clob == null) { return defaultValue; } Reader r = null; try { int length = (int) clob.length(); if (length == 0) { return defaultValue; } char[] buffer = new char[length]; r = clob.getCharacterStream(); r.read(buffer); return new String(buffer, 0, length); } finally { try { if (r != null) { r.close(); } } catch (IOException e) { /* ignored */ } close(clob); } } private static void close(Object lob) { if (lob == null) { return; } // ORACLE 'temporary lob' problem patch start Class clazz = lob.getClass(); String name = clazz.getName(); if (name.equals("oracle.sql.BLOB") || name.equals("oracle.sql.CLOB")) { try { if (clazz.getMethod("isTemporary", new Class[0]).invoke(lob, new Object[0]).equals(Boolean.TRUE)) { clazz.getMethod("freeTemporary", new Class[0]).invoke(lob, new Object[0]); } } catch (IllegalAccessException e) { /* ignored */ } catch (InvocationTargetException e) { /* ignored */ } catch (NoSuchMethodException e) { /* ignored */ } } } }