Search in sources :

Example 16 with AsyncFile

use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.

the class FileSystemTest method testAsyncFileCloseHandlerIsAsync.

@Test
public void testAsyncFileCloseHandlerIsAsync() throws Exception {
    String fileName = "some-file.dat";
    createFileWithJunk(fileName, 100);
    AsyncFile file = vertx.fileSystem().openBlocking(testDir + pathSep + fileName, new OpenOptions());
    ThreadLocal stack = new ThreadLocal();
    stack.set(true);
    file.close(ar -> {
        assertNull(stack.get());
        assertTrue(Vertx.currentContext().isEventLoopContext());
        testComplete();
    });
    await();
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) Test(org.junit.Test)

Example 17 with AsyncFile

use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.

the class FileSystemTest method testReadStreamSetReadPos.

@Test
public void testReadStreamSetReadPos() throws Exception {
    String fileName = "some-file.dat";
    int chunkSize = 1000;
    int chunks = 10;
    byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
    createFile(fileName, content);
    vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
        if (ar.succeeded()) {
            AsyncFile rs = ar.result();
            rs.setReadPos(chunkSize * chunks / 2);
            Buffer buff = Buffer.buffer();
            rs.handler(buff::appendBuffer);
            rs.exceptionHandler(t -> fail(t.getMessage()));
            rs.endHandler(v -> {
                ar.result().close(ar2 -> {
                    if (ar2.failed()) {
                        fail(ar2.cause().getMessage());
                    } else {
                        assertEquals(chunkSize * chunks / 2, buff.length());
                        byte[] lastHalf = new byte[chunkSize * chunks / 2];
                        System.arraycopy(content, chunkSize * chunks / 2, lastHalf, 0, chunkSize * chunks / 2);
                        assertEquals(Buffer.buffer(lastHalf), buff);
                        testComplete();
                    }
                });
            });
        } else {
            fail(ar.cause().getMessage());
        }
    });
    await();
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.core.file.AsyncFile) Test(org.junit.Test)

Example 18 with AsyncFile

use of io.vertx.core.file.AsyncFile in project vertx-web by vert-x3.

the class WebClientTest method testRequestWithBody.

private void testRequestWithBody(HttpMethod method, boolean chunked) throws Exception {
    String expected = TestUtils.randomAlphaString(1024 * 1024);
    File f = File.createTempFile("vertx", ".data");
    f.deleteOnExit();
    Files.write(f.toPath(), expected.getBytes());
    waitFor(2);
    server.requestHandler(req -> req.bodyHandler(buff -> {
        assertEquals(method, req.method());
        assertEquals(Buffer.buffer(expected), buff);
        complete();
        req.response().end();
    }));
    startServer();
    vertx.runOnContext(v -> {
        AsyncFile asyncFile = vertx.fileSystem().openBlocking(f.getAbsolutePath(), new OpenOptions());
        HttpRequest<Buffer> builder = null;
        switch(method) {
            case POST:
                builder = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
                break;
            case PUT:
                builder = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
                break;
            case PATCH:
                builder = client.patch(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
                break;
            default:
                fail("Invalid HTTP method");
        }
        if (!chunked) {
            builder = builder.putHeader("Content-Length", "" + expected.length());
        }
        builder.sendStream(asyncFile, onSuccess(resp -> {
            assertEquals(200, resp.statusCode());
            complete();
        }));
    });
    await();
}
Also used : VertxException(io.vertx.core.VertxException) AsyncFile(io.vertx.core.file.AsyncFile) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Arrays(java.util.Arrays) DecodeException(io.vertx.core.json.DecodeException) HttpServer(io.vertx.core.http.HttpServer) MultiMap(io.vertx.core.MultiMap) BodyCodec(io.vertx.ext.web.codec.BodyCodec) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Cert(io.vertx.test.core.tls.Cert) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) HttpTestBase(io.vertx.test.core.HttpTestBase) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) ConnectException(java.net.ConnectException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) ProxyOptions(io.vertx.core.net.ProxyOptions) OpenOptions(io.vertx.core.file.OpenOptions) Files(java.nio.file.Files) VertxOptions(io.vertx.core.VertxOptions) HttpHeaders(io.vertx.core.http.HttpHeaders) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) ProxyType(io.vertx.core.net.ProxyType) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) WineAndCheese(io.vertx.ext.web.client.jackson.WineAndCheese) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) Collections(java.util.Collections) OpenOptions(io.vertx.core.file.OpenOptions) Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.core.file.AsyncFile) AsyncFile(io.vertx.core.file.AsyncFile) File(java.io.File)

Example 19 with AsyncFile

use of io.vertx.core.file.AsyncFile in project georocket by georocket.

the class FileStore method getOne.

