The Super Secret InputStream
According to the log file, some poor soul stumbled across my blog while searching for covert inputstream to string on Google. And sure enough ... second site. Sweet.
Anyway, going out on a limb here, I think he was probably meaning convert. For the benefit of Google indexing, I'll re-state the question: How do I convert an InputStream to a String? Glad you asked.
Now, in "converting" an InputStream to a String, one must understand the limitations. An InputStream could be getting data from one of any number of sources, including a file. That file could be your recent DVD rip of the 194 minute movie, Titanic, weighing in at some 8GBs (I'm told). In most circumstances, it would be a bad idea to try to read that file into main memory.
But life isn't about following rules, so, here:
public String inputStreamToString (InputStream is) throws Exception {
StringBuffer sb = new StringBuffer();
byte[] bytes = new byte[50000];
int rleng = 0;
while (-1 != (rleng = is.read(bytes, 0, 50000)))
sb.append( new String(bytes,0,rleng) );
return sb.toString();
}
Use only as directed. Not for topical use. Contact a doctor if inhaled. You get what you pay for. Now click on the Google Ads link at the top!
Update: someone else searched for fileoutputstream max characters and arrived here. I'm fourth on that list, and I'll get to providing an answer later.