Java IO Performance XOR with 2 files

I'm having some problems with Java IO performance.

First, I read my performance tips here that I tried to implement.

But here is my problem:

With small files (up to 400 MB) it is pretty fast. But the real files that I will work with are about 30 GB. And with that, he slows down like hell.

What he does: take 2 files, make an exclusive one or write a new file.

BTW: Don't worry about the file cut out at the end. This is just to fix a small mistake that I have not found yet.

Hope someone has a clue for me. Thank.

Regards Timo

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

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

        final long start = System.currentTimeMillis();
        // Init. FileChannel1
        final File file1 = new File("/home/tmann/Downloads/test/1.zip");
        final RandomAccessFile fis1 = new RandomAccessFile(file1, "rw");
        final FileChannel ch1 = fis1.getChannel();

        // Init. FileChannel2
        final File file2 = new File("/home/tmann/Downloads/test/2.zip");
        final RandomAccessFile fis2 = new RandomAccessFile(file2, "rw");
        final FileChannel ch2 = fis2.getChannel();
        // Init FileChannel3
        final File file3 = new File("/home/tmann/Downloads/test/32.zip");
        final RandomAccessFile fis3 = new RandomAccessFile(file3, "rw");
        final FileChannel ch3 = fis3.getChannel();
        // Init ByteBuffer1

        final ByteBuffer bytebuffer1 = ByteBuffer.allocate(65536);
        // Init ByteBuffer2
        final ByteBuffer bytebuffer2 = ByteBuffer.allocate(65536);
        // Init ByteBuffer3
        final ByteBuffer bytebuffer3 = ByteBuffer.allocate(65536);


        int byte1 = 0;
        int byte2 = 0;

        final byte[] array1 = bytebuffer1.array();
        final byte[] array2 = bytebuffer2.array();
        final byte[] array3 = bytebuffer3.array();

        int count = 0;

        while (byte1 != -1) {
            byte1=ch1.read(bytebuffer1);
            byte2=ch2.read(bytebuffer2);
                while (count < byte1) {
                        array3[count] = (byte) (array1[count] ^ array2[count]);
                        count++;
                                        }

           bytebuffer3.put(array3);

            bytebuffer1.flip();
            bytebuffer2.flip();
            bytebuffer3.flip();

                while (bytebuffer3.hasRemaining()) {
                        ch3.write(bytebuffer3);
                                                    }

            bytebuffer1.clear();
            bytebuffer2.clear();
            bytebuffer3.clear();

        }

        fis3.setLength(fis3.length()-59858);
        final long endvar = System.currentTimeMillis();
        System.out.println((500.0 / ((endvar - start) / 1000f)) + "MB/s");

    }
}
+5
source share
1 answer

Why do you need a RandomAccessFile to read it sequentially?

BufferedInputStream FileInputStream?

+2

All Articles