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,190 points
3,848 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 26, 2018 by developer_1
• 3,350 points
0 votes

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

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

answered Aug 29, 2019 by Sirajul
• 59,230 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, 2019 in Java by Sirajul
• 59,230 points
1,467 views
0 votes
2 answers

How do I convert a String to an int in Java?

Use the lines of code mentioned below:- String ...READ MORE

answered Feb 9, 2022 in Java by Soham
• 9,710 points
3,209 views
0 votes
1 answer

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,770 points
1,247 views
0 votes
0 answers

How do I check if an array includes a value in JavaScript?

What is the shortest and most efficient ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
607 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 15, 2018 in Java by samarth295
• 2,220 points
2,269 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,620 points
819 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
10,655 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, 2019 in Java by Sirajul
• 59,230 points
2,920 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,920 points
13,192 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, 2019 in Java by Sirajul
• 59,230 points
1,298 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP