2

Suppose there is a file called input.zip in my folder.

I want to transfer this file from a client to a server, so what I'm currently doing is:

//client side
FileInputStream fis = new FileInputStream("input.zip");
while(fis.read(buffer) > 0) { ... }

In a nutshell: Inside the client, I separate the file in a lot of byte arrays using FileInputStream.read(buffer).

I send each of these arrays to the server and the server know the index of each of the arrays (i.e., the first array will have index 0, the second will have index 1, and so on).

Given that in the server side I have all the byte arrays and I know the order they were sent, I want to build a big byte array to store them all.

How can I build this big byte array and write the file (that should be equals to input.zip) in a file called output.zip?

6
  • 3
    Just write them to a FileOutputStream? What have you tried and why didn't it work? Commented Dec 11, 2018 at 23:10
  • @thatotherguy I changed the question a little bit, I wasn't clear enough. I'm confused at how I should build the big array given all small arrays. Using FileOutputStream is a bit complicated because the arrays come at random time, I'm using UDP transference and threads. Commented Dec 11, 2018 at 23:22
  • In your question, you're saying: "I have all the byte arrays and I know the order they were sent". So, write them, in the order they were sent, to a FileOutputStream (if you want to create a file), or to a ByteArrayOutputStream (if you want to create a byte array). If you actually don't have all the arrays on the server, but instead receive them in separate threads, in a random order, then change your question, explain what is the actual problem and post the relevant server code. Commented Dec 11, 2018 at 23:30
  • @Daniel You can use a RandomAccessFile to seek to the right byte position and write the data there whenever you get a new region Commented Dec 11, 2018 at 23:36
  • 1
    Please include a minimal reproducible example of the code that you are having problems with. Commented Dec 12, 2018 at 1:13

1 Answer 1

1

InputStream and OutputStream are processed sequentially.

for (;;) {
    int nread = fis.read(buffer);
    if.(nread <= 0) {
        break;
    }
    fos.write(buffer, 0, nread);
}

The last read buffer not entirely filled.

The utility class Files will do that and more.

Path path = Paths.get(“...“);
Files.copy(path, fos);
Sign up to request clarification or add additional context in comments.

2 Comments

should I use the lines Path path ... ; Files.copy ... before the for ?
No the for-code is the "repair" of the given code. However alternatively one needs only two lines to copy a file (Path) to an OutputStream. Path is a newer, more general concept for File (which only is for the file system). Files is a class worth knowing.

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.