1

I'm sending a byte array through a socket and I'm receiving it through a DataInputStream. I don't know the size of the byte array, and there's no way to check. I've tried doing this:

byte[] content = new byte[ARRAY_SIZE]; 
int something; 

while((something = inFromStream.read()) > 0)  
    output.write(something); 

This however, still means that I need to know the size of the byte array. I don't want to just fill in a gigantic number (since the byte array received from the stream could be 100 or maybe even 5000000000). How do I deal with this (preferably with the standard Java API/libraries)?

8
  • Why do you still need to know the size of the byte array here? What are you trying to do? Commented Jul 21, 2012 at 19:48
  • 2
    possible duplicate of Convert InputStream to byte[] in Java Commented Jul 21, 2012 at 19:50
  • I just said I need a way without having to know the size of the byte array sent through the stream. I obviously need to initialize the array first before I can actually use it. Is there an alternative? Commented Jul 21, 2012 at 19:51
  • 2
    You need an efficient ArrayList<Byte> ByteArrayOutputStream is exactly that. 16384 is just a buffer. It doesn't has to be that big. Commented Jul 21, 2012 at 19:57
  • 1
    Yes, it will work. It will copy up to 16384 bytes at every loop iteration. You can pick different buffer size, it will only affect performance. ByteArrayOutputStream will grow as necessary. toByteArray will copy the data to new array with the exact size. Commented Jul 21, 2012 at 20:20

2 Answers 2

6

You can send the byte[] piece wise to the OutputStream

byte[] buffer = new byte[1024 /* or some other number */];
int numRead;

while((numRead = inputStream.read(buffer)) > 0) {
    outputStream.write(buffer, 0, numRead);
}
Sign up to request clarification or add additional context in comments.

3 Comments

@user1007059 Exactly. You don't need the size at all, because you don't need all the data in memory at all. Reading it all into memory before writing any of it just wastes time and space, and doesn't scale to large inputs.
What's the reason for using 1024 as the size of the buffer?
@Trevor It's an arbitrary number. Your program's performance may be better with a different buffer size
-3

The way around - just logic without code sample

getSizeOfInput int countTokens (){

readInput tokenizeInput countTokens

return countTokens }

create an array with the size of countTokens

But all in all - look at the link create an ArrayList of bytes

1 Comment

That's an horrendously inefficient way of dealing with bytes in Java. Even if all Byte values are autoboxed to cached values (which they will be) you're still using the size of a reference for each byte in the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.