You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a simple test that creates a file and fills it by the transferFrom method.
@Test
fun testBlockSizeFit() {
val blockSize = 4096L
val fs = Jimfs.newFileSystem(
Configuration.unix().toBuilder()
.setBlockSize(4096)
.setMaxSize(blockSize*2)
.build())
val tmpDir = fs.getPath("/tmp/").also { Files.createDirectory(it) }
val tmpFile = Files.createTempFile(tmpDir, "file", ".tmp")
val data = Random.nextBytes(blockSize.toInt())
assertThat(data.size.toLong()).isEqualTo(blockSize) // ensure data size is correct
val fileChannel = FileChannel.open(tmpFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE)
fileChannel.use {
it.transferFrom(Channels.newChannel(ByteArrayInputStream(data)), 0, Long.MAX_VALUE)
}
assertThat(tmpDir.fileStore().usableSpace).isEqualTo(blockSize)
}
This test fails on the assertion with the following values:
expected:<[4096]L> but was:<[0]L>
Expected :[4096]L
Actual :[0]L
But the test passes if I pass 4096 instead of Long.MAX_VALUE as the value of limit parameter of the transferFrom method.
It seems, transferFrom doesn't respect the actual size of the source channel and allocates extra block when copying.
The text was updated successfully, but these errors were encountered:
1. `transferFrom` was not behaving according to the spec when given a position that was greater than the size of that file. The spec says that nothing is transferred in that case, but Jimfs was using the same behavior as `write` in this case: the file was first zero-filled from the start until the given position and then bytes were transferred starting there. Jimfs now simply returns 0 in this case.
2. When given a non-blocking channel that can return 0 bytes from a call to `read`, `transferFrom` was spinning until the input channel returned -1. The spec is for `transferFrom` to return immediately in that case.
3. When a transfer ended on a block boundary (i.e. after exactly filling one block) but still had bytes remaining based on the given `count`, an extra (empty) block would be left allocated on the end of the file. That block is now correctly de-allocated so it can be used elsewhere. Fixes#163.
Also simplify the implementation of `transferFrom` a bit.
RELNOTES=Fixed several bugs with the behavior of `FileChannel.transferFrom`.
PiperOrigin-RevId: 390185370
I have a simple test that creates a file and fills it by the transferFrom method.
This test fails on the assertion with the following values:
But the test passes if I pass 4096 instead of Long.MAX_VALUE as the value of
limit
parameter of the transferFrom method.It seems, transferFrom doesn't respect the actual size of the source channel and allocates extra block when copying.
The text was updated successfully, but these errors were encountered: