Skip to content

Instantly share code, notes, and snippets.

@alphaville
Created December 10, 2010 03:12
Show Gist options
  • Save alphaville/735704 to your computer and use it in GitHub Desktop.
Save alphaville/735704 to your computer and use it in GitHub Desktop.
BlobHelper
class BlobHelper {
public static Object toObject(byte[] bytes) {
Object object = null;
try {
object = new java.io.ObjectInputStream(new java.io.ByteArrayInputStream(bytes)).readObject();
} catch (java.io.IOException ioe) {
} catch (java.lang.ClassNotFoundException cnfe) {
}
return object;
}
private byte[] toByteArray(Blob fromModelBlob) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
return toByteArrayImpl(fromModelBlob, baos);
} catch (Exception e) {
}
return null;
}
private byte[] toByteArrayImpl(Blob fromModelBlob,
ByteArrayOutputStream baos) throws SQLException, IOException {
byte buf[] = new byte[4000];
int dataSize;
InputStream is = fromModelBlob.getBinaryStream();
try {
while ((dataSize = is.read(buf)) != -1) {
baos.write(buf, 0, dataSize);
}
} finally {
if (is != null) {
is.close();
}
}
return baos.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment