You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Timo Rumland <ti...@the-cr.de> on 2011/09/20 23:27:39 UTC

[io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Hello,

I recently started using the FileUtils class of Commons IO, and had a
quick look into the source code.

The method "FileUtils.writeByteArrayToFile(...)" internally uses the
private method "openOutputStream(...)", which creates (after some
smart checks) an FileOutputStream.

But, shouldn't "writeByteArrayToFile(...)" or "openOutputStream(...)"
not use/create a BufferedOutputStream, wrapping the FileOutputStream?

Or do I overlook something?

I think one should always buffer the bytes when writing to a file...


Any thoughts?


Thanks !


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Posted by Timo Rumland <ti...@the-cr.de>.
Hello,

> In this case we call FileOutputStream.write(byte[]) so the array
> should be output in one shot.

that means, that there is no need to buffer anything, right? The bytes
get written with one call to "write( byte[] )".

So I was wrong, I thought about writing one byte after another to a
file, which must be bufferd, but this is not the case for
"FileUtils.writeByteArrayToFile(...)". The byte-array is already the
buffer itself, which gets written as a whole.

Well, thanks a lot for your help, learning never stops :)


Best regards,
Timo




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Posted by Gary Gregory <GG...@seagullsoftware.com>.
In this case we call FileOutputStream.write(byte[]) so the array should be output in one shot. 

Gary

On Sep 25, 2011, at 13:59, "Daniel Pitts" <da...@coloraura.com> wrote:

> Buffering is important if you are writing many small chunks in a row.  I
> haven't looked at the implementation of writeByteArrayToFile, but I'm
> assuming it the entire byte array in one shot and then closes the stream.
> Buffering would actually add extra overhead, as it would copy the byte array
> into another buffer, and the write that to the final output stream.
> 
> On Sun, Sep 25, 2011 at 9:45 AM, Timo Rumland <ti...@the-cr.de>wrote:
> 
>> Hello everyone,
>> 
>> sorry to bump this, but does anyone not have a comment to my question?
>> I really think I missed something, I can't imagine that the Commons IO
>> "forgot" to buffer the bytes that should be written to a file.
>> 
>> Please see my original question below.
>> 
>> Thanks a lot!
>> 
>> -------------------------------------
>> 
>>> Hello,
>> 
>>> I recently started using the FileUtils class of Commons IO, and had a
>>> quick look into the source code.
>> 
>>> The method "FileUtils.writeByteArrayToFile(...)" internally uses the
>>> private method "openOutputStream(...)", which creates (after some
>>> smart checks) an FileOutputStream.
>> 
>>> But, shouldn't "writeByteArrayToFile(...)" or "openOutputStream(...)"
>>> not use/create a BufferedOutputStream, wrapping the FileOutputStream?
>> 
>>> Or do I overlook something?
>> 
>>> I think one should always buffer the bytes when writing to a file...
>> 
>>> Any thoughts?
>> 
>>> Thanks !
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>> 
>> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Posted by Gary Gregory <GG...@seagullsoftware.com>.
True. I need to look at the impl to see how it writes the buffer. 

Gary

On Sep 25, 2011, at 13:59, "Daniel Pitts" <da...@coloraura.com> wrote:

> Buffering is important if you are writing many small chunks in a row.  I
> haven't looked at the implementation of writeByteArrayToFile, but I'm
> assuming it the entire byte array in one shot and then closes the stream.
> Buffering would actually add extra overhead, as it would copy the byte array
> into another buffer, and the write that to the final output stream.
> 
> On Sun, Sep 25, 2011 at 9:45 AM, Timo Rumland <ti...@the-cr.de>wrote:
> 
>> Hello everyone,
>> 
>> sorry to bump this, but does anyone not have a comment to my question?
>> I really think I missed something, I can't imagine that the Commons IO
>> "forgot" to buffer the bytes that should be written to a file.
>> 
>> Please see my original question below.
>> 
>> Thanks a lot!
>> 
>> -------------------------------------
>> 
>>> Hello,
>> 
>>> I recently started using the FileUtils class of Commons IO, and had a
>>> quick look into the source code.
>> 
>>> The method "FileUtils.writeByteArrayToFile(...)" internally uses the
>>> private method "openOutputStream(...)", which creates (after some
>>> smart checks) an FileOutputStream.
>> 
>>> But, shouldn't "writeByteArrayToFile(...)" or "openOutputStream(...)"
>>> not use/create a BufferedOutputStream, wrapping the FileOutputStream?
>> 
>>> Or do I overlook something?
>> 
>>> I think one should always buffer the bytes when writing to a file...
>> 
>>> Any thoughts?
>> 
>>> Thanks !
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>> 
>> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Posted by Daniel Pitts <da...@coloraura.com>.
Buffering is important if you are writing many small chunks in a row.  I
haven't looked at the implementation of writeByteArrayToFile, but I'm
assuming it the entire byte array in one shot and then closes the stream.
Buffering would actually add extra overhead, as it would copy the byte array
into another buffer, and the write that to the final output stream.

