Example usage for java.nio.file StandardOpenOption SPARSE

List of usage examples for java.nio.file StandardOpenOption SPARSE

Introduction

In this page you can find the example usage for java.nio.file StandardOpenOption SPARSE.

Prototype

StandardOpenOption SPARSE

To view the source code for java.nio.file StandardOpenOption SPARSE.

Click Source Link

Document

Sparse file.

Usage

From source file:com.netflix.genie.web.services.impl.DiskJobFileServiceImpl.java

/**
 * {@inheritDoc}/*from w  ww.  ja v  a2  s  .  co m*/
 */
@Override
// TODO: We should be careful about how large the byte[] is. Perhaps we should have precondition to protect memory
//       or we should wrap calls to this in something that chunks it off an input stream or just take this in as
//       input stream
public void updateFile(final String jobId, final String relativePath, final long startByte, final byte[] data)
        throws IOException {
    log.debug("Attempting to write {} bytes from position {} into log file {} for job {}", data.length,
            startByte, relativePath, jobId);
    final Path jobFile = this.jobsDirRoot.resolve(jobId).resolve(relativePath);

    if (Files.notExists(jobFile)) {
        // Make sure all the directories exist on disk
        final Path logFileParent = jobFile.getParent();
        if (logFileParent != null) {
            this.createOrCheckDirectory(logFileParent);
        }
    } else if (Files.isDirectory(jobFile)) {
        // TODO: Perhaps this should be different exception
        throw new IllegalArgumentException(relativePath + " is a directory not a file. Unable to update");
    }

    try (FileChannel fileChannel = FileChannel.open(jobFile,
            EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SPARSE))) {
        // Move the byteChannel to the start byte
        fileChannel.position(startByte);

        // The size and length are ignored in this implementation as we just assume we're writing everything atm
        // TODO: Would it be better to provide an input stream and buffer the output?
        final ByteBuffer byteBuffer = ByteBuffer.wrap(data);

        while (byteBuffer.hasRemaining()) {
            fileChannel.write(byteBuffer);
        }
    }
}

From source file:org.neo4j.io.pagecache.PageCacheTest.java

@Test(timeout = SEMI_LONG_TIMEOUT_MILLIS)
public void mustIgnoreCertainOpenOptions() throws Exception {
    getPageCache(fs, maxPages, pageCachePageSize, PageCacheTracer.NULL);
    try (PagedFile pf = pageCache.map(file("a"), filePageSize, StandardOpenOption.READ,
            StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.SPARSE);
            PageCursor cursor = pf.io(0, PF_SHARED_WRITE_LOCK)) {
        assertTrue(cursor.next());//from w  ww. ja  v a 2s .  co m
    }
}