Created
December 10, 2010 03:12
-
-
Save alphaville/735704 to your computer and use it in GitHub Desktop.
BlobHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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