Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace StringBufferInputStream with StringReader?

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 ?

like image 789
Frank Avatar asked Feb 08 '10 03:02

Frank


2 Answers

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")) 
like image 99
Thilo Avatar answered Oct 02 '22 12:10

Thilo


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!

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.

like image 42
Stephen C Avatar answered Oct 02 '22 14:10

Stephen C