Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

mix chars and bytes in a stream.

Thread view: 
Sakagami Hiroki - 06 Aug 2006 17:16 GMT
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
Chris Uppal - 07 Aug 2006 09:26 GMT
> 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);
[quoted text clipped - 4 lines]
>   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
Mike Schilling - 07 Aug 2006 09:48 GMT
>> Is it safe to mix unbuffered OutputStream and Writer output?
>
[quoted text clipped - 4 lines]
> 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);
[quoted text clipped - 11 lines]
> 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?
Chris Uppal - 07 Aug 2006 10:19 GMT
[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
[quoted text clipped - 5 lines]
> 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 - 09 Aug 2006 18:35 GMT
Thank you for your responses.  I would like to call getBytes() rather
than to recreate Writer object each time...

> [me:]
> > > You should create a new Writer each time you start writing characters,
[quoted text clipped - 17 lines]
>
>     -- chris
Roland de Ruiter - 07 Aug 2006 18:56 GMT
> Hi,
>
[quoted text clipped - 15 lines]
> --
> 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));
}
Signature

Regards,

Roland

Sakagami Hiroki - 09 Aug 2006 18:42 GMT
> Why not keep it all in bytes: convert the strings to bytes with the
> desired encoding.
[quoted text clipped - 6 lines]
>    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
Oliver Wong - 31 Aug 2006 17:37 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.