views:

219

answers:

2

Using JNLP, I have this line :

File_Save_Service.saveFileDialog(null,null,new StringBufferInputStream("testing"),null);

How to replace StringBufferInputStream with StringReader in this line ? The API for StringBufferInputStream says better use StringReader, but how to convert between different types ?

+1  A: 

The standard Java class libraries offer no way to wrap a Reader as an InputStream, but the following SO question and its answers offer some solutions. Be careful to read the fine-print!

http://stackoverflow.com/questions/62241/how-to-convert-a-reader-to-inputstream-and-a-writer-to-outputstream

However, it is simpler to just stick with what you have got, (if this is just unit test code ... as it appears to be), or to replace the StringInputStream with a ByteArrayInputStream as described by @Thilo's answer.

Stephen C
+2  A: 

FileSaveService.saveFileDialog takes an InputStream, not a Reader (because it wants binary data, not character data).

StringBufferInputStream is deprecated because it does not convert characters into bytes properly.

You can use ByteArrayInputStream instead:

new ByteArrayInputStream("the string".getBytes("UTF-8"))
Thilo
The key point is the InputStream needs to know how to encode your bytes.
Matt
Yes, you need to pick an encoding (UTF-8 in the example above).
Thilo