這篇文章主要是講怎樣將InputStream轉換爲String。採用[weblink url="http://code.google.com/p/guava-libraries/"]Guava[/weblink]、[weblink url="http://commons.apache.org/proper/commons-io/"]Apache Commons IO [/weblink]以及普通Java代碼實現html
下面就是一個Guava轉換的例子—這裏用的是InputSupplier功能: java
@Test public void givenUsingGuava_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { String originalString = randomAlphabetic(8); InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); InputSupplier<InputStream> inputSupplier = new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return inputStream; } }; InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier(inputSupplier, Charsets.UTF_8); String text = CharStreams.toString(readerSupplier); assertThat(text, equalTo(originalString)); }
來按步棸解釋下: 1. 首先 — 咱們將 InputStream 放入 InputSupplier —這個方法很是簡單。 2. 而後 — 咱們使用 InputStream 的讀取參數—這樣咱們就能夠得到一個字符流。 3. 最後 — 咱們使用 Guava 的 CharStreams 工具來轉化爲String類型。web
注意,咱們最後使用了 CharStreams.toString 也會咱們關閉 inputStream。 apache
下面一個利用 Guava 轉化的方法,是沒用自動關閉 inputStream 的:app
@Test public void givenUsingGuavaAndJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { String originalString = randomAlphabetic(8); InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); String text = null; try (final Reader reader = new InputStreamReader(inputStream)) { text = CharStreams.toString(reader); } assertThat(text, equalTo(originalString)); }
這裏 CharStreams.toString 方法是沒用自動關閉 inputStream 的— 這就是上面爲何咱們使用java7的方法處理的緣由。 dom
咱們如今來使用用 Apache commons IO 組件來轉化 這裏要說名一點,相對於 Guava 來講,其餘的方法都沒有自動關閉 InputStream. ide
@Test public void givenUsingCommonsIo_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { String originalString = randomAlphabetic(8); InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name()); assertThat(text, equalTo(originalString)); }
咱們也能夠使用 StringWriter 來轉換:工具
@Test public void givenUsingCommonsIoWithCopy_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { String originalString = randomAlphabetic(8); InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); StringWriter writer = new StringWriter(); String encoding = StandardCharsets.UTF_8.name(); IOUtils.copy(inputStream, writer, encoding); assertThat(writer.toString(), equalTo(originalString)); }
下面一個利用普通java轉化的例子 — 一個 inputStream 和 StringBuilder:ui
@Test public void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { String originalString = randomAlphabetic(DEFAULT_SIZE); InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); StringBuilder textBuilder = new StringBuilder(); try (Reader reader = new BufferedReader(new InputStreamReader (inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) { int c = 0; while ((c = reader.read()) != -1) { textBuilder.append((char) c); } } assertEquals(textBuilder.toString(), originalString); }
@Test public void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { String originalString = randomAlphabetic(8); InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); String text = null; try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) { text = scanner.useDelimiter("\\A").next(); } assertThat(text, equalTo(originalString)); }
注意,這裏關閉 Scanner 的時候 已經關閉 inputStream 了google
咱們看到,InputStream to String 有不少不一樣的方法。咱們須要作的就是在須要用到的時候選擇一個咱們方便使用的。