Hi,
I tried to create a Blob from an InputStream and had the problem that the Blob was sometimes empty.
I looked into the Hibernate source and found the reason:
Code:
public static Blob createBlob(InputStream stream) throws IOException {
return new SerializableBlob( new BlobImpl( stream, stream.available() ) );
}
This works when reading from a file, because stream.available() returns the correct size. But how can this work when the size of the streamed data is unknown? I would have expected that this method reads until the end of the stream. According to the JDK documentation, the result of stream.available() should not be used here:
http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#available%28%29Quote:
Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
Regards,
Sven