/** * Create an empty file, fill with 0s, and return the {@link FileChannel} * * @param file to create * @param length of the file to create * @return {@link java.nio.channels.FileChannel} for the file */ public static FileChannel createEmptyFile(final File file, final long length) { return createEmptyFile(file, length, true); }
/** * Create an empty file, fill with 0s, and return the {@link FileChannel} * * @param file to create * @param length of the file to create * @return {@link java.nio.channels.FileChannel} for the file */ public static FileChannel createEmptyFile(final File file, final long length) { return createEmptyFile(file, length, true); }
/** * Create a new file, 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 size of the file to create and map * @return {@link java.nio.MappedByteBuffer} for the file */ public static MappedByteBuffer mapNewFile(final File location, final long size) { MappedByteBuffer mappedByteBuffer = null; try (FileChannel channel = createEmptyFile(location, size)) { mappedByteBuffer = channel.map(READ_WRITE, 0, size); } catch (final IOException ex) { LangUtil.rethrowUnchecked(ex); } return mappedByteBuffer; }
public static MappedFile map(final File bufferFile, final int size) { final FileChannel fileChannel; try { if (bufferFile.exists()) { // NB: closing RAF or FileChannel closes them both fileChannel = new RandomAccessFile(bufferFile, "rw").getChannel(); } else { fileChannel = IoUtil.createEmptyFile(bufferFile, (long)size); } final MappedByteBuffer mappedBuffer = fileChannel.map(READ_WRITE, 0, fileChannel.size()); return new MappedFile(bufferFile, fileChannel, new UnsafeBuffer(mappedBuffer)); } catch (final IOException ex) { LangUtil.rethrowUnchecked(ex); return null; } }