On Sun, Sep 25, 2011 at 9:45 AM, Timo Rumland <ti...@the-cr.de>wrote:

> Hello everyone,
>
> sorry to bump this, but does anyone not have a comment to my question?
> I really think I missed something, I can't imagine that the Commons IO
> "forgot" to buffer the bytes that should be written to a file.
>
> Please see my original question below.
>
> Thanks a lot!
>
> -------------------------------------
>
> > Hello,
>
> > I recently started using the FileUtils class of Commons IO, and had a
> > quick look into the source code.
>
> > The method "FileUtils.writeByteArrayToFile(...)" internally uses the
> > private method "openOutputStream(...)", which creates (after some
> > smart checks) an FileOutputStream.
>
> > But, shouldn't "writeByteArrayToFile(...)" or "openOutputStream(...)"
> > not use/create a BufferedOutputStream, wrapping the FileOutputStream?
>
> > Or do I overlook something?
>
> > I think one should always buffer the bytes when writing to a file...
>
> > Any thoughts?
>
> > Thanks !
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Re: [io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Posted by Jörg Schaible <jo...@scalaris.com>.
Gary Gregory wrote:

> The buffer increases performance actually. Its faster to write a file in
> 8KB blocks than 1 byte at a time.

As said, we write the bytes *at once* not one by one. Therefore a buffer 
decreases performance here!

- Jörg


> 
> Gary
> 
> On Sep 25, 2011, at 13:55, "Jörg Schaible" <jo...@gmx.de> wrote:
> 
>> Hi Timo,
>> 
>> Timo Rumland wrote:
>> 
>>> Hello everyone,
>>> 
>>> sorry to bump this, but does anyone not have a comment to my question?
>>> I really think I missed something, I can't imagine that the Commons IO
>>> "forgot" to buffer the bytes that should be written to a file.
>>> 
>>> Please see my original question below.
>> 
>> Why should it? You provide all the bytes which are written immediately
>> and the stream is closed afterwards. So what should a buffer be good for
>> except for decreasing performance?
>> 
>> - Jörg
>> 
>>> 
>>> Thanks a lot!
>>> 
>>> -------------------------------------
>>> 
>>>> Hello,
>>> 
>>>> I recently started using the FileUtils class of Commons IO, and had a
>>>> quick look into the source code.
>>> 
>>>> The method "FileUtils.writeByteArrayToFile(...)" internally uses the
>>>> private method "openOutputStream(...)", which creates (after some
>>>> smart checks) an FileOutputStream.
>>> 
>>>> But, shouldn't "writeByteArrayToFile(...)" or "openOutputStream(...)"
>>>> not use/create a BufferedOutputStream, wrapping the FileOutputStream?
>>> 
>>>> Or do I overlook something?
>>> 
>>>> I think one should always buffer the bytes when writing to a file...
>>> 
>>>> Any thoughts?
>>> 
>>>> Thanks !
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Posted by Gary Gregory <GG...@seagullsoftware.com>.
The buffer increases performance actually. Its faster to write a file in 8KB blocks than 1 byte at a time. 

Gary

On Sep 25, 2011, at 13:55, "Jörg Schaible" <jo...@gmx.de> wrote:

> Hi Timo,
> 
> Timo Rumland wrote:
> 
>> Hello everyone,
>> 
>> sorry to bump this, but does anyone not have a comment to my question?
>> I really think I missed something, I can't imagine that the Commons IO
>> "forgot" to buffer the bytes that should be written to a file.
>> 
>> Please see my original question below.
> 
> Why should it? You provide all the bytes which are written immediately and 
> the stream is closed afterwards. So what should a buffer be good for except 
> for decreasing performance?
> 
> - Jörg
> 
>> 
>> Thanks a lot!
>> 
>> -------------------------------------
>> 
>>> Hello,
>> 
>>> I recently started using the FileUtils class of Commons IO, and had a
>>> quick look into the source code.
>> 
>>> The method "FileUtils.writeByteArrayToFile(...)" internally uses the
>>> private method "openOutputStream(...)", which creates (after some
>>> smart checks) an FileOutputStream.
>> 
>>> But, shouldn't "writeByteArrayToFile(...)" or "openOutputStream(...)"
>>> not use/create a BufferedOutputStream, wrapping the FileOutputStream?
>> 
>>> Or do I overlook something?
>> 
>>> I think one should always buffer the bytes when writing to a file...
>> 
>>> Any thoughts?
>> 
>>> Thanks !
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Posted by Jörg Schaible <jo...@gmx.de>.
Hi Timo,

Timo Rumland wrote:

> Hello everyone,
> 
> sorry to bump this, but does anyone not have a comment to my question?
> I really think I missed something, I can't imagine that the Commons IO
> "forgot" to buffer the bytes that should be written to a file.
> 
> Please see my original question below.

Why should it? You provide all the bytes which are written immediately and 
the stream is closed afterwards. So what should a buffer be good for except 
for decreasing performance?

- Jörg

> 
> Thanks a lot!
> 
> -------------------------------------
> 
>> Hello,
> 
>> I recently started using the FileUtils class of Commons IO, and had a
>> quick look into the source code.
> 
>> The method "FileUtils.writeByteArrayToFile(...)" internally uses the
>> private method "openOutputStream(...)", which creates (after some
>> smart checks) an FileOutputStream.
> 
>> But, shouldn't "writeByteArrayToFile(...)" or "openOutputStream(...)"
>> not use/create a BufferedOutputStream, wrapping the FileOutputStream?
> 
>> Or do I overlook something?
> 
>> I think one should always buffer the bytes when writing to a file...
> 
>> Any thoughts?
> 
>> Thanks !



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Posted by Gary Gregory <GG...@seagullsoftware.com>.
It does seem like an omission indeed.  

Gary

On Sep 25, 2011, at 12:46, "Timo Rumland" <ti...@the-cr.de> wrote:

> Hello everyone,
> 
> sorry to bump this, but does anyone not have a comment to my question?
> I really think I missed something, I can't imagine that the Commons IO
> "forgot" to buffer the bytes that should be written to a file.
> 
> Please see my original question below.
> 
> Thanks a lot!
> 
> -------------------------------------
> 
>> Hello,
> 
>> I recently started using the FileUtils class of Commons IO, and had a
>> quick look into the source code.
> 
>> The method "FileUtils.writeByteArrayToFile(...)" internally uses the
>> private method "openOutputStream(...)", which creates (after some
>> smart checks) an FileOutputStream.
> 
>> But, shouldn't "writeByteArrayToFile(...)" or "openOutputStream(...)"
>> not use/create a BufferedOutputStream, wrapping the FileOutputStream?
> 
>> Or do I overlook something?
> 
>> I think one should always buffer the bytes when writing to a file...
> 
>> Any thoughts?
> 
>> Thanks !
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] Should "FileUtils.writeByteArrayToFile(...)" not use a BufferedOutputStream?

Posted by Timo Rumland <ti...@the-cr.de>.
Hello everyone,

sorry to bump this, but does anyone not have a comment to my question?
I really think I missed something, I can't imagine that the Commons IO
"forgot" to buffer the bytes that should be written to a file.

Please see my original question below.

Thanks a lot!

-------------------------------------

> Hello,

> I recently started using the FileUtils class of Commons IO, and had a
> quick look into the source code.

> The method "FileUtils.writeByteArrayToFile(...)" internally uses the
> private method "openOutputStream(...)", which creates (after some
> smart checks) an FileOutputStream.

> But, shouldn't "writeByteArrayToFile(...)" or "openOutputStream(...)"
> not use/create a BufferedOutputStream, wrapping the FileOutputStream?

> Or do I overlook something?

> I think one should always buffer the bytes when writing to a file...

> Any thoughts?

> Thanks !



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org