开发者

Convert Stream to String Java/Groovy

I stole this snippet off the web. But it looks to be limited to 4096 bytes and is quite ugly IMO. Anyone know of a better approach? I'm actually using Groovy btw...

String streamToString(InputStream input) {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = input.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
        return out.toString();
    }

EDIT:

I found a better solution in Groovy:

InputStream export开发者_JAVA百科TemplateStream = getClass().getClassLoader().getResourceAsStream("export.template")
assert exportTemplateStream: "[export.template stream] resource not found"
String exportTemplate = exportTemplateStream.text


Some good and fast answers. However I think the best one is Groovy has added a "getText" method to InputStream. So all I had to do was stream.text. And good call on the 4096 comment.


For Groovy

filePath = ... //< a FilePath object
stream = filePath.read() //< InputStream object

// Specify the encoding, and get the String object
//content = stream.getText("UTF-16") 
content = stream.getText("UTF-8") 

The InputStream class reference

The getText() without encoding, it will use current system encoding, ex ("UTF-8").


Try IOUtils from Apache Commons:

String s = IOUtils.toString(inputStream, "UTF-8");


It's reading the input in chunks of 4096 bytes(4KB), but the size of the actual string is not limited as it keeps reading more and appending it to the SringBuffer.


You can do it fairly easily using the Scanner class:

String streamToSring(InputStream input) {
    Scanner s = new Scanner(input);
    StringBuilder builder = new StringBuilder();
    while (s.hasNextLine()) {
        builder.append(s.nextLine() +"\n");
    }
    return builder.toString();
}


That snippet has a bug: if the input uses a multi-byte character encoding, there's a good chance that a single character will span two reads (and not be convertable). And it also has the semi-bug that it relies on the platform's default encoding.

Instead, use Jakarta Commons IO. In particular, the version of IOUtils.toString() that takes an InputStream and applies an encoding to it.


For future reviewers who have similar problems, please note that both IOUtils from Apache, and Groovy's InputStream.getText() method require the stream to complete, or be closed before returning. If you are working with a persistent stream you will nead to deal with the "ugly" example that Phil originally posted, or work with non-blocking IO.


You can try something similar to this

new FileInputStream( new File("c:/tmp/file.txt") ).eachLine { println it }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