Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > PrintWriter woes

Reply
Thread Tools

PrintWriter woes

 
 
Crouchez
Guest
Posts: n/a
 
      09-01-2007
I create a PrintWriter with:
p = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
mystream,"UTF-8"),4096));

and capture the bytes in the write method of mystream that extends
OutputStream

when I p.print(somestring) a number of times it pushes the bytes to mystream
in 8192 chunks. How come when I've set BufferWriter to 4096?


 
Reply With Quote
 
 
 
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      09-01-2007
Crouchez wrote:

> when I p.print(somestring) a number of times it pushes the bytes to mystream
> in 8192 chunks. How come when I've set BufferWriter to 4096?


A character has a length of 2 bytes. It's not very clear what
the API means with the size of the buffer but I assume it's
characters. But to make sure you should look into the sources.
Maybe there's another buffer (e.g. in OutputStreamWriter) leading
to the effect.


Regards, Lothar
--
Lothar Kimmeringer E-Mail:
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
 
 
 
Crouchez
Guest
Posts: n/a
 
      09-02-2007

"Lothar Kimmeringer" <> wrote in message
news:...
> Crouchez wrote:
>
>> when I p.print(somestring) a number of times it pushes the bytes to
>> mystream
>> in 8192 chunks. How come when I've set BufferWriter to 4096?

>
> A character has a length of 2 bytes. It's not very clear what
> the API means with the size of the buffer but I assume it's
> characters. But to make sure you should look into the sources.
> Maybe there's another buffer (e.g. in OutputStreamWriter) leading
> to the effect.
>
>
> Regards, Lothar
> --
> Lothar Kimmeringer E-Mail:
> PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
>
> Always remember: The answer is forty-two, there can only be wrong
> questions!



It doesn't matter what size is given to BufferedWriter it still sends
buffers of 8192. Does sun.nio.cs.StreamEncoder buffer? And where is it?


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-02-2007
On Sun, 02 Sep 2007 00:34:54 GMT, "Crouchez"
<> wrote, quoted or indirectly
quoted someone who said :

>It doesn't matter what size is given to BufferedWriter it still sends
>buffers of 8192. D


How did you discover this?
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-02-2007
On Sat, 01 Sep 2007 20:58:43 GMT, "Crouchez"
<> wrote, quoted or indirectly
quoted someone who said :

>I create a PrintWriter with:
>p = new PrintWriter(
> new BufferedWriter(new OutputStreamWriter(
> mystream,"UTF-8"),4096));
>
>and capture the bytes in the write method of mystream that extends
>OutputStream
>
>when I p.print(somestring) a number of times it pushes the bytes to mystream
>in 8192 chunks. How come when I've set BufferWriter to 4096?


look at the code for the BufferedWriter constructor.

public BufferedWriter(Writer out, int sz) {
super(out);
if (sz <= 0)
throw new IllegalArgumentException("Buffer size <= 0");
this.out = out;
cb = new char[sz];

The size of the buffer is specified in chars.

See http://mindprod.com/applet/fileio.html
When it generates code for you it now documents the unit of measure
for buffer sizes.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      09-02-2007
Crouchez wrote:

> It doesn't matter what size is given to BufferedWriter it still sends
> buffers of 8192.


Can you post the source you wrote for testing this? With the
constructor Roedy was showing I would expect the number of
bytes being sent to the underlying stream is dependent on the
characters being written and the encoding to be used. With x
characters written and UTF-8 used as encoding the number of
bytes should vary between x (ASCII) and 3x (0x800 - 0xffff).
So with a buffer-size of 4096 the number of bytes in your example
could reach up to 12288.

I don't know how OutputStreamWriter works internally but if there
is a buffer as well this might be the reason, why you always
get the same number of bytes. But I'm not going to look into that
further until you posted your test-code.

> Does sun.nio.cs.StreamEncoder buffer? And where is it?


In rt.jar or charset.jar, but I would expect it in the first one.


Regards, Lothar
--
Lothar Kimmeringer E-Mail:
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-02-2007
On Sat, 01 Sep 2007 20:58:43 GMT, "Crouchez"
<> wrote, quoted or indirectly
quoted someone who said :

>when I p.print(somestring) a number of times it pushes the bytes to mystream
>in 8192 chunks. How come when I've set BufferWriter to 4096?


In short BufferedWriter constructor takes the size in chars. 4096
chars = 8192 bytes.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Crouchez
Guest
Posts: n/a
 
      09-02-2007

"Roedy Green" <> wrote in message
news:...
> On Sat, 01 Sep 2007 20:58:43 GMT, "Crouchez"
> <> wrote, quoted or indirectly
> quoted someone who said :
>
>>when I p.print(somestring) a number of times it pushes the bytes to
>>mystream
>>in 8192 chunks. How come when I've set BufferWriter to 4096?

>
> In short BufferedWriter constructor takes the size in chars. 4096
> chars = 8192 bytes.
> --
> Roedy Green Canadian Mind Products
> The Java Glossary
> http://mindprod.com


It doesn't matter if you put a buffer of 1024 it still pushes a buffer
through of 8192

out = new AnOutputStream( 1024); //buf then set as 1024
p = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
out,"UTF-8"),1024));


