/**
* Reads a file into a BLOB.
*
* @return BLOB
* @throws IOException
* @throws SQLException
*/
private BLOB getBlobData(Connection conn, InputStream is) throws IOException, SQLException {
BLOB fileData = null;
if (is == null) {
final String msg = "Unable to read from InputStream because it is null";
mLogger.error(msg);
throw new IOException(msg);
}
else {
ByteArrayOutputStream blobStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutStrm = new GZIPOutputStream(blobStream);
byte[] buffer = new byte[512];
int length = 0;
while ((length = is.read(buffer)) != -1) {
gzipOutStrm.write(buffer, 0, length);
}
gzipOutStrm.close();
/*
* Although BLOB.createTemporary takes a java.sql.Connection object
* as a parameter, it ONLY works if the Connection is an
* oracle.jdbc.driver.OracleConnection object. If it's not an
* oracle.jdbc.driver.OracleConnection, you get a
* ClassCastException.
*
* In JBoss, when we get the database connection via a DataSource
* object looked up via JNDI, the connection is not a real
* OracleConnection object, but an object that wraps the real
* OracleConnection.
*
* The JBossNativeJdbcExtractor is used to get the underlying
* connection from the wrapped connection. Note that this does not
* tie the code to JBoss - If the given Connection is not a JBoss
* wrapper, it will be returned as-is.
*/
JBossNativeJdbcExtractor nativeJdbcExtractor = new JBossNativeJdbcExtractor();
Connection nativeConnection = nativeJdbcExtractor.getNativeConnection(conn);
if (mLogger.isDebugEnabled()) {
StringBuilder msg = new StringBuilder("Retrieved native connection : ");
msg.append((nativeConnection != null) ? nativeConnection.getClass().getName() : "null");
msg.append(" from wrapped connection : ");
msg.append((conn != null) ? conn.getClass().getName() : "null");
mLogger.debug(msg.toString());
}
fileData = BLOB.createTemporary(nativeConnection, false,
oracle.sql.BLOB.DURATION_SESSION);
fileData.setBytes(1, blobStream.toByteArray());
blobStream.close();
}
return fileData;
}