Java SQL Blob readBytes(Blob blob, byte[] defaultValue)

Here you can find the source of readBytes(Blob blob, byte[] defaultValue)

Description

read Bytes

License

Apache License

Declaration

private static byte[] readBytes(Blob blob, byte[] defaultValue) throws SQLException, IOException 

Method Source Code


//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 */
            }
        }
    }
}

Related

  1. getLongBlobTypeString(Connection conn)
  2. getString(Blob blob)
  3. getStringValue(Blob blob)
  4. isBlob(Object object)
  5. readBlob(Blob blob)
  6. safeFree(Blob blob)
  7. safeFree(Blob blob)
  8. toByteArray(Blob blob)
  9. writeStringToBlob(String value, PreparedStatement preparedStatement, int index)