use of java.sql.Blob in project microservices by pwillhan.
the class LazyProperties method storeLoadLocator.
@Test
public void storeLoadLocator() throws Exception {
// TODO: This test fails on H2 standalone
// http://groups.google.com/group/h2-database/browse_thread/thread/9c6f4893a62c9b1a
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
byte[] bytes = new byte[131072];
new Random().nextBytes(bytes);
InputStream imageInputStream = new ByteArrayInputStream(bytes);
int byteLength = bytes.length;
Item someItem = new Item();
someItem.setName("Some item");
someItem.setDescription("This is some description.");
// Need the native Hibernate API
Session session = em.unwrap(Session.class);
// You need to know the number of bytes you want to read from the stream!
Blob blob = session.getLobHelper().createBlob(imageInputStream, byteLength);
someItem.setImageBlob(blob);
em.persist(someItem);
tx.commit();
em.close();
Long ITEM_ID = someItem.getId();
tx.begin();
em = JPA.createEntityManager();
Item item = em.find(Item.class, ITEM_ID);
// You can stream the bytes directly...
InputStream imageDataStream = item.getImageBlob().getBinaryStream();
// ... or materialize them into memory:
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
StreamUtils.copy(imageDataStream, outStream);
byte[] imageBytes = outStream.toByteArray();
assertEquals(imageBytes.length, 131072);
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of java.sql.Blob in project jaqy by Teradata.
the class ObjectTypeHandler method getString.
@Override
public String getString(JaqyResultSet rs, int columnIndex, JaqyInterpreter interpreter) throws SQLException {
Object obj = rs.getObject(columnIndex);
if (obj == null)
return null;
String value = null;
if (obj instanceof Clob) {
Clob clob = (Clob) obj;
value = clob.getSubString(1, (int) clob.length());
clob.free();
} else if (obj instanceof byte[]) {
value = StringUtils.getHexString((byte[]) obj);
} else if (obj instanceof Blob) {
Blob blob = ((Blob) obj);
byte[] bytes = blob.getBytes(1, (int) blob.length());
value = StringUtils.getHexString((byte[]) bytes);
blob.free();
} else {
value = obj.toString();
}
return value;
}
use of java.sql.Blob in project incubator-gobblin by apache.
the class MysqlStateStore method getAll.
protected List<T> getAll(String storeName, String tableName, boolean useLike) throws IOException {
List<T> states = Lists.newArrayList();
try (Connection connection = dataSource.getConnection();
PreparedStatement queryStatement = connection.prepareStatement(useLike ? SELECT_JOB_STATE_WITH_LIKE_SQL : SELECT_JOB_STATE_SQL)) {
queryStatement.setString(1, storeName);
queryStatement.setString(2, tableName);
try (ResultSet rs = queryStatement.executeQuery()) {
while (rs.next()) {
Blob blob = rs.getBlob(1);
Text key = new Text();
try (InputStream is = StreamUtils.isCompressed(blob.getBytes(1, 2)) ? new GZIPInputStream(blob.getBinaryStream()) : blob.getBinaryStream();
DataInputStream dis = new DataInputStream(is)) {
// keep deserializing while we have data
while (dis.available() > 0) {
T state = this.stateClass.newInstance();
key.readString(dis);
state.readFields(dis);
states.add(state);
}
} catch (EOFException e) {
// no more data. GZIPInputStream.available() doesn't return 0 until after EOF.
}
}
}
} catch (RuntimeException re) {
throw re;
} catch (Exception e) {
throw new IOException("failure retrieving state from storeName " + storeName + " tableName " + tableName, e);
}
return states;
}
use of java.sql.Blob in project incubator-gobblin by apache.
the class MysqlStateStore method get.
@Override
public T get(String storeName, String tableName, String stateId) throws IOException {
try (Connection connection = dataSource.getConnection();
PreparedStatement queryStatement = connection.prepareStatement(SELECT_JOB_STATE_SQL)) {
int index = 0;
queryStatement.setString(++index, storeName);
queryStatement.setString(++index, tableName);
try (ResultSet rs = queryStatement.executeQuery()) {
if (rs.next()) {
Blob blob = rs.getBlob(1);
Text key = new Text();
try (InputStream is = StreamUtils.isCompressed(blob.getBytes(1, 2)) ? new GZIPInputStream(blob.getBinaryStream()) : blob.getBinaryStream();
DataInputStream dis = new DataInputStream(is)) {
// keep deserializing while we have data
while (dis.available() > 0) {
T state = this.stateClass.newInstance();
key.readFields(dis);
state.readFields(dis);
if (key.toString().equals(stateId)) {
return state;
}
}
} catch (EOFException e) {
// no more data. GZIPInputStream.available() doesn't return 0 until after EOF.
}
}
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IOException("failure retrieving state from storeName " + storeName + " tableName " + tableName, e);
}
return null;
}
use of java.sql.Blob in project wicket by apache.
the class BlobImageResource method getImageData.
@Override
protected byte[] getImageData(Attributes attributes) {
try {
Blob blob = getBlob(attributes);
if (blob != null) {
InputStream in = blob.getBinaryStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(in, out);
return out.toByteArray();
}
return new byte[0];
} catch (SQLException e) {
throw new WicketRuntimeException("Error while reading image data", e);
} catch (IOException e) {
throw new WicketRuntimeException("Error while reading image data", e);
}
}
Aggregations