p.print(string) x 4000 times eg.

result of public synchronized void write(byte b[], int off, int len) in out

len:8192

....


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-02-2007
Crouchez wrote:
> out = new AnOutputStream( 1024); //buf then set as 1024
> p = new PrintWriter(
> new BufferedWriter(new OutputStreamWriter(
> out,"UTF-8"),1024));
>
> len:8192


> when I p.print(somestring) a number of times it pushes the bytes to mystream

a.k.a. "out"
> in 8192 chunks. How come when I've set BufferWriter to 4096?


To be clear - you are seeing writes to the underlying stream in 8 KiB chunks?
You aren't measuring the writes in either of the Writers?

Had you considered reading the API docs for OutputStreamWriter?
<http://java.sun.com/javase/6/docs/api/java/io/OutputStreamWriter.html>
wherein it lets you know that:
> Each invocation of a write() method causes the encoding converter to be invoked on the given character(s).
> The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes.


Dollars to doughnuts the size of that OutputStreamWriter buffer on your system
is 8 KiB. Any takers?

Aren't the API docs wonderful?

--
Lew
 
Reply With Quote
 
Crouchez
Guest
Posts: n/a
 
      09-02-2007

"Lew" <> wrote in message
news:kfOdne1-...
> Crouchez wrote:
>> out = new AnOutputStream( 1024); //buf then set as 1024
>> p = new PrintWriter(
>> new BufferedWriter(new OutputStreamWriter(
>> out,"UTF-8"),1024));
>>
>> len:8192

>
>> when I p.print(somestring) a number of times it pushes the bytes to
>> mystream

> a.k.a. "out"
>> in 8192 chunks. How come when I've set BufferWriter to 4096?

>
> To be clear - you are seeing writes to the underlying stream in 8 KiB
> chunks? You aren't measuring the writes in either of the Writers?
>
> Had you considered reading the API docs for OutputStreamWriter?
> <http://java.sun.com/javase/6/docs/api/java/io/OutputStreamWriter.html>
> wherein it lets you know that:
>> Each invocation of a write() method causes the encoding converter to be
>> invoked on the given character(s). The resulting bytes are accumulated in
>> a buffer before being written to the underlying output stream. The size
>> of this buffer may be specified, but by default it is large enough for
>> most purposes.

>
> Dollars to doughnuts the size of that OutputStreamWriter buffer on your
> system is 8 KiB. Any takers?
>
> Aren't the API docs wonderful?
>
> --
> Lew



So you can't set this value unless you create your own version of it? So
sun.nio.cs.StreamEncoder buffers at 8192 potentially causing buffer
overflows at the end of the line?

"The size of this buffer may be specified, but by default it is large enough
for most purposes. "

Specified where exactly?



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confused with PrintWriter, OutputStream and OutputStreamWriter. CamT Java 1 03-16-2005 08:06 PM
What's the diff PrintWriter vs. OutputStreamWriter ? Lars Willich Java 0 01-28-2005 10:16 PM
Possible to share PrintWriter stream for two files? Sharp Java 5 10-15-2004 04:02 PM
Sending a file via PrintWriter Houman Java 2 08-18-2004 03:15 AM
Java program hangs on new PrintWriter Oz Levanon Java 1 03-01-2004 01:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57