mix chars and bytes in a stream.

  • Thread starter Sakagami Hiroki
  • Start date
S

Sakagami Hiroki

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,
 
C

Chris Uppal

Sakagami said:
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
 
M

Mike Schilling

Chris Uppal said:
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.
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?
 
C

Chris Uppal

Mike Schilling wrote:

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

Roland de Ruiter

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,
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));
}
 
S

Sakagami Hiroki

Thank you for your responses. I would like to call getBytes() rather
than to recreate Writer object each time...

Chris said:
Mike Schilling wrote:

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

Sakagami Hiroki

Roland said:
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,
 
O

Oliver Wong

Sakagami Hiroki said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top