Skip to content

Commit

Permalink
Change
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim-Brooks committed May 3, 2024
1 parent af6f41f commit 0d1bce4
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,29 @@ public static int copyToCacheFileAligned(IO fc, InputStream input, int fileChann
if (bytesRead <= 0) {
break;
}
bytesCopied += copyToCacheFileAligned(fc, fileChannelPos + bytesCopied, buffer);
bytesCopied += copyBufferToCacheFileAligned(fc, fileChannelPos + bytesCopied, buffer);
progressUpdater.accept(bytesCopied);
}
return bytesCopied;
}

public static int copyToCacheFileAligned(IO fc, int fileChannelPos, ByteBuffer buffer) throws IOException {
/**
* Copy all bytes from {@code buffer} to {@code fc}, only doing writes aligned along {@link #PAGE_SIZE}.
*
* @param fc output cache file reference
* @param fileChannelPos position in {@code fc} to write to
* @param buffer bytebuffer to copy from
* @return the number of bytes copied
* @throws IOException on failure
*/
public static int copyBufferToCacheFileAligned(IO fc, int fileChannelPos, ByteBuffer buffer) throws IOException {
if (buffer.hasRemaining()) {
// ensure that last write is aligned on 4k boundaries (= page size)
// ensure the write is aligned on 4k boundaries (= page size)
final int remainder = buffer.position() % PAGE_SIZE;
final int adjustment = remainder == 0 ? 0 : PAGE_SIZE - remainder;
buffer.position(buffer.position() + adjustment);
}
int bytesCopied = positionalWrite(fc, fileChannelPos, buffer);
return bytesCopied;
return positionalWrite(fc, fileChannelPos, buffer);
}

private static int positionalWrite(IO fc, int start, ByteBuffer byteBuffer) throws IOException {
Expand Down

0 comments on commit 0d1bce4

Please sign in to comment.