How to convert an InputStream to a String in Java?

11 ways to popular Java issue (with code examples)
15 August 2017   7758

For example, if you have object, what should you do to process that object and produce a String?

We've made a research and found 11 ways to solve this task.

  1. With IOUtils.toString (Apache Utils)

    String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
  2. With CharStreams (guava)

    String result = CharStreams.toString(new InputStreamReader(
          inputStream, Charsets.UTF_8));
  3. Using Scanner (JDK)

    Scanner s = new Scanner(inputStream).useDelimiter("\\A");
    String result = s.hasNext() ? : "";
  4. With Stream Api (Java 8).
    Warning: This solution convert different line breaks (like \r\n) to \n.

    String result = new BufferedReader(new InputStreamReader(inputStream))
  5. With parallel Stream Api (Java 8). 
    Warning: This solution convert different line breaks (like \r\n) to \n.

    String result = new BufferedReader(new InputStreamReader(inputStream)).lines()
  6. Using InputStreamReader and StringBuilder (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 =, 0, buffer.length);
        if (rsz < 0)
        out.append(buffer, 0, rsz);
    return out.toString();
  7. Using StringWriter and IOUtils.copy (Apache Commons)

    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, "UTF-8");
    return writer.toString();
  8. Using ByteArrayOutputStream and (JDK) (solution with the best performance).

    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = != -1) {
        result.write(buffer, 0, length);
    // > JDK 7
    return result.toString("UTF-8");
  9. Using BufferedReader (JDK). Warning: This solution convert different line breaks (like \n\r) to line.separator system property (for example, in Windows to "\r\n").

    String newLine = System.getProperty("line.separator");
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder result = new StringBuilder();
    String line; boolean flag = false;
    while ((line = reader.readLine()) != null) {
        result.append(flag? newLine: "").append(line);
        flag = true;
    return result.toString();
  10. Using BufferedInputStream and ByteArrayOutputStream (JDK)

    BufferedInputStream bis = new BufferedInputStream(inputStream);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result =;
    while(result != -1) {
        buf.write((byte) result);
        result =;
    // > JDK 7
    return buf.toString("UTF-8");
  11. Using and StringBuilder (JDK). Warning: This solution has problem with Unicode, for example with Russian text (work correctly only with non-Unicode text)

    int ch;
    StringBuilder sb = new StringBuilder();
    while((ch = != -1)
    return sb.toString();


Android-x86 8.1 Released

Let's see what team has made in the fresh release of Android to x86 port
17 January 2019   173

The team of the Android-x86 project, which is the independent community porting the Android platform for the x86 architecture, have published the first stable release of the assembly based on the Android 8.1 platform, which includes fixes and additions that ensure seamless operation on x86-based platforms. Universal Android-x86 8.1 Live-builds for x86 32-bit and x86_64 architectures suitable for use on typical laptops and tablet PCs are prepared for download. In addition, assemblies in the form of rpm-packages are available for installing the Android environment in Linux distributions.

Let's check new features:

  • Support both 64-bit and 32-bit kernel and userspace with latest LTS kernel 4.19.15.
  • Support OpenGL ES 3.x hardware acceleration for Intel, AMD, Nvidia and QEMU(virgl) by Mesa 18.3.1.
  • Support OpenGL ES 2.0 via SwiftShader for software rendering on unsupported GPU devices.
  • Support hardware accelerated codecs on devices with Intel HD & G45 graphics family.
  • Support secure booting from UEFI and installing to UEFI disk.
  • A text based GUI installer.
  • Add theme support to GRUB-EFI.
  • Support Multi-touch, Audio, Wifi, Bluetooth, Sensors, Camera and Ethernet (DHCP only).
  • Auto-mount external usb drive and sdcard.
  • Add Taskbar as an alternative launcher which puts a start menu and recent apps tray on top of your screen and support freeform window mode.
  • Enable ForceDefaultOrientation on devices without known sensors. Portrait apps can run in a landscape device without rotating the screen.
  • Support arm arch apps via the native bridge mechanism. (Settings -> Android-x86 options)
  • Support to upgrade from non-official releases.
  • Add experimental Vulkan support for newer Intel and AMD GPUs. (Boot via Advanced options -> Vulkan support)
  • Mouse integration support for VMs including VirtualBox, QEMU, VMware and Hyper-V.

Get more infoand download links at official website of the project.