[hibernate-issues] [Hibernate-JIRA] Created: (HHH-4095) bug in org.hibernate.Hibernate.createBlob( InputStream in )

Stephan Schröder (JIRA) noreply at atlassian.com
Wed Aug 19 02:29:15 EDT 2009


bug in org.hibernate.Hibernate.createBlob( InputStream in )
-----------------------------------------------------------

                 Key: HHH-4095
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4095
             Project: Hibernate Core
          Issue Type: Bug
          Components: core
    Affects Versions: 3.3.2
         Environment: (doesn't matter because ii's just a plain wrong usage of the InputStream.available-mehtode but anyway:) Hibernate. 3.3.2, Hsqldb1.8
            Reporter: Stephan Schröder
            Priority: Minor


The implementation of org.hibernate.Hibernate.createBlob( InputStream in ) looks like this

public static Blob createBlob(InputStream stream) throws IOException {
    return new SerializableBlob( new BlobImpl( stream, stream.available() ) );
}

The second parameter here is supposed to be the length of the inputstream. The problem is that stream.available() doesn't return the length of the inputstream. That's explicitly noted in the Java-API (http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#available):
"Note that while some implementations of InputStream will return the total number of bytes in the stream (on calling available() ), 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."

This is the source of my problem, only the first available bytes are read out of the inputstream. While the workaround for me is to simply call org.hibernate.Hibernate.createBlob( InputStream in,int length ), the misuse of stream.available() in Hibernate.createBlob( InputStream in ) remains to be resolved.

Solution: the only way to get the length of an InputStream ist to read it completly, if you don't want to do that Hibernate.createBlob( InputStream i ) should be removed.

(By the way, why is length of type int? java.io.File.length() and java.sql.Blob.length() are of type long, so length should be of type long as well).

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

       



More information about the hibernate-issues mailing list