Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

fix write operation #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down