You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/06/22 14:47:39 UTC

[GitHub] [kafka] divijvaidya commented on a diff in pull request #12331: KAFKA-1194: changes needed to run on Windows

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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org