Search in sources :

Example 1 with AsynchronousFileChannel

use of java.nio.channels.AsynchronousFileChannel in project quasar by puniverse.

the class FiberFileChannel method open.

/**
     * Opens or creates a file for reading and/or writing, returning a file channel to access the file.
     *
     * <p>
     * The {@code options} parameter determines how the file is opened.
     * The {@link StandardOpenOption#READ READ} and {@link StandardOpenOption#WRITE
     * WRITE} options determines if the file should be opened for reading and/or
     * writing. If neither option is contained in the array then an existing file
     * is opened for reading.
     *
     * <p>
     * In addition to {@code READ} and {@code WRITE}, the following options
     * may be present:
     *
     * <table border=1 cellpadding=5 summary="">
     * <tr> <th>Option</th> <th>Description</th> </tr>
     * <tr>
     * <td> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </td>
     * <td> When opening an existing file, the file is first truncated to a
     * size of 0 bytes. This option is ignored when the file is opened only
     * for reading.</td>
     * </tr>
     * <tr>
     * <td> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </td>
     * <td> If this option is present then a new file is created, failing if
     * the file already exists. When creating a file the check for the
     * existence of the file and the creation of the file if it does not exist
     * is atomic with respect to other file system operations. This option is
     * ignored when the file is opened only for reading. </td>
     * </tr>
     * <tr>
     * <td > {@link StandardOpenOption#CREATE CREATE} </td>
     * <td> If this option is present then an existing file is opened if it
     * exists, otherwise a new file is created. When creating a file the check
     * for the existence of the file and the creation of the file if it does
     * not exist is atomic with respect to other file system operations. This
     * option is ignored if the {@code CREATE_NEW} option is also present or
     * the file is opened only for reading. </td>
     * </tr>
     * <tr>
     * <td > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </td>
     * <td> When this option is present then the implementation makes a
     * <em>best effort</em> attempt to delete the file when closed by the
     * the {@link #close close} method. If the {@code close} method is not
     * invoked then a <em>best effort</em> attempt is made to delete the file
     * when the Java virtual machine terminates. </td>
     * </tr>
     * <tr>
     * <td>{@link StandardOpenOption#SPARSE SPARSE} </td>
     * <td> When creating a new file this option is a <em>hint</em> that the
     * new file will be sparse. This option is ignored when not creating
     * a new file. </td>
     * </tr>
     * <tr>
     * <td> {@link StandardOpenOption#SYNC SYNC} </td>
     * <td> Requires that every update to the file's content or metadata be
     * written synchronously to the underlying storage device. (see <a
     * href="../file/package-summary.html#integrity"> Synchronized I/O file
     * integrity</a>). </td>
     * <tr>
     * <tr>
     * <td> {@link StandardOpenOption#DSYNC DSYNC} </td>
     * <td> Requires that every update to the file's content be written
     * synchronously to the underlying storage device. (see <a
     * href="../file/package-summary.html#integrity"> Synchronized I/O file
     * integrity</a>). </td>
     * </tr>
     * </table>
     *
     * <p>
     * An implementation may also support additional options.
     *
     * <p>
     * The {@code executor} parameter is the {@link ExecutorService} to
     * which tasks are submitted to handle I/O events and dispatch completion
     * results for operations initiated on resulting channel.
     * The nature of these tasks is highly implementation specific and so care
     * should be taken when configuring the {@code Executor}. Minimally it
     * should support an unbounded work queue and should not run tasks on the
     * caller thread of the {@link ExecutorService#execute execute} method.
     * Shutting down the executor service while the channel is open results in
     * unspecified behavior.
     *
     * <p>
     * The {@code attrs} parameter is an optional array of file {@link
     * FileAttribute file-attributes} to set atomically when creating the file.
     *
     * <p>
     * The new channel is created by invoking the {@link
     * FileSystemProvider#newFileChannel newFileChannel} method on the
     * provider that created the {@code Path}.
     *
     * @param path       The path of the file to open or create
     * @param options    Options specifying how the file is opened
     * @param ioExecutor The thread pool or {@code null} to associate the channel with the default thread pool
     * @param attrs      An optional list of file attributes to set atomically when creating the file
     *
     * @return A new file channel
     *
     * @throws IllegalArgumentException      If the set contains an invalid combination of options
     * @throws UnsupportedOperationException If the {@code file} is associated with a provider that does not
     *                                       support creating asynchronous file channels, or an unsupported
     *                                       open option is specified, or the array contains an attribute that
     *                                       cannot be set atomically when creating the file
     * @throws IOException                   If an I/O error occurs
     * @throws SecurityException             If a security manager is installed and it denies an
     *                                       unspecified permission required by the implementation.
     *                                       In the case of the default provider, the {@link SecurityManager#checkRead(String)}
     *                                       method is invoked to check read access if the file is opened for reading.
     *                                       The {@link SecurityManager#checkWrite(String)} method is invoked to check
     *                                       write access if the file is opened for writing
     */
