glusterfs-java-filesystem icon indicating copy to clipboard operation
glusterfs-java-filesystem copied to clipboard

fix write operation

Open fubai opened this issue 9 years ago • 2 comments

fubai avatar Oct 18 '16 07:10 fubai

Hi Allen,

Thank you for your contribution. I'm a bit puzzled by this change, and would like to get more information to understand it better.

How does the current code cause problems? Can you show me how to reproduce the issue that this change fixes?

Also, I'd like to improve the test suite to cover this issue. Currently the tests fail with your changes. We'll need to fix the existing test that is failing, and write a new test to cover the problem you've identified. The new test should fail with the old code, and pass with your new changes.

We can work together on the test cases, but I need to better understand what the problem is that you're solving here. I look forward to hearing more about it.

Thanks again!

-louis

louiszuckerman avatar Oct 18 '16 13:10 louiszuckerman

Hi semiosis,

try (OutputStream output = Files.newOutputStream(path, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
    int len;
    byte[] buffer = new byte[8192];
    while ((len = input.read(buffer, 0, 8192)) > 0) {
        output.write(buffer, 0, len);
    }
    output.flush();
}

here I'm using buffer to transfer byte from input to output , where int len is the actual length of input file, so we should write this length of byte to output , not the whole byte everytime.

e.g.

input provide 8200 bytes , so we got buffer filled with 8192 the first time in while loop , with 8 bytes the second time , what we should write is 8192 + 8 , not 8192 *2 as old code.

fubai avatar Oct 19 '16 02:10 fubai