How do I convert an input stream into a byte array

0 votes
I want to read an input stream into a byte array. what is the best way to do that.
Apr 25, 2018 in Java by Akrati
• 3,150 points
759 views

2 answers to this question.

0 votes

There is a simple solution without the need for importing any 3rd party library.

InputStream inStream;
byte[] array = inStream.readAllBytes();

This method was introduced in Java 9 .

answered Apr 25, 2018 by developer_1
• 3,300 points
0 votes

You could probably try this if you are using google Guava.

byte[] bytes = ByteStreams.toByteArray(inputStream);

answered Aug 29 by Sirajul
• 40,990 points

Related Questions In Java

0 votes
2 answers

How to convert a java string into byte array

Try using String.getBytes(). It returns a byte[] ...READ MORE

answered Aug 22 in Java by Sirajul
• 40,990 points
87 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,880 points
4,900 views
+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,150 points
74 views
0 votes
2 answers

How to convert a byte into Hexadecimal in Java?

private static final String ...READ MORE

answered Aug 29 in Java by Sirajul
• 40,990 points
66 views
0 votes
3 answers

How to convert File to Byte[] in Java?

import java.io.File; import java.nio.file.Files; File file; // ...(file is initialised)... byte[] ...READ MORE

answered Aug 14, 2018 in Java by samarth295
• 2,190 points
101 views
0 votes
1 answer

Convert String type to byte[] in Java

Try using String.getBytes(). It returns a byte[] ...READ MORE

answered May 8, 2018 in Java by Rishabh
• 3,540 points
105 views
+2 votes
10 answers

Convert Java Byte Array - String - Byte Array

Character Stream Vs Byte Stream Character oriented are ...READ MORE

answered Sep 25, 2018 in Java by carldivin
• 180 points
3,749 views
0 votes
2 answers

How can I convert byte array into hex string in Java?

public static String byteArrayToHex(byte[] a) { ...READ MORE

answered Aug 29 in Java by Sirajul
• 40,990 points
626 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,880 points
165 views
0 votes
2 answers

what is the best way to convert an ArrayList to a String

You could probably use the Joiner class ...READ MORE

answered Aug 19 in Java by Sirajul
• 40,990 points
82 views