@Suspendable
public static FiberFileChannel open(final ExecutorService ioExecutor, final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
    // FiberAsyncIO.ioExecutor(); // 
    final ExecutorService ioExec = ioExecutor != null ? ioExecutor : fiberFileThreadPool;
    AsynchronousFileChannel afc = FiberAsyncIO.runBlockingIO(fiberFileThreadPool, new CheckedCallable<AsynchronousFileChannel, IOException>() {

        @Override
        public AsynchronousFileChannel call() throws IOException {
            return AsynchronousFileChannel.open(path, options, ioExec, attrs);
        }
    });
    return new FiberFileChannel(afc);
}
Also used : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) Suspendable(co.paralleluniverse.fibers.Suspendable)

Example 2 with AsynchronousFileChannel

use of java.nio.channels.AsynchronousFileChannel in project spring-framework by spring-projects.

the class ResourceRegionEncoder method readResourceRegion.

private Flux<DataBuffer> readResourceRegion(ResourceRegion region, DataBufferFactory bufferFactory) {
    Resource resource = region.getResource();
    try {
        if (resource.isFile()) {
            File file = region.getResource().getFile();
            AsynchronousFileChannel channel = AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ);
            return DataBufferUtils.read(channel, region.getPosition(), bufferFactory, this.bufferSize);
        }
    } catch (IOException ignore) {
    // fallback to resource.readableChannel(), below
    }
    try {
        ReadableByteChannel channel = resource.readableChannel();
        Flux<DataBuffer> in = DataBufferUtils.read(channel, bufferFactory, this.bufferSize);
        return DataBufferUtils.skipUntilByteCount(in, region.getPosition());
    } catch (IOException ex) {
        return Flux.error(ex);
    }
}
Also used : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) ReadableByteChannel(java.nio.channels.ReadableByteChannel) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) File(java.io.File) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 3 with AsynchronousFileChannel

use of java.nio.channels.AsynchronousFileChannel in project spring-framework by spring-projects.

the class DataBufferUtilsTests method readAsynchronousFileChannelPosition.

@Test
public void readAsynchronousFileChannelPosition() throws Exception {
    URI uri = DataBufferUtilsTests.class.getResource("DataBufferUtilsTests.txt").toURI();
    AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ);
    Flux<DataBuffer> flux = DataBufferUtils.read(channel, 3, this.bufferFactory, 3);
    StepVerifier.create(flux).consumeNextWith(stringConsumer("bar")).consumeNextWith(stringConsumer("baz")).consumeNextWith(stringConsumer("qux")).expectComplete().verify();
}
Also used : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) URI(java.net.URI) Test(org.junit.Test)

Example 4 with AsynchronousFileChannel

use of java.nio.channels.AsynchronousFileChannel in project lucene-solr by apache.

the class TestVerboseFS method testAsyncFileChannel.

/** Test AsynchronousFileChannel.open */
public void testAsyncFileChannel() throws IOException {
    InfoStreamListener stream = new InfoStreamListener("newAsynchronousFileChannel");
    Path dir = wrap(createTempDir(), stream);
    AsynchronousFileChannel channel = AsynchronousFileChannel.open(dir.resolve("foobar"), StandardOpenOption.CREATE_NEW, StandardOpenOption.READ, StandardOpenOption.WRITE);
    assertTrue(stream.sawMessage());
    channel.close();
    try {
        AsynchronousFileChannel.open(dir.resolve("foobar"), StandardOpenOption.CREATE_NEW, StandardOpenOption.READ, StandardOpenOption.WRITE);
        fail("didn't get expected exception");
    } catch (IOException expected) {
    }
}
Also used : Path(java.nio.file.Path) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) IOException(java.io.IOException)

Example 5 with AsynchronousFileChannel

use of java.nio.channels.AsynchronousFileChannel in project webpieces by deanhiller.

the class StaticFileReader method runAsyncFileRead.

