Example usage for java.sql Clob getCharacterStream

List of usage examples for java.sql Clob getCharacterStream

Introduction

In this page you can find the example usage for java.sql Clob getCharacterStream.

Prototype

java.io.Reader getCharacterStream() throws SQLException;

Source Link

Document

Retrieves the CLOB value designated by this Clob object as a java.io.Reader object (or as a stream of characters).

Usage

From source file:com.textocat.textokit.commons.cpe.JdbcCollectionReader.java

private DbTuple toTuple(ResultSet rs) throws SQLException, IOException {
    String url = rs.getString(documentUrlColumn);
    String text = null;// w w w  .j a v  a  2s.c om
    Clob textClob = rs.getClob(textColumn);
    if (textClob != null) {
        Reader textReader = null;
        try {
            textReader = textClob.getCharacterStream();
            text = IOUtils.toString(textReader);
        } finally {
            IOUtils.closeQuietly(textReader);
            textClob.free();
        }
    }
    return new DbTuple(url, text);
}

From source file:com.mirth.connect.connectors.jdbc.DatabaseReceiver.java

private String clobToString(Clob clob) throws Exception {
    StringBuilder stringBuilder = new StringBuilder();
    Reader reader = clob.getCharacterStream();
    BufferedReader bufferedReader = new BufferedReader(reader);
    int c;/*w  w w.j a va2  s  .c  o  m*/

    try {
        while ((c = bufferedReader.read()) != -1) {
            stringBuilder.append((char) c);
        }

        return stringBuilder.toString();
    } finally {
        IOUtils.closeQuietly(bufferedReader);
        IOUtils.closeQuietly(reader);
    }
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.xmi.exporter.EObjectCreatorImpl.java

/**
 * Returns the content of the specified {@code clob} and returns it. 
 * /* w  w  w. ja  v  a 2s.  c  o  m*/
 * @param clob the clob object
 * @return the content or {@code null}, if the content cannot be read
 */
private Object getClobContent(Clob clob) {
    try {
        Reader characterStream = clob.getCharacterStream();
        return IOUtils.toString(characterStream);
    } catch (IOException e) {
        LOGGER.error("An unexpected error occurred reading the clob", e);
    } catch (SQLException e) {
        LOGGER.error("An unexpected error occurred reading the clob", e);
    }

    return null;
}

From source file:com.microsoft.tfs.core.internal.db.DBStatement.java

private String clobToString(final Clob clob) throws SQLException, IOException {
    final StringWriter writer = new StringWriter();
    final Reader reader = clob.getCharacterStream();
    final char[] buf = new char[2048];
    int len;//from   w  w  w . j av a 2  s  . c  o  m
    while ((len = reader.read(buf)) != -1) {
        writer.write(buf, 0, len);
    }

    return writer.toString();
}

From source file:data.DefaultExchanger.java

protected String clobString(@Nullable Clob clob) throws SQLException {
    if (clob == null) {
        return null;
    }// www. j  av a  2s .co m
    Reader reader = clob.getCharacterStream();
    StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(reader, writer);
        return writer.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.recomdata.transmart.data.export.util.FileWriterUtil.java

public String getClobAsString(Clob clob) {
    String strVal = "";
    Reader reader = null;//from  w  w  w  .  j  a v  a2 s .  c  o m
    StringBuffer strBuf = null;
    try {
        if (null != clob) {
            Long clobLength = (null != clob) ? clob.length() : 0;
            reader = clob.getCharacterStream();

            if (null != clobLength && clobLength > 0 && null != reader) {
                //Here length of String is being rounded to 5000 * n, this is because the buffer size is 5000
                //Sometimes the cloblength is less than 5000 * n
                char[] buffer = new char[clobLength.intValue()];
                @SuppressWarnings("unused")
                int count = 0;
                strBuf = new StringBuffer();
                while ((count = reader.read(buffer)) > 0) {
                    strBuf.append(buffer);
                }
                strVal = strBuf.toString();
            }
        }
    } catch (IOException e) {
        log.info(e.getMessage());
    } catch (SQLException e2) {
        log.info("SQLException :: " + e2.getMessage());
    } finally {
        try {
            if (null != reader)
                reader.close();

            //Nullify the objects so they become ready for Garbage Collection
            reader = null;
            strBuf = null;
            clob = null;
            super.finalize();
        } catch (IOException e) {
            log.info("Error closing Reader in getClobAsString");
        } catch (Throwable e) {
            log.info("Error during super.finalize()");
        }
    }

    return strVal;
}

From source file:org.xsystem.sql2.dml.DmlCommand.java

String getClob(Clob clob) throws SQLException {
    Reader rdr = clob.getCharacterStream();
    try {//from  w  w w.ja v  a2 s  . c om
        String ret = IOUtils.toString(rdr);
        return ret;
    } catch (IOException ex) {
        throw new SQLException(ex);
    } finally {
        Auxilary.close(rdr);
    }
}

From source file:it.greenvulcano.gvesb.utils.GVESBPropertyHandler.java

private String expandSQLProperties(String str, Map<String, Object> inProperties, Object object, Object extra)
        throws PropertiesHandlerException {
    PreparedStatement ps = null;//from   ww  w .ja  va 2  s .  c  o m
    ResultSet rs = null;
    String sqlStatement = null;
    Connection conn = null;
    String connName = "";
    boolean intConn = false;
    try {
        if (!PropertiesHandler.isExpanded(str)) {
            str = PropertiesHandler.expand(str, inProperties, object, extra);
        }
        int pIdx = str.indexOf("::");
        if (pIdx != -1) {
            connName = str.substring(0, pIdx);
            sqlStatement = str.substring(pIdx + 2);
            intConn = true;
        } else {
            sqlStatement = str;
        }
        if (intConn) {
            conn = JDBCConnectionBuilder.getConnection(connName);
        } else if ((extra != null) && (extra instanceof Connection)) {
            conn = (Connection) extra;
        } else {
            throw new PropertiesHandlerException(
                    "Error handling 'sql' metadata '" + str + "', Connection undefined.");
        }
        logger.debug("Executing SQL statement {" + sqlStatement + "} on connection [" + connName + "]");
        ps = conn.prepareStatement(sqlStatement);
        rs = ps.executeQuery();

        String paramValue = null;

        if (rs.next()) {
            ResultSetMetaData rsmeta = rs.getMetaData();
            if (rsmeta.getColumnType(1) == Types.CLOB) {
                Clob clob = rs.getClob(1);
                if (clob != null) {
                    Reader is = clob.getCharacterStream();
                    StringWriter strW = new StringWriter();

                    IOUtils.copy(is, strW);
                    is.close();
                    paramValue = strW.toString();
                }
            } else {
                paramValue = rs.getString(1);
            }
        }

        return (paramValue != null) ? paramValue.trim() : paramValue;
    } catch (Exception exc) {
        logger.warn("Error handling 'sql' metadata '" + sqlStatement + "'", exc);
        if (PropertiesHandler.isExceptionOnErrors()) {
            if (exc instanceof PropertiesHandlerException) {
                throw (PropertiesHandlerException) exc;
            }
            throw new PropertiesHandlerException("Error handling 'sql' metadata '" + str + "'", exc);
        }
        return "sql" + PROP_START + str + PROP_END;
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception exc) {
                // do nothing
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception exc) {
                // do nothing
            }
        }
        if (intConn && (conn != null)) {
            try {
                JDBCConnectionBuilder.releaseConnection(connName, conn);
            } catch (Exception exc) {
                // do nothing
            }
        }
    }
}

