Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.11
-
None
Description
Unfortunately, RandomAccessFile does not create sparse files on Windows with seek/position, it requires a specific flag set with NTFS. Luckily, NIO.2 supports this. File creation for 4 GiB will be tens of milliseconds. Use this snippet as soon as we move to Java 7:
Path tempDirectory = Files.createTempDirectory("jetty"); final ByteBuffer buf = ByteBuffer.allocate(4).putInt(2); buf.rewind(); final OpenOption[] options = { StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW , StandardOpenOption.SPARSE }; final Path hugeFile = tempDirectory.resolve("hugefile.txt"); try (final SeekableByteChannel channel = Files.newByteChannel(hugeFile, options);) { channel.position(HUGE_FILE_SIZE); channel.write(buf); }