mix chars and bytes in a stream.

Discussion in 'Java' started by Sakagami Hiroki, Aug 6, 2006.

  1. Hi,

    Is it safe to mix unbuffered OutputStream and Writer output?

    for example:

    public void output(OutputStream out) {
    Writer w = new OutputStreamWriter(out);
    w.write("blah blah");
    w.write("blah blah");

    Sakagami Hiroki, Aug 6, 2006
    1. Advertisements

  2. Sakagami Hiroki

    Chris Uppal Guest

    It is safe, but you should consider carefully what you want to do it for. It
    is very easy to mix them when you are writing out to a file or to the network,
    but it is significantly more difficult to un-mix them when you read it in

    You should create a new Writer each time you start writing characters, and
    flush() it when you have finished. /Don't/ close it, or it will close the
    OutputStream too. If you write some binary to the OutputStream and then want
    to write yet another sequence of text, then create a new OutputStreamWriter.

    -- chris
    Chris Uppal, Aug 7, 2006
    1. Advertisements

  3. Since there's no guarantee that a Reader won't read and buffer bytes that it
    doesn't eventually return as characters, and there's no obverse of "flush()"
    to push them back onto the InputStream.
    Flush it when you're done, of course, but why is it necessary to create a
    new one each time?
    Mike Schilling, Aug 7, 2006
  4. Sakagami Hiroki

    Chris Uppal Guest

    Mike Schilling wrote:

    Now you come to mention it, I can't think of a really convincing reason. I was
    bothered by the possibility that the OutputStreamWriter would get confused
    about where it was in the stream, but I don't think that's an issue for this
    example, and may not be an issue at all ever (I'd have to check the

    I still /would/ recreate the OutputStreamWriter each time, though, if only for
    comfort ;-)

    -- chris
    Chris Uppal, Aug 7, 2006
  5. Why not keep it all in bytes: convert the strings to bytes with the
    desired encoding.

    public void output(OutputStream out) {
    final String desiredStringEncoding = "UTF-8"; // for example
    out.write("blah blah".getBytes(desiredStringEncoding));
    out.write("blah blah 2".getBytes(desiredStringEncoding));
    Roland de Ruiter, Aug 7, 2006
  6. Thank you for your responses. I would like to call getBytes() rather
    than to recreate Writer object each time...

    Sakagami Hiroki, Aug 9, 2006
  7. Because if there are many many lines of code writing to Writer,
    resulting source code will be very ugly...

    I wish Java syntax allowed byte array literal like b"blah blah".

    Sakagami Hiroki, Aug 9, 2006
  8. Sakagami Hiroki

    Oliver Wong Guest

    There's no single obvious way to transform a string into a sequence of
    bytes. What encoding would it use? UTF-8? UTF-16LE? UTF-16BE? Something

    - Oliver
    Oliver Wong, Aug 31, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.