Is it possible to copy files without using a stream buffer?

Asked

Viewed 161 times

4

In my application, I have a class that periodically performs a backup of a file, considerably small (less than 1MB), but I am doing some tests and after reading this answer, it seems to me that it has been suggested that you can make copies without having to use buffer. With the code below, I’m using buffer of 4kb, and the result until it is quite satisfactory.

Follow the code I’m using:

private static void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst); 
    byte[] buf = new byte[4096];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

It is possible to make this copy without using buffer? Remove the buffer really optimizes file copy?

  • Do you want to use something ready or do the algorithm in hand?

  • @Good mustache, initially I would like to understand, even if it is in a summarized form, the functioning. Of course an example wouldn’t be all bad, even if it doesn’t have much to do with what I exemplified.

  • 1

    Is that the question is not clear on this, if it is something ready, is already answered, you could put what you have already done in chat. If you want to know how to do it manually, the question doesn’t talk about it.

  • @bigown what I wanted to know is if it is possible, using inputStream and outputStream, to make the copy without having to use buffer, that code there that I showed is using filechannel.

  • It is possible, as you have already read in the OS, basically have to access the DMA, I do not know how to do this in Java. But I think in this case can not use stream. I find it strange to wear a stream , if not using memory.

  • @bigown my question is this: if it was possible, and if there’s performance benefit in that.

Show 1 more comment

1 answer

4


In my conception, after some evaluation, if I understood the question, is not possible.

The copy without the use of buffer is possible using DMA, so it doesn’t even go through your memory. This operation needs to be atomic, that is, it cannot be a reading and then a writing, it has to be something simultaneous (even), it has to be something done by the hardware under command of the operating system. I do not know if Java gives access to DMA, and if this is confirmed, I would have to do something in C by calling the specific API and exposing it to Java, probably with JNI.

But how streams, at least those available in the Java API, must be read or write, a buffer. With this API is not possible, you would need to use an API that does not require an intermediate process.

I believe it is only possible to do with FileChannel. Even the one I can’t say uses DMA. The documentation doesn’t make anything clear about this and maybe it’s implementation detail.

Other options give clues that use buffer.

Remove the buffer certainly gives a great performance gain since it does not need to go through memory, just do not expect miracles, after all memory is fast and secondary storage (still) is slow.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.