caigy commented on issue #8017:
URL: https://github.com/apache/rocketmq/issues/8017#issuecomment-2056368726
> > `ByteStreams.toByteArray()` creates large array in memory and is
unnecessary. It seems `FileChannel.transferFrom()` with NIO channel would be
more efficient for writing streams to a file.
>
> Thx for reply, I tried `FileChannel.transferFrom()` but find out some
unittest failed:
>
> ```
> @Test
> public void consumeQueueTest() throws ClassNotFoundException,
NoSuchMethodException {
> ...
> ByteBuffer cqItem1 = fileSegment.read(initPosition, unitSize);
> Assert.assertEquals(baseOffset, cqItem1.getLong()); // failed
> ...
> }
> ```
>
> > java.lang.AssertionError:
> > Expected :1000
> > Actual :0
>
> It turns out the cq data was not flush correctly by debugging, I wonder if
the way I use `FileChannel.transferFrom()` is wrong: Before:
>
> ```
> byte[] byteArray = ByteStreams.toByteArray(inputStream);
> writeFileChannel.position(position);
> ByteBuffer buffer = ByteBuffer.wrap(byteArray);
> while (buffer.hasRemaining()) {
> writeFileChannel.write(buffer);
> }
> writeFileChannel.force(true);
> ```
>
> After:
>
> ```
> ReadableByteChannel readableByteChannel =
Channels.newChannel(inputStream);
> writeFileChannel.transferFrom(readableByteChannel,
position, length);
> writeFileChannel.force(true);
> ```
It seems the modification is not equivalent.
`writeFileChannel.position(position)` has side effect, making
`writeFileChannel` written from `position`. But
`writeFileChannel.transferFrom()` exits when `position` is larger than the size
of `writeFileChannel`.
You might extend the file of `writeFileChannel` to `position` and also check
if the transferred bytes is equal to `length`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]