Convert Stream to String Java/Groovy -


i stole snippet off web. looks limited 4096 bytes , quite ugly imo. know of better approach? i'm using groovy btw...

string streamtostring(inputstream input) {         stringbuffer out = new stringbuffer();         byte[] b = new byte[4096];         (int n; (n = input.read(b)) != -1;) {             out.append(new string(b, 0, n));         }         return out.tostring();     } 

edit:

i found better solution in groovy:

inputstream exporttemplatestream = getclass().getclassloader().getresourceasstream("export.template") assert exporttemplatestream: "[export.template stream] resource not found" string exporttemplate = exporttemplatestream.text 

some , fast answers. think best 1 groovy has added "gettext" method inputstream. had stream.text. , call on 4096 comment.


Comments