Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding enter to inputstream  RSS feed

 
Igor Ba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can I add 'Enter' to the end of Inputstram.
My method receives inputstream and only thing I need to do is to add an Enter/EOL at the end of Inputstream.

thanks,

Igor
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print the string "\n" for Unix, "\r" for MacOS, "\r\n" for Windows.
Or just print System.getProperty("line.separator") which will resolve to one of the above.
 
Igor Ba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Print the string "\n" for Unix, "\r" for MacOS, "\r\n" for Windows.
Or just print System.getProperty("line.separator") which will resolve to one of the above.


Hi
Yes, exactly this strings I need to add.
But my problem is: How to add this String to existing InputStream object ?
(InputStream has only 'read' methods).

Thanks.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can't directly.

However, you might be able to wrap your InputStream in a custom InputStream. This class which you'll have to write sometimes returns the enter (as bytes), otherwise it returns the value of the wrapped InputStream's read() method.

Keep in mind though that a) you also have to take into account the other read methods, and b) how to determine when to return the enter.
 
Igor Ba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need something like this.

StringBuffer sb = new StringBuffer("Hello world.");
sb.append(System.getProperty("line.separator"));

Instead of StringBuffer I have InputStream.
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could read the entire input stream into a ByteArrayOutputStream, append your data and return a corresponding input stream, but that would be memory intensive (to the point of crashing the VM in large files). I like Rob's idea better.
 
Igor Ba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Well you can't directly.

However, you might be able to wrap your InputStream in a custom InputStream. This class which you'll have to write sometimes returns the enter (as bytes), otherwise it returns the value of the wrapped InputStream's read() method.

Keep in mind though that a) you also have to take into account the other read methods, and b) how to determine when to return the enter.


Ok,
thanks.
I will try this solution with StreamWrapper although the b) is pretty tough
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!