In Java, an InputStream is an abstract class in the java.io package that provides a way to read data sequentially as a stream of bytes. It is a foundation for various input stream implementations, allowing data to be read from sources such as files, network connections, and memory buffers.
Common Implementations of InputStream:
Converting InputStream to String: An InputStream can be converted to a String in Java using various approaches. Some of them are explained below:
import java.io.*;
public class InputStreamToString {
public static String convert(InputStream iStream) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
StringBuilder stringBuilder = new StringBuilder()) {
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
return stringBuilder.toString();
}
}
public static void main(String[] args) throws IOException {
InputStream inputStream = new ByteArrayInputStream("Hello, world!".getBytes());
System.out.println(convert(inputStream));
}
}
import java.io.*;
import java.util.Scanner;
public class InputStreamToString {
public static String convert(InputStream iStream) {
try (Scanner scanner = new Scanner(iStream).useDelimiter("\\A")) {
return scanner.hasNext() ? scanner.next() : "";
}
}
public static void main(String[] args) {
InputStream inputStream = new ByteArrayInputStream("Hello, world!".getBytes());
System.out.println(convert(inputStream));
}
}
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
public class InputStreamToString {
public static String convert(InputStream iStream) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(iStream, StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
InputStream inputStream = new ByteArrayInputStream("Hello, world!".getBytes(StandardCharsets.UTF_8));
System.out.println(convert(inputStream));
}
}
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class InputStreamToString {
public static String convert(InputStream iStream) throws IOException {
return IOUtils.toString(iStream, StandardCharsets.UTF_8);
}
public static void main(String[] args) throws IOException {
InputStream inputStream = new ByteArrayInputStream("Hello, world!".getBytes(StandardCharsets.UTF_8));
System.out.println(convert(inputStream));
}
}
Maven Dependency for Apache Commons IO:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
import java.io.*; public class InputStreamToString { public static String convert(InputStream iStream) throws IOException { ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = iStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8"); } public static void main(String[] args) throws IOException { InputStream inputStream = new ByteArrayInputStream("Hello, world!".getBytes("UTF-8")); System.out.println(convert(inputStream)); } }
Converting an InputStream to a String in Java can be achieved through multiple approaches, each with advantages depending on the use case.
Ultimately, the best approach depends on your specific needs—whether it's performance, simplicity, or compatibility with third-party libraries.