private CompletableFuture<Void> runAsyncFileRead(RequestInfo info, RenderStaticResponse renderStatic) throws IOException {
    boolean isFile = true;
    String fullFilePath = renderStatic.getFilePath();
    if (fullFilePath == null) {
        isFile = false;
        fullFilePath = renderStatic.getDirectory() + renderStatic.getRelativePath();
    }
    String extension = null;
    int lastDirIndex = fullFilePath.lastIndexOf("/");
    int lastDot = fullFilePath.lastIndexOf(".");
    if (lastDot > lastDirIndex) {
        extension = fullFilePath.substring(lastDot + 1);
    }
    ResponseEncodingTuple tuple = responseCreator.createResponse(info.getRequest(), StatusCode.HTTP_200_OK, extension, "application/octet-stream", false);
    Http2Response response = tuple.response;
    //On startup, we protect developers from breaking clients.  In http, all files that change
    //must also change the hash automatically and the %%{ }%% tag generates those hashes so the
    //files loaded are always the latest
    Long timeMs = config.getStaticFileCacheTimeSeconds();
    if (timeMs != null)
        response.addHeader(new Http2Header(Http2HeaderName.CACHE_CONTROL, "max-age=" + timeMs));
    Path file;
    Compression compr = compressionLookup.createCompressionStream(info.getRouterRequest().encodings, extension, tuple.mimeType);
    //during startup as I don't feel like paying a cpu penalty for compressing while live
    if (compr != null && compr.getCompressionType().equals(routerConfig.getStartupCompression())) {
        response.addHeader(new Http2Header(Http2HeaderName.CONTENT_ENCODING, compr.getCompressionType()));
        File routesCache = renderStatic.getTargetCache();
        File fileReference;
        if (isFile) {
            String fileName = fullFilePath.substring(lastDirIndex + 1);
            fileReference = new File(routesCache, fileName);
        } else {
            fileReference = new File(routesCache, renderStatic.getRelativePath());
        }
        fullFilePath = fileReference.getAbsolutePath();
        file = fetchFile("Compressed File from cache=", fullFilePath + ".gz");
    } else {
        file = fetchFile("File=", fullFilePath);
    }
    AsynchronousFileChannel asyncFile = AsynchronousFileChannel.open(file, options, fileExecutor);
    CompletableFuture<StreamWriter> future;
    try {
        log.info(() -> "sending chunked file via async read=" + file);
        long length = file.toFile().length();
        AtomicLong remaining = new AtomicLong(length);
        future = info.getResponseSender().sendResponse(response).thenCompose(s -> readLoop(s, info.getPool(), file, asyncFile, 0, remaining));
    } catch (Throwable e) {
        future = new CompletableFuture<StreamWriter>();
        future.completeExceptionally(e);
    }
    return //our finally block for failures
    future.handle((s, exc) -> handleClose(info, s, exc)).thenAccept(s -> empty());
}
Also used : Path(java.nio.file.Path) BufferPool(org.webpieces.data.api.BufferPool) RouterConfig(org.webpieces.router.api.RouterConfig) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) CompletableFuture(java.util.concurrent.CompletableFuture) ResponseEncodingTuple(org.webpieces.webserver.impl.ResponseCreator.ResponseEncodingTuple) Singleton(javax.inject.Singleton) ByteBuffer(java.nio.ByteBuffer) HashSet(java.util.HashSet) Inject(javax.inject.Inject) DataWrapper(org.webpieces.data.api.DataWrapper) Named(javax.inject.Named) CompressionLookup(org.webpieces.router.impl.compression.CompressionLookup) Path(java.nio.file.Path) DataWrapperGeneratorFactory(org.webpieces.data.api.DataWrapperGeneratorFactory) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.webpieces.util.logging.Logger) RenderStaticResponse(org.webpieces.router.api.dto.RenderStaticResponse) StatusCode(com.webpieces.http2parser.api.dto.StatusCode) OpenOption(java.nio.file.OpenOption) CompletionHandler(java.nio.channels.CompletionHandler) StandardOpenOption(java.nio.file.StandardOpenOption) Set(java.util.Set) HttpFrontendFactory(org.webpieces.frontend2.api.HttpFrontendFactory) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) IOException(java.io.IOException) File(java.io.File) Http2HeaderName(com.webpieces.http2parser.api.dto.lib.Http2HeaderName) AtomicLong(java.util.concurrent.atomic.AtomicLong) BufferCreationPool(org.webpieces.data.api.BufferCreationPool) WebServerConfig(org.webpieces.webserver.api.WebServerConfig) StreamWriter(com.webpieces.http2engine.api.StreamWriter) Paths(java.nio.file.Paths) NotFoundException(org.webpieces.router.api.exceptions.NotFoundException) DataWrapperGenerator(org.webpieces.data.api.DataWrapperGenerator) LoggerFactory(org.webpieces.util.logging.LoggerFactory) Http2Response(com.webpieces.hpack.api.dto.Http2Response) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) Compression(org.webpieces.router.impl.compression.Compression) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Compression(org.webpieces.router.impl.compression.Compression) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) StreamWriter(com.webpieces.http2engine.api.StreamWriter) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) AtomicLong(java.util.concurrent.atomic.AtomicLong) CompletableFuture(java.util.concurrent.CompletableFuture) ResponseEncodingTuple(org.webpieces.webserver.impl.ResponseCreator.ResponseEncodingTuple) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File)

Aggregations

AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)11 IOException (java.io.IOException)7 Path (java.nio.file.Path)4 File (java.io.File)3 Test (org.junit.Test)3 URI (java.net.URI)2 ByteBuffer (java.nio.ByteBuffer)2 ReadableByteChannel (java.nio.channels.ReadableByteChannel)2 ExecutorService (java.util.concurrent.ExecutorService)2 Suspendable (co.paralleluniverse.fibers.Suspendable)1 Http2Response (com.webpieces.hpack.api.dto.Http2Response)1 StreamWriter (com.webpieces.http2engine.api.StreamWriter)1 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)1 StatusCode (com.webpieces.http2parser.api.dto.StatusCode)1 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)1 Http2HeaderName (com.webpieces.http2parser.api.dto.lib.Http2HeaderName)1 OutputStream (java.io.OutputStream)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 CompletionHandler (java.nio.channels.CompletionHandler)1 FileChannel (java.nio.channels.FileChannel)1