Subject | RE: [Firebird-Java] Why use BufferedInputStream? |
---|---|
Author | Robert DiFalco |
Post date | 2003-09-29T22:52:53Z |
Hmmmm...this doesn't really make sense to me. A BufferedInputStream is
going to use MORE memory, not less.
-----Original Message-----
From: Rick Fincher [mailto:rnf@...]
Sent: Monday, September 29, 2003 3:42 PM
To: Firebird-Java@yahoogroups.com
Subject: Re: [Firebird-Java] Why use BufferedInputStream?
Hi Robert,
If I remember correctly, if you don't do this it reads the entire blob
into
a memory buffer. Multi-megabyte blobs can cause out of memory errors.
If
your system has enough memory you can increase the memory Java
allocates,
but if you distribute your program you cannot guarantee that this will
be
done.
Rick
going to use MORE memory, not less.
-----Original Message-----
From: Rick Fincher [mailto:rnf@...]
Sent: Monday, September 29, 2003 3:42 PM
To: Firebird-Java@yahoogroups.com
Subject: Re: [Firebird-Java] Why use BufferedInputStream?
Hi Robert,
If I remember correctly, if you don't do this it reads the entire blob
into
a memory buffer. Multi-megabyte blobs can cause out of memory errors.
If
your system has enough memory you can increase the memory Java
allocates,
but if you distribute your program you cannot guarantee that this will
be
done.
Rick
----- Original Message -----
> In the FAQ.[txt|html], there is the following code. I'm pretty sure I
> have no idea why the advice is given to use a BufferedInputStream.
First
> of all because we are reading into a byte array.
>
> Second, I'm not sure why one should "ever" use a BufferedInputStream
> since it appears to me that the result of #getBinaryStream is already
> buffered.
>
> What am I missing? Here's the code from the FAQ.
>
> try {
>
> // The ByteArrayOutputStream buffers all bytes written to it
> // until we call getBytes() which returns to us an array of bytes:
> ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
>
> // Create an input stream from the BLOB column. By default,
> rs.getBinaryStream()
> // returns a vanilla InputStream instance. We override this for
> efficiency
> // but you don't have to:
> BufferedInputStream bis = new BufferedInputStream(
> rs.getBinaryStream("fieldblob") );
>
> // A temporary buffer for the byte data:
> byte bindata[1024];
>
> // Used to return how many bytes are read with each read() of the
> input stream:
> int bytesread = 0;
>
> // Make sure its not a NULL value in the column:
> if ( !rs.wasNull() ) {
> if ( (bytesread = bis.read(bindata,0,bindata.length)) != -1 ) {
> // Write out 'bytesread' bytes to the writer instance:
> baos.write(bindata,0,bytesread);
> } else {
> // When the read() method returns -1 we've hit the end of the
> stream,
> // so now we can get our bytes out of the writer object:
> returndata = baos.getBytes();
> }
> }
>
> // Close the binary input stream:
> bis.close();
> } catch ( IOException ioe ) {
> System.err.println("Problem retrieving binary data: " + ioe);
> } catch ( ClassNotFoundException cnfe ) {
> System.err.println("Problem retrieving binary data: " + cnfe);
> }
>
>
> To unsubscribe from this group, send an email to:
> Firebird-Java-unsubscribe@yahoogroups.com
>
>
>
> Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/
>
>
Yahoo! Groups Sponsor
ADVERTISEMENT
To unsubscribe from this group, send an email to:
Firebird-Java-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.