divijvaidya commented on code in PR #12331:
URL: https://github.com/apache/kafka/pull/12331#discussion_r903841247
##########
clients/src/main/java/org/apache/kafka/common/record/FileRecords.java:
##########
@@ -461,14 +461,15 @@ private static FileChannel openChannel(File file,
int initFileSize,
boolean preallocate) throws
IOException {
if (mutable) {
- if (fileAlreadyExists || !preallocate) {
- return FileChannel.open(file.toPath(),
StandardOpenOption.CREATE, StandardOpenOption.READ,
- StandardOpenOption.WRITE);
- } else {
- RandomAccessFile randomAccessFile = new RandomAccessFile(file,
"rw");
- randomAccessFile.setLength(initFileSize);
- return randomAccessFile.getChannel();
+ if (preallocate && !fileAlreadyExists) {
+ try (RandomAccessFile randomAccessFile = new
RandomAccessFile(file, "rw")) {
Review Comment:
You don't necessarily need `RandomAccessFile.setLength()` here. Since NIO.2
in JDK 8, you can use `SeekableByteChannel` which should fix your problem with
Windows.
```
final OpenOption[] options = { StandardOpenOption.WRITE,
StandardOpenOption.CREATE_NEW , StandardOpenOption.SPARSE };
final Path hugeFile = Paths.get("hugefile.txt");
try (final SeekableByteChannel channel = Files.newByteChannel(hugeFile,
options);) {
channel.position(HUGE_FILE_SIZE);
channel.write(buf);
}
```
--
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]