public static MappedByteBuffer mapNewFile(final File location, final long length, final boolean fillWithZeros)
    {
        MappedByteBuffer mappedByteBuffer = null;
        try (FileChannel channel = FileChannel.open(location.toPath(), CREATE_NEW, READ, WRITE))
        {
            mappedByteBuffer = channel.map(READ_WRITE, 0, length);
            if (fillWithZeros)
            {
                int pos = 0;
                while (pos < length)
                {
                    mappedByteBuffer.put(pos, (byte)0);
                    pos += BLOCK_SIZE;
                }
            }
        }
        catch (final IOException ex)
        {
            LangUtil.rethrowUnchecked(ex);
        }

        return mappedByteBuffer;
    }
public static MappedByteBuffer mapNewFile ( final File location , final long length , final boolean fillWithZeros ) { MappedByteBuffer mappedByteBuffer = null ; try ( FileChannel channel = FileChannel . open ( location . toPath ( ) , CREATE_NEW , READ , WRITE ) ) { mappedByteBuffer = channel . map ( READ_WRITE , 0 , length ) ; if ( fillWithZeros ) { int pos = 0 ; while ( pos < length ) { mappedByteBuffer . put ( pos , ( byte ) 0 ) ; pos += BLOCK_SIZE ; } } } catch ( final IOException ex ) { LangUtil . rethrowUnchecked ( ex ) ; } return mappedByteBuffer ; }
Create a new file, and optionally fill with 0s, and return a {@link java.nio.MappedByteBuffer} for the file.
<p>
The file itself will be closed, but the mapping will persist.

@param location      of the file to create and map
@param length        of the file to create and map
@param fillWithZeros to force allocation.
@return {@link java.nio.MappedByteBuffer} for the file