Convert Inputstream to Byte Array in Java

Convert InputStream to byte array in Java

You can use Apache Commons IO to handle this and similar tasks.

The IOUtils type has a static method to read an InputStream and return a byte[].

InputStream is;
byte[] bytes = IOUtils.toByteArray(is);

Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray(). It handles large files by copying the bytes in blocks of 4KiB.

Most efficient way to convert InputStream into byte[]?

Yes. Use a ByteArrayOutputStream rather than an ArrayList. Then read chunks of bytes from the InputStream (without using available(), which should almost always never used) and write these chunks to the ByteArrayOutputStream, until the read() method returns -1. Then call toByteArray() on your ByteArrayOutputStream.

You could use Guava's ByteStreams.toByteArray() method, which does all that for you, or you could read its source code to have a better idea of how it does it. Reading the IO tutorial might also help.

In Java, how can I convert an InputStream into a byte array (byte[])?

The simplest way is to create a new ByteArrayOutputStream, copy the bytes to that, and then call toByteArray:

public static byte[] readFully(InputStream input) throws IOException
{
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = input.read(buffer)) != -1)
{
output.write(buffer, 0, bytesRead);
}
return output.toByteArray();
}

Convert InputStream(Image) to ByteArrayInputStream

Read from input stream and write to a ByteArrayOutputStream, then call its toByteArray() to obtain the byte array.

Create a ByteArrayInputStream around the byte array to read from it.

Here's a quick test:

import java.io.*;

public class Test {


public static void main(String[] arg) throws Throwable {
File f = new File(arg[0]);
InputStream in = new FileInputStream(f);

byte[] buff = new byte[8000];

int bytesRead = 0;

ByteArrayOutputStream bao = new ByteArrayOutputStream();

while((bytesRead = in.read(buff)) != -1) {
bao.write(buff, 0, bytesRead);
}

byte[] data = bao.toByteArray();

ByteArrayInputStream bin = new ByteArrayInputStream(data);
System.out.println(bin.available());
}
}

InputStream to byte array

The solution to this is to implement a Caching output stream:

public class CachingOutputStream extends OutputStream {
private final OutputStream os;
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();

public CachingOutputStream(OutputStream os) {
this.os = os;
}

public void write(int b) throws IOException {
try {
os.write(b);
baos.write(b);
} catch (Exception e) {
if(e instanceof IOException) {
throw e;
} else {
e.printStackTrace();
}
}
}

public byte[] getCache() {
return baos.toByteArray();
}

public void close() throws IOException {
os.close();
}

public void flush() throws IOException {
os.flush();
}
}

And do this:

final CachingOutputStream cachingOutputStream = new CachingOutputStream(outputStream);
flow(inputStream, cachingOutputStream, buff);
cached = cachingOutputStream.getCache();
if(cached != null) {
cacheService.put(cacheKey, cached);
}


Related Topics



Leave a reply



Submit