What I need to do is send multiple files using DataStreams. I’m doing this by sending the name of the file, and then the file’s bytes. I need to send an undetermined number of files though. Here is the DataOutputStream code.
out.writeUTF(path);
out.write(Files.readAllBytes(file.toPath()));
It does that for each file that needs to be sent. But I don’t know how to read it correctly with DataInputStream. This is what I have so far.
while (in.available() != 0) {
String path = in.readUTF();
byte bytes = in.readByte();
}
Obviously it wouldn’t work, since it is only reading one byte. But I don’t know how to make it read all of the bytes. Since there are several files being sent, available() would only equal 0 when the end of all the files are read, I think. Any help is greatly appreciated.
Something I completely forgot to mention, I want to be able to send a large file without running out of memory, and I don’t think this would work. I think I would need to use a buffer, but I don’t know what class supports that with files.
Anytime you send variable length messages you need some way to mark the beginning and end of each method.
Then to read it you would do something like this