5

I want to create an unbuffered OutputStreamWriter. The docs (Java 7) say

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.

"The size of this buffer may be specified"... how?

(Edit: this was an old bug in the documentation; fixed in Java 9)

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • Nice catch. I'd expect it to be in the constructor, but apparently not... – Jon Skeet Dec 17 '14 at 18:49
  • @JonSkeet Documentation bug? If so, it looks quite old... Weird. http://www.cs.mun.ca/~michael/java/jdk1.2-docs/api/java/io/OutputStreamWriter.html – leonbloy Dec 17 '14 at 19:05
  • 1
    I believe _some_ buffer is outright required, because of the way character encoding works: if you receive the first half of a surrogate pair, you have to buffer that until you receive its second half. – Louis Wasserman Dec 17 '14 at 19:13
  • @JonSkeet It should not be in constructor. If its there then there is no need for BufferedWritter – Siva Kumar Dec 17 '14 at 19:16
  • 1
    @SivaKumar: Yes there is - because a `BufferedWriter` doesn't do encoding. Assuming there's *any* buffering, it would make sense to be able to specify the size. – Jon Skeet Dec 17 '14 at 19:17
  • @JonSkeet Yes. Once we can redefine the size it will be over head for the class. – Siva Kumar Dec 17 '14 at 19:21
  • 1
    @SivaKumar: In what possible way? There's already a buffer - if a caller specified a *smaller* buffer, that would be *less* overhead, surely. As Louis noted, there has to be a buffer of some description, and as I noted there's still a very significant difference between this and `BufferedWriter`. – Jon Skeet Dec 17 '14 at 19:23
  • @LouisWasserman: Actually that need must be implemented using a separated character buffer (raw), not by the (coded) byte buffer. – leonbloy Dec 17 '14 at 19:38
  • @LoudiWasserman That need concerns input: this question is about output. – user207421 Dec 17 '14 at 20:43
  • @EJP: I'm not convinced? An `OutputStreamWriter` receives `char`s, and you can receive one `char` and not yet be ready to convert it into bytes until you receive the other half of the surrogate pair. @leonbloy: Max's answer below suggests that at least one `OutputStreamWriter` implementation _is_ using a character buffer. – Louis Wasserman Dec 17 '14 at 21:23

1 Answers1

5

Well, it seems that there is no way to tweak the buffer size. It was once possible to set the size in the constructor... in Java 1.1!

The constructor dissapeared in Java 1.2 (1998) and apparently they forgot to update the documentation.

(It looks strange to me that this documentation bug has gone unnoticed so many years - and it also looks strange that I have no way to turn off the buffering.)

Update: still the same in Java 8

Update 2: acknowledged as doc bug, finally removed in Java 9 docs

leonbloy
  • 73,180
  • 20
  • 142
  • 190