From source file:org.jesterj.ingest.scanners.JdbcScanner.java

private byte[] getContentBytes(ResultSet rs) throws SQLException {
    byte[] rawBytes = null;

    // If the content column was specified
    if (StringUtils.isNotBlank(contentColumn)) {
        // Get its value.
        Object content = rs.getObject(contentColumn);

        if (content != null) {
            // Clob
            if (content instanceof Clob) {
                Clob clob = (Clob) content;
                try (Reader reader = clob.getCharacterStream()) {
                    rawBytes = CharStreams.toString(reader).getBytes();
                } catch (IOException ex) {
                    String msg = String.format("I/O error while reading value of content column '%s'.",
                            contentColumn);
                    log.error(msg, ex);/*from w  w w. j  av a 2s .  com*/
                }
            }
            // Blob
            else if (content instanceof Blob) {
                Blob blob = (Blob) content;
                try (InputStream stream = blob.getBinaryStream()) {
                    rawBytes = IOUtils.toByteArray(stream);
                } catch (IOException ex) {
                    String msg = String.format("I/O error while reading value of content column '%s'.",
                            contentColumn);
                    log.error(msg, ex);
                }
            }
            // Date (unlikely, but)
            else if (content instanceof Date) {
                rawBytes = convertDateToString(content).getBytes();
            }
            // Anything else
            else {
                rawBytes = content.toString().getBytes();
            }
        }
    }
    return rawBytes;
}

