Makks129
Makks129

Reputation: 582

Mechanics of splitting and connecting back file using Binary I/O in Java

I've encountered the problem with connecting back .dat files that where made by splitting some file using Binary I/O.

The problem arises with some types of files, for example, with .avi files (not more than 2GB). When connected back the output file seems to be exactly the same as the file that was split, but for .avi files the "Cannot render the file" error occures. (The same thing happens if you are making a copy of the file using Binary I/O). However, for example, .mp4 files are connected back properly.

My question is why that happens? Because as I was taught - any file is just a sequence of 0 and 1. So if you just rewrite the binary sequence of the file and set the same file format - everything should work fine.

Just in case, here is my code for splitting and connecting back a file (it works fine):

--- Splitter ---

public static void main(String[] args) throws IOException {

    String sourceFile = "Movie2.mp4";
    int parts = 5;

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(sourceFile));
    BufferedOutputStream out;

    int partSize = in.available() / parts;
    byte[] b;       

    for (int i = 0; i < parts; i++) {
        if (i == parts - 1) {                
            b = new byte[in.available()];
            in.read(b);
            out = new BufferedOutputStream(new FileOutputStream(sourceFile + "_" + i + ".dat"));
            out.write(b);
            out.close();
        } else {
            b = new byte[partSize];
            in.read(b);
            out = new BufferedOutputStream(new FileOutputStream(sourceFile + "_" + i + ".dat"));
            out.write(b);
            out.close();
        }
    }

    in.close();
}

--- Connector ---

public static void main(String[] args) throws IOException {

    String sourceFile;
    BufferedInputStream in;
    byte[] b;
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Movie2RESTORED.mp4", true));

    for (int i = 0; i < 5; i++) {
        sourceFile = "Movie2.mp4_" + i + ".dat";
        in = new BufferedInputStream(new FileInputStream(sourceFile));
        b = new byte[in.available()];
        in.read(b);
        out.write(b);
        in.close();
    }

    out.close();
}

Thanks in advance!

Upvotes: 2

Views: 687

Answers (1)

Joni
Joni

Reputation: 111239

You need a loop around in.read(b).

Even if available is supposed to return the number of bytes that could be read without blocking, you may have to call read several times to get all of them. This would be easier using a buffer with a fixed size, but if you insist on reading the number of bytes given by available:

int toBeRead = in.available();
byte[] b = new byte[toBeRead];
int totalRead = 0;
int read;
while ((read = in.read(b, totalRead, toBeRead-totalRead)) != -1) {
    totalRead += read;
}

Also, like mentioned by aetheria, you should call close to close the various output streams. Otherwise data that is kept in JVM and OS buffers may not get into the files, although it seems like currently they do.

Upvotes: 3

Related Questions