[JAVA] I want to convert InputStream to String

I'm tired of going around every time I need it, so I'll keep it as a memo for myself (´ ・ ω ・ `)

If you want to read all InputStream and convert it to String, you can write roughly as follows.

public static String readAll(InputStream in) throws IOException {
    byte[] b = new byte[1024];
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len;
    while ((len = in.read(b)) != -1) {
        out.write(b, 0, len);
    }
    return out.toString();
}

If the character code of InputStream that you want to convert to String is not UTF-8, you can use ByteArrayOutputStream.toString (Charset charset). For example, if InputStream is Windows-31J, write as follows.

public static String readAll(InputStream in) throws IOException {
    byte[] b = new byte[1024];
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len;
    while ((len = in.read(b)) != -1) {
        out.write(b, 0, len);
    }
    return out.toString(Charset.forName("Windows-31J"));
}

In order to avoid reinventing the wheel, if you can use the OSS library such as IOUtils of Apache Commons, use that (´ ・ ω ・ `)

** Environmental Information: **

C:\>javac -version
javac 11.0.3
C:\>java -version
openjdk version "11.0.3" 2019-04-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.3+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.3+7, mixed mode)

Recommended Posts

I want to convert InputStream to String
I want to convert characters ...
I want to simply write a repeating string
Convert String type to Timestamp type
Swift: I want to chain arrays
[Java] I want to convert a byte array to a hexadecimal number
I want to docker-compose up Next.js!
Convert iso-2022-jp character string to utf-8
I tried to convert a string to a LocalDate type in Java
I want to convert an array to Active Record Relation with Rails
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to develop a web application!
I want to eliminate duplicate error messages
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
I want to write a unit test!
I want to install PHP 7.2 on Ubuntu 20.04.
[Java] Convert Object type null to String type
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
I will absolutely convert the time string!
[Ruby] I want to output only the odd-numbered characters in the character string
I want to find out what character the character string appears from the left
I want to test Action Cable with RSpec test
Convert to a tag to URL string in Rails
Convert from C String pointer to Swift String type
Run R from Java I want to run rJava
[Swift] I want to draw grid lines (squares)
I want to send an email in Java.
I want to graduate from npm install properly [2020]
I want to use arrow notation in Ruby
Convert a string to a character-by-character array with swift
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
I want to play with Firestore from Rails
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I want to reduce simple mistakes. To command yourself.
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to delete files managed by Git
I want to get the value in Ruby
I want to output the character number from the left where an arbitrary character string appears.
<java> Read Zip file and convert directly to string
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
Sample code to convert List to List <String> in Java Stream
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to use NetBeans on Mac → I can use it!
I want to embed any TraceId in the log
URL to String conversion