0

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.

jmon117
  • 65
  • 8

3 Answers3

1

Take a look at Apache Commons IO library. It has a utility class called IOUtils which can read an entire input stream into byte array without you having to declare a byte [] beforehand. For more information look at this

Community
  • 1
  • 1
opensam
  • 368
  • 1
  • 4
  • 10
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
-1

I made a library for TCP server and client. You might as well use it. GitHub

It is not much, but it is simple.

mihibo5
  • 1
  • 1
  • 3