Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > mix chars and bytes in a stream.

Reply
Thread Tools

mix chars and bytes in a stream.

 
 
Sakagami Hiroki
Guest
Posts: n/a
 
      08-06-2006
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

 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      08-07-2006
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



 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      08-07-2006

"Chris Uppal" <(E-Mail Removed)-THIS.org> wrote in message
news:44d6f93e$0$632$(E-Mail Removed)...
> 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?


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      08-07-2006
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


 
Reply With Quote
 
Roland de Ruiter
Guest
Posts: n/a
 
      08-07-2006
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
 
Reply With Quote
 
Sakagami Hiroki
Guest
Posts: n/a
 
      08-09-2006
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


 
Reply With Quote
 
Sakagami Hiroki
Guest
Posts: n/a
 
      08-09-2006

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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      08-31-2006
"Sakagami Hiroki" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> 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

 
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
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
bytes, chars, and strings, oh my! David N. Welton Java 5 10-06-2005 11:37 AM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM
Packing bytes into chars Chris Java 9 09-03-2003 07:59 PM



Advertisments