I have several output listeners that are implementing OutputStream. It can be either a PrintStream writing to stdout or to a File, or it can be writing to memory or any other output destination; therefore, I specified OutputStream as (an) argument in the method.
Now, I have received the String. What is the best way to write to streams here?
Should I just use Writer.write(message.getBytes())? I can give it bytes, but if the destination stream is a character stream then will it convert automatically?
Do I need to use some bridge streams here instead?
Wrap your OutputStream with a PrintWriter and use the print methods on that class. They take in a String and do the work for you.
By design it is to be done this way:
Streams (
InputStream
andOutputStream
) transfer binary data. If you want to write a string to a stream, you must first convert it to bytes, or in other words encode it. You can do that manually (as you suggest) using theString.getBytes(Charset)
method, but you should avoid theString.getBytes()
method, because that uses the default encoding of the JVM, which can't be reliably predicted in a portable way.The usual way to write character data to a stream, though, is to wrap the stream in a
Writer
, (often aPrintWriter
), that does the conversion for you when you call itswrite(String)
(orprint(String)
) method. The corresponding wrapper for InputStreams is a Reader.PrintStream
is a specialOutputStream
implementation in the sense that it also contain methods that automatically encode strings (it uses a writer internally). But it is still a stream. You can safely wrap your stream with a writer no matter if it is aPrintStream
or some other stream implementation. There is no danger of double encoding.Example of PrintWriter with OutputStream:
OutputStream writes bytes, String provides chars. You need to define Charset to encode string to byte[]:
Change
UTF-8
to a charset of your choice.You may use Apache Commons IO:
You can create a PrintStream wrapping around your OutputStream and then just call it's print(String):