• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help: inputstream to String or StringBuffer  RSS feed

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably a really simple problem if you know much about input streams at all. I'm reading some bytes streaming in over HTTP - basically, I read the InputStream and just write it out to a file, and later I read in that file into an XML SAX parser like so:



I'm trying to modify this code so that I when I read in the InputStream I can just write the bytes to a StreamBuffer or something that I can hold in memory, because I would prefer to store that info until I run it through the parser so that I can more appropriately name the file I store (based on info that the XML parser extracts for me). Here is my current code that reads the InputStream and writes it to a file:




Not the most elegant way to handle this, I'm sure...how I can write those bytes to something else in memory and just hang onto them and pass them to the parser and then write them out to a file once the parser has given me the item # out of the file that I need to properly name the file? Thanks!
 
Van Glass
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using a ByteArrayOutputStream which is an in memory output stream.

e.g.

ByteArrayOutputStream bout = new ByteArrayOutputStream();

while((i = in.read()) != -1) {
bout.write(i);
}

byte[] data = bout.toByteArray();
String str = new String(data);

Hope that helps
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just what I needed. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!