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)?

ZimZim
  • 3,291
  • 10
  • 49
  • 67
  • Why do you still need to know the size of the byte array here? What are you trying to do? – Jeffrey Jul 21 '12 at 19:48
  • 2
    possible duplicate of [Convert InputStream to byte\[\] in Java](http://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-in-java) – Piotr Praszmo Jul 21 '12 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? – ZimZim Jul 21 '12 at 19:51
  • @Banthar, there's my problem. Like I specified in the title, I don't actually know the size, or anything close to the size of the byte array. I can't give it a number like 16384 because it might be bigger than that, and I don't want to initialize an array with a huge number either because it could be smaller. – ZimZim Jul 21 '12 at 19:53
  • please look at banthar's answer – SJuan76 Jul 21 '12 at 19:55
  • 2
    You need an efficient `ArrayList` `ByteArrayOutputStream` is exactly that. `16384` is just a buffer. It doesn't has to be that big. – Piotr Praszmo Jul 21 '12 at 19:57
  • But what if it's bigger? That's my entire issue. Will sending a 50 mb file still work if I initialize it as a 16384 byte array? – ZimZim Jul 21 '12 at 20:11
  • 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](http://docs.oracle.com/javase/1.4.2/docs/api/java/io/ByteArrayOutputStream.html) will grow as necessary. [toByteArray](http://docs.oracle.com/javase/1.4.2/docs/api/java/io/ByteArrayOutputStream.html#toByteArray\(\)) will copy the data to new array with the exact size. – Piotr Praszmo Jul 21 '12 at 20:20

2 Answers2

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);
}
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • 1
    @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. – user207421 Jul 22 '12 at 02:01
  • What's the reason for using 1024 as the size of the buffer? – Trevor Jul 28 '19 at 22:06
  • @Trevor It's an arbitrary number. Your program's performance may be better with a different buffer size – Jeffrey Jul 29 '19 at 22:44
-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

Community
  • 1
  • 1
java_xof
  • 439
  • 4
  • 16
  • 3
    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. – Jon Skeet Jul 21 '12 at 20:08