From source file:it.greenvulcano.gvesb.utils.GVESBPropertyHandler.java

private String expandSQLListProperties(String str, Map<String, Object> inProperties, Object object,
        Object extra) throws PropertiesHandlerException {
    PreparedStatement ps = null;//  www  .j a  v a2  s.c  o m
    ResultSet rs = null;
    String sqlStatement = null;
    Connection conn = null;
    String connName = "";
    String separator = ",";
    boolean intConn = false;
    try {
        if (!PropertiesHandler.isExpanded(str)) {
            str = PropertiesHandler.expand(str, inProperties, object, extra);
        }
        int pIdx = str.indexOf("::");
        if (pIdx != -1) {
            connName = str.substring(0, pIdx);
            sqlStatement = str.substring(pIdx + 2);
            int pIdx2 = str.indexOf("::", pIdx + 2);
            if (pIdx2 != -1) {
                separator = str.substring(pIdx + 2, pIdx2);
                sqlStatement = str.substring(pIdx2 + 2);
            }
            intConn = true;
        } else {
            sqlStatement = str;
        }
        if (intConn) {
            conn = JDBCConnectionBuilder.getConnection(connName);
        } else if ((extra != null) && (extra instanceof Connection)) {
            conn = (Connection) extra;
        } else {
            throw new PropertiesHandlerException(
                    "Error handling 'sqllist' metadata '" + str + "', Connection undefined.");
        }
        ps = conn.prepareStatement(sqlStatement);
        rs = ps.executeQuery();

        String paramValue = "";

        int type = rs.getMetaData().getColumnType(1);

        while (rs.next()) {
            if (type == Types.CLOB) {
                Clob clob = rs.getClob(1);
                if (clob != null) {
                    Reader is = clob.getCharacterStream();
                    StringWriter strW = new StringWriter();

                    IOUtils.copy(is, strW);
                    is.close();
                    paramValue += separator + strW.toString();
                } else {
                    paramValue += separator + "null";
                }
            } else {
                paramValue += separator + rs.getString(1);
            }
        }

        if (!paramValue.equals("")) {
            paramValue = paramValue.substring(separator.length());
        }

        return (paramValue != null) ? paramValue.trim() : paramValue;
    } catch (Exception exc) {
        logger.warn("Error handling 'sqllist' metadata '" + sqlStatement + "'", exc);
        if (PropertiesHandler.isExceptionOnErrors()) {
            if (exc instanceof PropertiesHandlerException) {
                throw (PropertiesHandlerException) exc;
            }
            throw new PropertiesHandlerException("Error handling 'sqllist' metadata '" + str + "'", exc);
        }
        return "sqllist" + PROP_START + str + PROP_END;
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception exc) {
                // do nothing
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception exc) {
                // do nothing
            }
        }
        if (intConn && (conn != null)) {
            try {
                JDBCConnectionBuilder.releaseConnection(connName, conn);
            } catch (Exception exc) {
                // do nothing
            }
        }
    }
}