Guava equivalent for IOUtils.toString(InputStream)

前端 未结 9 1077
深忆病人
深忆病人 2020-12-04 16:00

Apache Commons IO has a nice convenience method IOUtils.toString() to read an InputStream to a String.

Since I am trying to move away from Apache Common

相关标签:
9条回答
  • 2020-12-04 16:36

    Nearly. You could use something like this:

    InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier
        (streamSupplier, Charsets.UTF_8);
    String text = CharStreams.toString(readerSupplier);
    

    Personally I don't think that IOUtils.toString(InputStream) is "nice" - because it always uses the default encoding of the platform, which is almost never what you want. There's an overload which takes the name of the encoding, but using names isn't a great idea IMO. That's why I like Charsets.*.

    EDIT: Not that the above needs an InputSupplier<InputStream> as the streamSupplier. If you've already got the stream you can implement that easily enough though:

    InputSupplier<InputStream> supplier = new InputSupplier<InputStream>() {
        @Override public InputStream getInput() {
            return stream;
        }
    };
    
    0 讨论(0)
  • 2020-12-04 16:37

    If you've got a Readable you can use CharStreams.toString(Readable). So you can probably do the following:

    String string = CharStreams.toString( new InputStreamReader( inputStream, "UTF-8" ) );
    

    Forces you to specify a character set, which I guess you should be doing anyway.

    0 讨论(0)
  • 2020-12-04 16:37

    There is much shorter autoclosing solution in case when input stream comes from classpath resource:

    URL resource = classLoader.getResource(path);
    byte[] bytes = Resources.toByteArray(resource);
    String text = Resources.toString(resource, StandardCharsets.UTF_8);
    

    Uses Guava Resources, inspired by IOExplained.

    0 讨论(0)
  • 2020-12-04 16:42

    Based on the accepted answer, here is a utility method that mocks the behavior of IOUtils.toString() (and an overloaded version with a charset, as well). This version should be safe, right?

    public static String toString(final InputStream is) throws IOException{
        return toString(is, Charsets.UTF_8);
    }
    
    
    public static String toString(final InputStream is, final Charset cs)
    throws IOException{
        Closeable closeMe = is;
        try{
            final InputStreamReader isr = new InputStreamReader(is, cs);
            closeMe = isr;
            return CharStreams.toString(isr);
        } finally{
            Closeables.closeQuietly(closeMe);
        }
    }
    
    0 讨论(0)
  • 2020-12-04 16:44

    UPDATE: Looking back, I don't like my old solution. Besides it is 2013 now and there are better alternatives available now for Java7. So here is what I use now:

    InputStream fis = ...;
    String text;
    try (  InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8)){
            text = CharStreams.toString(reader);
    }
    

    or if with InputSupplier

    InputSupplier<InputStreamReader> spl = ...
    try (  InputStreamReader reader = spl.getInput()){
            text = CharStreams.toString(reader);
        }
    
    0 讨论(0)
  • 2020-12-04 16:49

    Another option is to read bytes from Stream and create a String from them:

    new String(ByteStreams.toByteArray(inputStream))
    new String(ByteStreams.toByteArray(inputStream), Charsets.UTF_8)
    

    It's not 'pure' Guava, but it's a little bit shorter.

    0 讨论(0)
提交回复
热议问题