Search in sources :

Example 1 with AsyncFile

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

the class FileSystemExamples method asyncFileWrite.

public void asyncFileWrite() {
    Vertx vertx = Vertx.vertx();
    vertx.fileSystem().open("target/classes/hello.txt", new OpenOptions(), result -> {
        if (result.succeeded()) {
            AsyncFile file = result.result();
            Buffer buff = Buffer.buffer("foo");
            for (int i = 0; i < 5; i++) {
                file.write(buff, buff.length() * i, ar -> {
                    if (ar.succeeded()) {
                        System.out.println("Written ok!");
                    } else {
                        System.err.println("Failed to write: " + ar.cause());
                    }
                });
            }
        } else {
            System.err.println("Cannot open file " + result.cause());
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.core.file.AsyncFile) Vertx(io.vertx.core.Vertx)

Example 2 with AsyncFile

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

the class FileSystemTest method testIllegalArguments.

@Test
public void testIllegalArguments() throws Exception {
    assertNullPointerException(() -> vertx.fileSystem().copy(null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().copy("ignored", null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().copyBlocking(null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().copyBlocking("ignored", null));
    assertNullPointerException(() -> vertx.fileSystem().copyRecursive(null, "ignored", true, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().copyRecursive("ignored", null, true, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().copyRecursiveBlocking(null, "ignored", true));
    assertNullPointerException(() -> vertx.fileSystem().copyRecursiveBlocking("ignored", null, true));
    assertNullPointerException(() -> vertx.fileSystem().move(null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().move("ignored", null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().moveBlocking(null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().moveBlocking("ignored", null));
    assertNullPointerException(() -> vertx.fileSystem().truncate(null, 0, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().truncateBlocking(null, 0));
    assertNullPointerException(() -> vertx.fileSystem().chmod(null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().chmod("ignored", null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().chmodBlocking(null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().chmodBlocking("ignored", null));
    assertNullPointerException(() -> vertx.fileSystem().chmodRecursive(null, "ignored", "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().chmodRecursive("ignored", null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().chmodRecursiveBlocking(null, "ignored", "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().chmodRecursiveBlocking("ignored", null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().chown(null, "ignored", "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().chownBlocking(null, "ignored", "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().props(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().propsBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().lprops(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().lpropsBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().link(null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().link("ignored", null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().linkBlocking(null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().linkBlocking("ignored", null));
    assertNullPointerException(() -> vertx.fileSystem().symlink(null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().symlink("ignored", null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().symlinkBlocking(null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().symlinkBlocking("ignored", null));
    assertNullPointerException(() -> vertx.fileSystem().unlink(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().unlinkBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().readSymlink(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().readSymlinkBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().delete(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().deleteBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().deleteRecursive(null, true, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().deleteRecursiveBlocking(null, true));
    assertNullPointerException(() -> vertx.fileSystem().mkdir(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().mkdirBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().mkdir(null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().mkdirBlocking(null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().mkdirs(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().mkdirsBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().mkdirs(null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().mkdirsBlocking(null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().readDir(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().readDirBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().readDir(null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().readDirBlocking(null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().readFile(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().readFileBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().writeFile(null, Buffer.buffer(), h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().writeFile("ignored", null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().writeFileBlocking(null, Buffer.buffer()));
    assertNullPointerException(() -> vertx.fileSystem().writeFileBlocking("ignored", null));
    assertNullPointerException(() -> vertx.fileSystem().open(null, new OpenOptions(), h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().open("ignored", null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().openBlocking(null, new OpenOptions()));
    assertNullPointerException(() -> vertx.fileSystem().openBlocking("ignored", null));
    assertNullPointerException(() -> vertx.fileSystem().createFile(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().createFileBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().createFile(null, "ignored", h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().createFileBlocking(null, "ignored"));
    assertNullPointerException(() -> vertx.fileSystem().exists(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().existsBlocking(null));
    assertNullPointerException(() -> vertx.fileSystem().fsProps(null, h -> {
    }));
    assertNullPointerException(() -> vertx.fileSystem().fsPropsBlocking(null));
    String fileName = "some-file.dat";
    AsyncFile asyncFile = vertx.fileSystem().openBlocking(testDir + pathSep + fileName, new OpenOptions());
    assertNullPointerException(() -> asyncFile.write(null));
    assertIllegalArgumentException(() -> asyncFile.setWriteQueueMaxSize(1));
    assertIllegalArgumentException(() -> asyncFile.setWriteQueueMaxSize(0));
    assertIllegalArgumentException(() -> asyncFile.setWriteQueueMaxSize(-1));
    assertNullPointerException(() -> asyncFile.write(null, 0, h -> {
    }));
    assertNullPointerException(() -> asyncFile.write(Buffer.buffer(), 0, null));
    assertIllegalArgumentException(() -> asyncFile.write(Buffer.buffer(), -1, h -> {
    }));
    assertNullPointerException(() -> asyncFile.read(null, 0, 0, 0, h -> {
    }));
    assertNullPointerException(() -> asyncFile.read(Buffer.buffer(), 0, 0, 0, null));
    assertIllegalArgumentException(() -> asyncFile.read(Buffer.buffer(), -1, 0, 0, h -> {
    }));
    assertIllegalArgumentException(() -> asyncFile.read(Buffer.buffer(), 0, -1, 0, h -> {
    }));
    assertIllegalArgumentException(() -> asyncFile.read(Buffer.buffer(), 0, 0, -1, h -> {
    }));
}
Also used : AsyncFile(io.vertx.core.file.AsyncFile) AsyncFileImpl(io.vertx.core.file.impl.AsyncFileImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileSystemException(io.vertx.core.file.FileSystemException) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) FileSystemProps(io.vertx.core.file.FileSystemProps) Utils(io.vertx.core.impl.Utils) Unpooled(io.netty.buffer.Unpooled) FileProps(io.vertx.core.file.FileProps) HashSet(java.util.HashSet) LinkOption(java.nio.file.LinkOption) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) UserPrincipal(java.nio.file.attribute.UserPrincipal) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestUtils(io.vertx.test.core.TestUtils) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) Pump(io.vertx.core.streams.Pump) JsonObject(io.vertx.core.json.JsonObject) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) Path(java.nio.file.Path) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) OpenOptions(io.vertx.core.file.OpenOptions) Files(java.nio.file.Files) Vertx(io.vertx.core.Vertx) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) File(java.io.File) List(java.util.List) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) Paths(java.nio.file.Paths) Handler(io.vertx.core.Handler) TemporaryFolder(org.junit.rules.TemporaryFolder) FileSystems(java.nio.file.FileSystems) OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) Test(org.junit.Test)

Example 3 with AsyncFile

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

the class FileSystemTest method testReadStreamWithBufferSize.

@Test
public void testReadStreamWithBufferSize() throws Exception {
    String fileName = "some-file.dat";
    int chunkSize = 16384;
    int chunks = 1;
    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.setReadBufferSize(chunkSize);
            Buffer buff = Buffer.buffer();
            int[] callCount = { 0 };
            rs.handler((buff1) -> {
                buff.appendBuffer(buff1);
                callCount[0]++;
            });
            rs.exceptionHandler(t -> fail(t.getMessage()));
            rs.endHandler(v -> {
                ar.result().close(ar2 -> {
                    if (ar2.failed()) {
                        fail(ar2.cause().getMessage());
                    } else {
                        assertEquals(1, callCount[0]);
                        assertEquals(Buffer.buffer(content), 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 4 with AsyncFile

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

the class JsonStreamingExample method start.

@Override
public void start() throws Exception {
    vertx.fileSystem().open("large.json", new OpenOptions(), ar -> {
        if (ar.succeeded()) {
            AsyncFile asyncFile = ar.result();
            AtomicInteger counter = new AtomicInteger();
            // Here a Json streaming parser is created wrapping an AsyncFile
            // JsonParser is a ReadStream of JsonEvent and can wrap any ReadStream of Buffer
            JsonParser jsonParser = JsonParser.newParser(asyncFile);
            // We want to parse a giant array of small obects so we switch the parser to object-value mode
            // The parser will then emit objects inside the array as a single value event
            // This is pretty convenient for the developer
            jsonParser.objectValueMode().exceptionHandler(t -> {
                t.printStackTrace();
                asyncFile.close();
            }).endHandler(v -> {
                System.out.println("Done!");
                asyncFile.close();
            }).handler(event -> {
                // In object-value mode the event should always be of type "VALUE"
                if (event.type() == VALUE) {
                    // Use mapTo to map the JSON obect to a Java class
                    DataPoint dataPoint = event.mapTo(DataPoint.class);
                    // Let's not log all objects from this giant file...
                    if (counter.incrementAndGet() % 100 == 0) {
                        System.out.println("DataPoint = " + dataPoint);
                    }
                }
            });
        } else {
            ar.cause().printStackTrace();
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) OpenOptions(io.vertx.core.file.OpenOptions) JsonEventType(io.vertx.core.parsetools.JsonEventType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractVerticle(io.vertx.core.AbstractVerticle) JsonParser(io.vertx.core.parsetools.JsonParser) Runner(io.vertx.example.util.Runner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncFile(io.vertx.core.file.AsyncFile) JsonParser(io.vertx.core.parsetools.JsonParser)

Example 5 with AsyncFile

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

the class Client method start.

@Override
public void start() throws Exception {
    HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions()).put(8080, "localhost", "/someurl", resp -> {
        System.out.println("Response " + resp.statusCode());
    });
    String filename = "upload.txt";
    FileSystem fs = vertx.fileSystem();
    fs.props(filename, ares -> {
        FileProps props = ares.result();
        System.out.println("props is " + props);
        long size = props.size();
        req.headers().set("content-length", "" + size);
        fs.open(filename, new OpenOptions(), ares2 -> {
            AsyncFile file = ares2.result();
            Pump pump = Pump.pump(file, req);
            file.endHandler(v -> {
                req.end();
            });
            pump.start();
        });
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) HttpClientRequest(io.vertx.core.http.HttpClientRequest) FileSystem(io.vertx.core.file.FileSystem) AsyncFile(io.vertx.core.file.AsyncFile) FileProps(io.vertx.core.file.FileProps) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Pump(io.vertx.core.streams.Pump)

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