美文网首页
Java:11 种方法 InputStream => Strin

Java:11 种方法 InputStream => Strin

作者: Athenaearl | 来源:发表于2019-04-23 17:17 被阅读0次

    转自StackOverflow:
    原贴地址:https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java

    从InputStream 获得 String 应该算是比较常见的问题,方法也很多,我在网上看到了一个问答,回答者总结了11种常用的方法并比较了它们的运行效率:

    1. 使用IOUtils.toString (Apache Utils)

    String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    

    2. 使用CharStreams (Guava)

    String result = CharStreams.toString(new InputStreamReader(
          inputStream, Charsets.UTF_8));
    

    3. 使用 Scanner (JDK)

    Scanner s = new Scanner(inputStream).useDelimiter("\\A");
    String result = s.hasNext() ? s.next() : "";
    

    4. 使用 Stream API (Java 8)

    注意:这种方式会把换行(比如:"\r\n")一律转化为"\n"

    String result = new BufferedReader(new InputStreamReader(inputStream))
      .lines().collect(Collectors.joining("\n"));
    

    5. 使用 Parallel Stream API (Java 8)

    注意: 这种方式会把换行(比如:"\r\n")一律转化为"\n"

    String result = new BufferedReader(new InputStreamReader(inputStream)).lines()
       .parallel().collect(Collectors.joining("\n"));
    

    6. 使用 InputStreamReader 和 StreamBuilder (JDK)

    final int bufferSize = 1024;
    final char[] buffer = new char[bufferSize];
    final StringBuilder out = new StringBuilder();
    Reader in = new InputStreamReader(inputStream, "UTF-8");
    for (; ; ) {
        int rsz = in.read(buffer, 0, buffer.length);
        if (rsz < 0)
            break;
        out.append(buffer, 0, rsz);
    }
    return out.toString();
    

    7. 使用StringWriter 和 IOUtils.copy (Apache Commons)

    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, "UTF-8");
    return writer.toString();
    

    8. 使用 ByteArrayOutputStream 和 inputStream.read (JDK)

    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) != -1) {
        result.write(buffer, 0, length);
    }
    // StandardCharsets.UTF_8.name() > JDK 7
    return result.toString("UTF-8");
    

    9. 使用 BufferedReader

    注意:这种方法会把换行(比如:"\n\r")转换成 line.separator 然后在不同的操作系统中转换成不同的形式

    String newLine = System.getProperty("line.separator");
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder result = new StringBuilder();
    boolean flag = false;
    for (String line; (line = reader.readLine()) != null; ) {
        result.append(flag? newLine: "").append(line);
        flag = true;
    }
    return result.toString();
    

    10. 使用 BufferedInputStream 和 ByteArrayOutputStram (JDK)

    BufferedInputStream bis = new BufferedInputStream(inputStream);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result = bis.read();
    while(result != -1) {
        buf.write((byte) result);
        result = bis.read();
    }
    // StandardCharsets.UTF_8.name() > JDK 7
    return buf.toString("UTF-8");
    

    11. 使用 InputStream.read() 和 StringBuilder (JDK)

    注意: 这种方法不能处理unicode (处理不了中文)

    int ch;
    StringBuilder sb = new StringBuilder();
    while((ch = inputStream.read()) != -1)
        sb.append((char)ch);
    reset();
    return sb.toString();
    

    对小的String (长度为175)进行处理,表现:

                 Benchmark                         Mode  Cnt   Score   Error  Units
     8. ByteArrayOutputStream and read (JDK)        avgt   10   1,343 ± 0,028  us/op
     6. InputStreamReader and StringBuilder (JDK)   avgt   10   6,980 ± 0,404  us/op
    10. BufferedInputStream, ByteArrayOutputStream  avgt   10   7,437 ± 0,735  us/op
    11. InputStream.read() and StringBuilder (JDK)  avgt   10   8,977 ± 0,328  us/op
     7. StringWriter and IOUtils.copy (Apache)      avgt   10  10,613 ± 0,599  us/op
     1. IOUtils.toString (Apache Utils)             avgt   10  10,605 ± 0,527  us/op
     3. Scanner (JDK)                               avgt   10  12,083 ± 0,293  us/op
     2. CharStreams (guava)                         avgt   10  12,999 ± 0,514  us/op
     4. Stream Api (Java 8)                         avgt   10  15,811 ± 0,605  us/op
     9. BufferedReader (JDK)                        avgt   10  16,038 ± 0,711  us/op
     5. parallel Stream Api (Java 8)                avgt   10  21,544 ± 0,583  us/op
    

    对大的String (长度为50100) 进行处理,表现:

                   Benchmark                        Mode  Cnt   Score        Error  Units
     8. ByteArrayOutputStream and read (JDK)        avgt   10   200,715 ±   18,103  us/op
     1. IOUtils.toString (Apache Utils)             avgt   10   300,019 ±    8,751  us/op
     6. InputStreamReader and StringBuilder (JDK)   avgt   10   347,616 ±  130,348  us/op
     7. StringWriter and IOUtils.copy (Apache)      avgt   10   352,791 ±  105,337  us/op
     2. CharStreams (guava)                         avgt   10   420,137 ±   59,877  us/op
     9. BufferedReader (JDK)                        avgt   10   632,028 ±   17,002  us/op
     5. parallel Stream Api (Java 8)                avgt   10   662,999 ±   46,199  us/op
     4. Stream Api (Java 8)                         avgt   10   701,269 ±   82,296  us/op
    10. BufferedInputStream, ByteArrayOutputStream  avgt   10   740,837 ±    5,613  us/op
     3. Scanner (JDK)                               avgt   10   751,417 ±   62,026  us/op
    11. InputStream.read() and StringBuilder (JDK)  avgt   10  2919,350 ± 1101,942  us/op
    

    对不同长度的String 进行处理,表现:

    length  182    546     1092    3276    9828    29484   58968
    
     test8  0.38    0.938   1.868   4.448   13.412  36.459  72.708
     test4  2.362   3.609   5.573   12.769  40.74   81.415  159.864
     test5  3.881   5.075   6.904   14.123  50.258  129.937 166.162
     test9  2.237   3.493   5.422   11.977  45.98   89.336  177.39
     test6  1.261   2.12    4.38    10.698  31.821  86.106  186.636
     test7  1.601   2.391   3.646   8.367   38.196  110.221 211.016
     test1  1.529   2.381   3.527   8.411   40.551  105.16  212.573
     test3  3.035   3.934   8.606   20.858  61.571  118.744 235.428
     test2  3.136   6.238   10.508  33.48   43.532  118.044 239.481
     test10 1.593   4.736   7.527   20.557  59.856  162.907 323.147
     test11 3.913   11.506  23.26   68.644  207.591 600.444 1211.545
    

    可见,第8 种方法(使用 ByteArrayOutputStream 和 inputStream.read)效率远优于其他10种

    而,第3种方法(使用 Scanner)在3种实验中表现都不是很好

    相关文章

      网友评论

          本文标题:Java:11 种方法 InputStream => Strin

          本文链接:https://www.haomeiwen.com/subject/fphvgqtx.html