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);
    out.write(SOME_BYTE_ARRAY);
    w.write("blah blah");
    out.write(OTHER_BYTE_ARRAY);
    w.write("blah blah");
    w.close();
    }

    Regards,

    --
    Sakagami Hiroki
    Sakagami Hiroki, Aug 6, 2006
    #1
    1. Advertising

  2. Sakagami Hiroki

    Chris Uppal Guest

    Sakagami Hiroki wrote:

    > Is it safe to mix unbuffered OutputStream and Writer output?


    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
    later.


    > public void output(OutputStream out) {
    > Writer w = new OutputStreamWriter(out);
    > out.write(SOME_BYTE_ARRAY);
    > w.write("blah blah");
    > out.write(OTHER_BYTE_ARRAY);
    > w.write("blah blah");
    > w.close();
    > }


    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
    #2
    1. Advertising

  3. "Chris Uppal" <-THIS.org> wrote in message
    news:44d6f93e$0$632$...
    > Sakagami Hiroki wrote:
    >
    >> Is it safe to mix unbuffered OutputStream and Writer output?

    >
    > 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
    > later.


    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.

    >
    >
    >> public void output(OutputStream out) {
    >> Writer w = new OutputStreamWriter(out);
    >> out.write(SOME_BYTE_ARRAY);
    >> w.write("blah blah");
    >> out.write(OTHER_BYTE_ARRAY);
    >> w.write("blah blah");
    >> w.close();
    >> }

    >
    > 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.


    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
    #3
  4. Sakagami Hiroki

    Chris Uppal Guest

    Mike Schilling wrote:

    [me:]
    > > 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.

    >
    > Flush it when you're done, of course, but why is it necessary to create a
    > new one each time?


    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
    implementation).

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

    -- chris
    Chris Uppal, Aug 7, 2006
    #4
  5. On 6-8-2006 18:16, Sakagami Hiroki wrote:
    > Hi,
    >
    > Is it safe to mix unbuffered OutputStream and Writer output?
    >
    > for example:
    >
    > public void output(OutputStream out) {
    > Writer w = new OutputStreamWriter(out);
    > out.write(SOME_BYTE_ARRAY);
    > w.write("blah blah");
    > out.write(OTHER_BYTE_ARRAY);
    > w.write("blah blah");
    > w.close();
    > }
    >
    > Regards,
    >
    > --
    > Sakagami Hiroki
    >

    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(SOME_BYTE_ARRAY);
    out.write("blah blah".getBytes(desiredStringEncoding));
    out.write(OTHER_BYTE_ARRAY);
    out.write("blah blah 2".getBytes(desiredStringEncoding));
    }
    --
    Regards,

    Roland
    Roland de Ruiter, Aug 7, 2006
    #5
  6. Thank you for your responses. I would like to call getBytes() rather
    than to recreate Writer object each time...

    Chris Uppal wrote:
    > Mike Schilling wrote:
    >
    > [me:]
    > > > 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.

    > >
    > > Flush it when you're done, of course, but why is it necessary to create a
    > > new one each time?

    >
    > 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
    > implementation).
    >
    > I still /would/ recreate the OutputStreamWriter each time, though, if only for
    > comfort ;-)
    >
    > -- chris
    Sakagami Hiroki, Aug 9, 2006
    #6
  7. Roland de Ruiter wrote:
    > 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(SOME_BYTE_ARRAY);
    > out.write("blah blah".getBytes(desiredStringEncoding));
    > out.write(OTHER_BYTE_ARRAY);
    > out.write("blah blah 2".getBytes(desiredStringEncoding));
    > }


    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".

    Regards,

    --
    Sakagami Hiroki
    Sakagami Hiroki, Aug 9, 2006
    #7
  8. Sakagami Hiroki

    Oliver Wong Guest

    "Sakagami Hiroki" <> wrote in message
    news:...
    >
    > I wish Java syntax allowed byte array literal like b"blah blah".


    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
    else?

    - Oliver
    Oliver Wong, Aug 31, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Packing bytes into chars

    Chris, Sep 2, 2003, in forum: Java
    Replies:
    9
    Views:
    530
    Eric Sosman
    Sep 3, 2003
  2. David N. Welton

    bytes, chars, and strings, oh my!

    David N. Welton, Oct 5, 2005, in forum: Java
    Replies:
    5
    Views:
    441
    Thomas Fritsch
    Oct 6, 2005
  3. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,250
    Tim Rentsch
    Sep 23, 2005
  4. Hongyu
    Replies:
    9
    Views:
    887
    James Kanze
    Aug 8, 2008
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    216
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page