I can not guess at what you may have encountered. This code does work. It would help if you posted a code snippet to show where its broken. Using sockets can cause a number of issues which are not necessarily related to converting an InputStream to a String, or vice-versa.
The input stream => string conversion code isn't quite right: if the last line of the file doesn't end with a newline, the resulting String won't accurately represent the contents of the file.
Fundamentally, StringReader.readLine() is an ugly API.
Although, you may find the API "ugly", it still will accomplish the job most of the time. There are other methods available, if this does not meet your needs.
Basically, parseISToString is on client side and parseStringtoIS is on server side. I do this because InputStream is not serializable and cannot be send across EJB layer. But my customer says he sees weird characters on screen when he includes "Kanji" chinese characters in comments. The outputs of the above two system outs are different when the text file has chinese characters. What does that mean?
I imagine the issue has to do with the fact that the encoding is UTF-8. Since Chinese requires multi-byte to display its glyphs, you will need to use a different encoding. Try using a different encoding like UTF-16.
InputStream iFile = /* get your InputStream here */
String s = ""; try{ while(true){ int x = iFile.read(); // returns -1 at the end of the stream if(x == -1) break; s = s + (char)x; } } catch(Exception e){ e.printStackTrace(); }
12 comments :
Well, it isn't working...I am using sockets and is I try to use this I get a Connection CLose exception.
I can not guess at what you may have encountered. This code does work. It would help if you posted a code snippet to show where its broken. Using sockets can cause a number of issues which are not necessarily related to converting an InputStream to a String, or vice-versa.
Just a word of Thanks!
This works perfect for me. You saved me at least 3 hours :)
I am glad that worked for you. It is validation that the code posted does work.
The input stream => string conversion code isn't quite right: if the last line of the file doesn't end with a newline, the resulting String won't accurately represent the contents of the file.
Fundamentally, StringReader.readLine() is an ugly API.
Although, you may find the API "ugly", it still will accomplish the job most of the time. There are other methods available, if this does not meet your needs.
Thanks for the help!
Hi, My code is this:
File file = new File ( "C:/File.txt" ) ;
boolean isFile = file.exists () ;
InputStream is = new FileInputStream(file) ;
String s = parseISToString(is);
System.out.println("Hi :" + s);
System.out.println("Hi :"+parseISToString(parseStringToIS(s)));
Basically, parseISToString is on client side and parseStringtoIS is on server side. I do this because InputStream is not serializable and cannot be send across EJB layer.
But my customer says he sees weird characters on screen when he includes "Kanji" chinese characters in comments.
The outputs of the above two system outs are different when the text file has chinese characters. What does that mean?
I imagine the issue has to do with the fact that the encoding is UTF-8. Since Chinese requires multi-byte to display its glyphs, you will need to use a different encoding. Try using a different encoding like UTF-16.
Just to add my thanks - very useful!
// A much easier approach
InputStream iFile = /* get your InputStream here */
String s = "";
try{
while(true){
int x = iFile.read();
// returns -1 at the end of the stream
if(x == -1)
break;
s = s + (char)x;
}
} catch(Exception e){
e.printStackTrace();
}
Here is a longer example, but I think it is safer than using char conversions.
String s = "Hello World!";
byte[] buf = s.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
InputStreamReader isr = new InputStreamReader(bais);
BufferedReader br = new BufferedReader(isr);
CharBuffer cb = CharBuffer.allocate(1024);
br.read(cb);
cb.flip();
System.out.println(cb.toString());
Post a Comment