I'm working on a TCP server project and I need to transfer a file via a byte[] using a DataOutputStream and a DataInputStream and I've gotten stuck. I'm able to write the byte array to the output stream with no problems, but I was looking at the documentation for the DataInputStream and the method that it seems I would want is read(byte[] b). The issue is that this requires I create a byte[] beforehand, but I don't necessarily know what the size it needs to be so I need some help on how I should go about doing this.
Asked
Active
Viewed 1,388 times
0
-
You don't care. Just use the standard copy loop with a byte array of 8192 or whatever you like. – user207421 Feb 10 '17 at 20:31
-
1Send the size first, then on the receiver, read the size and build the array – David Zimmerman Feb 10 '17 at 20:47
-
where is your code so that i can help you – nackolysis Feb 11 '17 at 05:47
3 Answers
0
You don't. All you need is the standard Java copy loop:
byte[] buffer = new byte[8192]; // or wherever you like > 0
int count;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}

user207421
- 305,947
- 44
- 307
- 483