@Override
public void getOne(String path, Handler<AsyncResult<ChunkReadStream>> handler) {
    String absolutePath = Paths.get(root, path).toString();
    // check if chunk exists
    FileSystem fs = vertx.fileSystem();
    ObservableFuture<Boolean> observable = RxHelper.observableFuture();
    fs.exists(absolutePath, observable.toHandler());
    observable.flatMap(exists -> {
        if (!exists) {
            return Observable.error(new FileNotFoundException("Could not find chunk: " + path));
        }
        return Observable.just(exists);
    }).flatMap(exists -> {
        // get chunk's size
        ObservableFuture<FileProps> propsObservable = RxHelper.observableFuture();
        fs.props(absolutePath, propsObservable.toHandler());
        return propsObservable;
    }).map(props -> props.size()).flatMap(size -> {
        // open chunk
        ObservableFuture<AsyncFile> openObservable = RxHelper.observableFuture();
        OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
        fs.open(absolutePath, openOptions, openObservable.toHandler());
        return openObservable.map(f -> new FileChunkReadStream(size, f));
    }).subscribe(readStream -> {
        // send chunk to peer
        handler.handle(Future.succeededFuture(readStream));
    }, err -> {
        handler.handle(Future.failedFuture(err));
    });
}
Also used : PathUtils(io.georocket.util.PathUtils) AsyncFile(io.vertx.core.file.AsyncFile) OpenOptions(io.vertx.core.file.OpenOptions) ObservableFuture(io.vertx.rx.java.ObservableFuture) IndexedStore(io.georocket.storage.indexed.IndexedStore) Vertx(io.vertx.core.Vertx) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) FileProps(io.vertx.core.file.FileProps) Observable(rx.Observable) Buffer(io.vertx.core.buffer.Buffer) Paths(java.nio.file.Paths) FileSystem(io.vertx.core.file.FileSystem) RxHelper(io.vertx.rx.java.RxHelper) Preconditions(com.google.common.base.Preconditions) Queue(java.util.Queue) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ChunkReadStream(io.georocket.storage.ChunkReadStream) ConfigConstants(io.georocket.constants.ConfigConstants) OpenOptions(io.vertx.core.file.OpenOptions) ObservableFuture(io.vertx.rx.java.ObservableFuture) FileSystem(io.vertx.core.file.FileSystem) FileNotFoundException(java.io.FileNotFoundException)

Example 20 with AsyncFile

use of io.vertx.core.file.AsyncFile in project georocket by georocket.

the class ImportCommand method importFile.

/**
 * Upload a file to GeoRocket
 * @param path path to file to import
 * @param client the GeoRocket client
 * @param vertx the Vert.x instance
 * @return an observable that will emit when the file has been uploaded
 */
protected Observable<Void> importFile(String path, GeoRocketClient client, Vertx vertx) {
    // open file
    FileSystem fs = vertx.fileSystem();
    OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
    return fs.rxOpen(path, openOptions).flatMap(f -> fs.rxProps(path).map(props -> Pair.of(f, props.size()))).flatMapObservable(f -> {
        ObservableFuture<Void> o = RxHelper.observableFuture();
        Handler<AsyncResult<Void>> handler = o.toHandler();
        AsyncFile file = f.getLeft().getDelegate();
        WriteStream<Buffer> out = client.getStore().startImport(layer, tags, properties, Optional.of(f.getRight()), fallbackCRS, handler);
        AtomicBoolean fileClosed = new AtomicBoolean();
        Pump pump = Pump.pump(file, out);
        file.endHandler(v -> {
            file.close();
            out.end();
            fileClosed.set(true);
        });
        Handler<Throwable> exceptionHandler = t -> {
            if (!fileClosed.get()) {
                file.endHandler(null);
                file.close();
            }
            handler.handle(Future.failedFuture(t));
        };
        file.exceptionHandler(exceptionHandler);
        out.exceptionHandler(exceptionHandler);
        pump.start();
        return o;
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) Arrays(java.util.Arrays) OptionParserException(de.undercouch.underline.OptionParserException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) Observable(rx.Observable) Pair(org.apache.commons.lang3.tuple.Pair) FileSet(org.apache.tools.ant.types.FileSet) WriteStream(io.vertx.core.streams.WriteStream) FileSystem(io.vertx.rxjava.core.file.FileSystem) Project(org.apache.tools.ant.Project) UnknownAttributes(de.undercouch.underline.UnknownAttributes) Pump(io.vertx.core.streams.Pump) AsyncResult(io.vertx.core.AsyncResult) Splitter(com.google.common.base.Splitter) OptionDesc(de.undercouch.underline.OptionDesc) PrintWriter(java.io.PrintWriter) OpenOptions(io.vertx.core.file.OpenOptions) ObservableFuture(io.vertx.rx.java.ObservableFuture) SystemUtils(org.apache.commons.lang3.SystemUtils) InputReader(de.undercouch.underline.InputReader) DurationFormat(io.georocket.util.DurationFormat) IOException(java.io.IOException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) GeoRocketClient(io.georocket.client.GeoRocketClient) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) Paths(java.nio.file.Paths) RxHelper(io.vertx.rx.java.RxHelper) Optional(java.util.Optional) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) ArgumentType(de.undercouch.underline.Option.ArgumentType) Handler(io.vertx.core.Handler) FilenameUtils(org.apache.commons.io.FilenameUtils) Vertx(io.vertx.rxjava.core.Vertx) Buffer(io.vertx.core.buffer.Buffer) Pump(io.vertx.core.streams.Pump) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileSystem(io.vertx.rxjava.core.file.FileSystem) AsyncFile(io.vertx.core.file.AsyncFile) AsyncResult(io.vertx.core.AsyncResult)

Aggregations

AsyncFile (io.vertx.core.file.AsyncFile)27 OpenOptions (io.vertx.core.file.OpenOptions)23 Buffer (io.vertx.core.buffer.Buffer)16 Handler (io.vertx.core.Handler)9 Vertx (io.vertx.core.Vertx)8 AsyncResult (io.vertx.core.AsyncResult)7 FileSystem (io.vertx.core.file.FileSystem)7 Test (org.junit.Test)7 Future (io.vertx.core.Future)6 File (java.io.File)6 List (java.util.List)6 WriteStream (io.vertx.core.streams.WriteStream)5 IOException (java.io.IOException)5 JsonObject (io.vertx.core.json.JsonObject)4 ReadStream (io.vertx.core.streams.ReadStream)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Pump (io.vertx.core.streams.Pump)3 Arrays (java.util.Arrays)3 Splitter (com.google.common.base.Splitter)2 ByteBuf (io.netty.buffer.ByteBuf)2