Java SQL Type readAscii(Clob clob, String defaultValue)

Here you can find the source of readAscii(Clob clob, String defaultValue)

Description

read Ascii

License

Apache License

Declaration

private static String readAscii(Clob clob, String defaultValue) throws SQLException, IOException 

Method Source Code


//package com.java2s;
/*/*from  w  w w  .  j  a v  a 2 s  . c  om*/
 * 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.Clob;

import java.sql.SQLException;

public class Main {
    private static String readAscii(Clob clob, String defaultValue) throws SQLException, IOException {
        if (clob == null) {
            return defaultValue;
        }

        InputStream is = null;
        try {
            int length = (int) clob.length();
            if (length == 0) {
                return defaultValue;
            }

            byte[] buffer = new byte[length];
            is = clob.getAsciiStream();
            is.read(buffer);

            return new String(buffer, 0, length);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                /* ignored */ }
            close(clob);
        }
    }

    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. nativeSQL(String sql, boolean noBackslashEscapes)
  2. newArrayOfType(int typeCode, int size)
  3. newInstance(Class clazz, Object... params)
  4. parse(String txt, int level)
  5. prettyPrint(Object obj)
  6. readMapListBySQL(String fileDir, String sql, List headers, List headerTypes)
  7. readObject(DataInput in, int sqlType)
  8. resolveEscapes(String escaped, boolean noBackslashEscapes)
  9. setFieldValue(Object oBean, String sFieldName, Object oValue)