diff --git a/glusterfs-java-filesystem/src/main/java/com/peircean/glusterfs/GlusterFileChannel.java b/glusterfs-java-filesystem/src/main/java/com/peircean/glusterfs/GlusterFileChannel.java index a03ba28..825e29d 100644 --- a/glusterfs-java-filesystem/src/main/java/com/peircean/glusterfs/GlusterFileChannel.java +++ b/glusterfs-java-filesystem/src/main/java/com/peircean/glusterfs/GlusterFileChannel.java @@ -166,7 +166,7 @@ long readHelper(ByteBuffer[] byteBuffers, int offset, int length) throws IOExcep public int write(ByteBuffer byteBuffer) throws IOException { guardClosed(); byte[] buf = byteBuffer.array(); - int written = GLFS.glfs_write(fileptr, buf, buf.length, 0); + int written = GLFS.glfs_write(fileptr, buf, byteBuffer.limit(), 0); if (written < 0) { throw new IOException(UtilJNI.strerror()); } @@ -189,17 +189,16 @@ public long write(ByteBuffer[] byteBuffers, int offset, int length) throws IOExc } long totalWritten = 0L; - for (int i = offset; i < length + offset; i++) { int remaining = byteBuffers[i].remaining(); while (remaining > 0) { byte[] bytes = byteBuffers[i].array(); - int written = GLFS.glfs_write(fileptr, bytes, remaining, 0); + int written = GLFS.glfs_write(fileptr, bytes, remaining, byteBuffers[i].position()); if (written < 0) { throw new IOException(); } position += written; - byteBuffers[i].position(written); + byteBuffers[i].position(byteBuffers[i].position() + written); totalWritten += written; remaining = byteBuffers[i].remaining(); } diff --git a/glusterfs-java-filesystem/src/test/java/com/peircean/glusterfs/GlusterFileChannelTest.java b/glusterfs-java-filesystem/src/test/java/com/peircean/glusterfs/GlusterFileChannelTest.java index 1f06449..0a4dac6 100644 --- a/glusterfs-java-filesystem/src/test/java/com/peircean/glusterfs/GlusterFileChannelTest.java +++ b/glusterfs-java-filesystem/src/test/java/com/peircean/glusterfs/GlusterFileChannelTest.java @@ -568,6 +568,7 @@ public void testWrite1Arg() throws IOException { when(GLFS.glfs_write(fileptr, bytes, bufferLength, 0)).thenReturn(bufferLength); doReturn(bytes).when(mockBuffer).array(); + doReturn(bufferLength).when(mockBuffer).limit(); doReturn(null).when(mockBuffer).position(bufferLength); int written = channel.write(mockBuffer);