List of usage examples for java.sql Clob getAsciiStream
java.io.InputStream getAsciiStream() throws SQLException;
From source file:org.springframework.jdbc.support.lob.DefaultLobHandler.java
@Override public InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) throws SQLException { logger.debug("Returning CLOB as ASCII stream"); if (this.wrapAsLob) { Clob clob = rs.getClob(columnIndex); return clob.getAsciiStream(); } else {//ww w . j a v a 2 s .c om return rs.getAsciiStream(columnIndex); } }
From source file:org.springframework.jdbc.support.lob.OracleLobHandler.java
@Override public InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) throws SQLException { logger.debug("Returning Oracle CLOB as ASCII stream"); Clob clob = rs.getClob(columnIndex); initializeResourcesBeforeRead(rs.getStatement().getConnection(), clob); InputStream retVal = (clob != null ? clob.getAsciiStream() : null); releaseResourcesAfterRead(rs.getStatement().getConnection(), clob); return retVal; }
From source file:org.xenei.jdbc4sparql.iface.TypeConverter.java
@SuppressWarnings("unchecked") public static <T> T extractData(final Object columnObject, final Class<T> resultingClass) throws SQLException { if (columnObject == null) { return (T) nullValueMap.get(resultingClass); }//from w w w .j a va 2s. c om // try the simple case if (resultingClass.isAssignableFrom(columnObject.getClass())) { return resultingClass.cast(columnObject); } // see if we can do a simple numeric assignment if (columnObject instanceof Number) { return fromNumber(columnObject, resultingClass); } // see if we can convert from a string if (columnObject instanceof String) { return fromString(columnObject, resultingClass); } if (columnObject instanceof Boolean) { final Boolean b = (Boolean) columnObject; return fromString(b ? "1" : "0", resultingClass); } if (columnObject instanceof byte[]) { try { if (resultingClass.isAssignableFrom(Clob.class)) { return resultingClass.cast( new SerialClob(IOUtils.toCharArray(new ByteArrayInputStream((byte[]) columnObject)))); } if (resultingClass.isAssignableFrom(Blob.class)) { return resultingClass.cast(new SerialBlob((byte[]) columnObject)); } if (resultingClass.isAssignableFrom(InputStream.class)) { return resultingClass.cast(new ByteArrayInputStream((byte[]) columnObject)); } final String s = new String((byte[]) columnObject); return fromString(s, resultingClass); } catch (final IOException e) { throw new SQLException(e.getMessage(), e); } } if (columnObject instanceof Blob) { try { final Blob b = (Blob) columnObject; if (resultingClass.isAssignableFrom(byte[].class)) { return resultingClass.cast(IOUtils.toByteArray(b.getBinaryStream())); } if (resultingClass.isAssignableFrom(Clob.class)) { return resultingClass.cast(new SerialClob(IOUtils.toCharArray(b.getBinaryStream()))); } if (resultingClass.isAssignableFrom(InputStream.class)) { return resultingClass.cast(b.getBinaryStream()); } final String s = new String(IOUtils.toByteArray(((Blob) columnObject).getBinaryStream())); return fromString(s, resultingClass); } catch (final IOException e) { throw new SQLException(e.getMessage(), e); } } if (columnObject instanceof Clob) { try { final Clob c = (Clob) columnObject; if (resultingClass.isAssignableFrom(byte[].class)) { return resultingClass.cast(IOUtils.toByteArray(c.getAsciiStream())); } if (resultingClass.isAssignableFrom(Blob.class)) { return resultingClass.cast(new SerialBlob(IOUtils.toByteArray(c.getAsciiStream()))); } if (resultingClass.isAssignableFrom(InputStream.class)) { return resultingClass.cast(c.getAsciiStream()); } final String s = String.valueOf(IOUtils.toCharArray(c.getCharacterStream())); return fromString(s, resultingClass); } catch (final IOException e) { throw new SQLException(e.getMessage(), e); } } if (columnObject instanceof InputStream) { try { final InputStream is = (InputStream) columnObject; if (resultingClass.isAssignableFrom(Clob.class)) { return resultingClass.cast(new SerialClob(IOUtils.toCharArray(is))); } if (resultingClass.isAssignableFrom(Blob.class)) { return resultingClass.cast(new SerialBlob(IOUtils.toByteArray(is))); } if (resultingClass.isAssignableFrom(byte[].class)) { return resultingClass.cast(IOUtils.toByteArray(is)); } return fromString(new String(IOUtils.toByteArray(is)), resultingClass); } catch (final IOException e) { throw new SQLException(e.getMessage(), e); } } throw new SQLException(String.format(" Can not cast %s (%s) to %s", columnObject.getClass(), columnObject.toString(), resultingClass)); }
From source file:util.ClobUtil.java
/** * Convert the given clob to a String./*from ww w .j av a 2 s . com*/ * * @param clob The clob to convert. * @return The converted String. * @throws SQLException * @throws IOException */ public static String convertClobToString(Clob clob) throws SQLException, IOException { InputStream in = clob.getAsciiStream(); StringWriter w = new StringWriter(); IOUtils.copy(in, w); return w.toString(); }