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();
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.
source share