BufferedInputStream for ByteArrayOutputStream is very slow

I have a problem very similar to the link below:

PDF to byte array and vice versa

The main difference is that I'm trying to interpret a Socket connection using a ServerSocket containing a Binary, not a file. This works as expected.

However, the problem is that this process takes quite a while to read in memory, about 1 minute 30 seconds for 500 bytes (although the size of each stream will vary in bulk)

Here is my code:

BufferedInputStream input = new BufferedInputStream(theSocket.getInputStream()); byte[] buffer = new byte[8192]; int bytesRead; ByteArrayOutputStream output = new ByteArrayOutputStream(); while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } byte[] outputBytes = output.toByteArray(); //Continue ... and eventually close inputstream 

If I record its progress in a while loop inside the terminal, it seems to write all the bytes quite quickly (i.e. reaches the end of the stream), but then it seems to temporarily pause until the while loop exits and continues.

Hope this makes sense.

+6
source share
3 answers

Well, you read until the socket is closed, basically - that when read will return -1.

Therefore, I assume that the other end of the connection holds it open for 90 seconds before closing it. Correct it and you will fix your problem.

+5
source
 ByteArrayOutputStream(int size); 

By default, the size is 32 bytes, so it looks like this: 32-> 64-> 128-> 256 β†’ ... Therefore, initialize it with a larger capacity.

0
source

You can specify the time required to copy data between BufferedInputStream and ByteArrayOutputStream.

 int size = 256 << 20; // 256 MB ByteArrayInputStream bais = new ByteArrayInputStream(new byte[size]); long start = System.nanoTime(); BufferedInputStream input = new BufferedInputStream(bais); byte[] buffer = new byte[8192]; int bytesRead; ByteArrayOutputStream output = new ByteArrayOutputStream(); while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } byte[] outputBytes = output.toByteArray(); long time = System.nanoTime() - start; System.out.printf("Took %.3f seconds to copy %,d MB %n", time / 1e9, size >> 20); 

prints

 Took 0.365 seconds to copy 256 MB 

This will be much faster for smaller messages, i.e. <<256 MB.

0
source

All Articles