Search in sources :

Example 1 with OpenOptions

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

the class HttpServerFileUploadImpl method streamToFileSystem.

@Override
public HttpServerFileUpload streamToFileSystem(String filename) {
    pause();
    vertx.fileSystem().open(filename, new OpenOptions(), ar -> {
        if (ar.succeeded()) {
            file = ar.result();
            Pump p = Pump.pump(HttpServerFileUploadImpl.this, ar.result());
            p.start();
            resume();
        } else {
            notifyExceptionHandler(ar.cause());
        }
    });
    return this;
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Pump(io.vertx.core.streams.Pump)

Example 2 with OpenOptions

use of io.vertx.core.file.OpenOptions 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 3 with OpenOptions

use of io.vertx.core.file.OpenOptions 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 4 with OpenOptions

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

the class FileSystemTest method testDefaultOptionOptions.

@Test
public void testDefaultOptionOptions() {
    OpenOptions def = new OpenOptions();
    OpenOptions json = new OpenOptions(new JsonObject());
    assertEquals(def.getPerms(), json.getPerms());
    assertEquals(def.isRead(), json.isRead());
    assertEquals(def.isWrite(), json.isWrite());
    assertEquals(def.isCreate(), json.isCreate());
    assertEquals(def.isCreateNew(), json.isCreateNew());
    assertEquals(def.isDeleteOnClose(), json.isDeleteOnClose());
    assertEquals(def.isTruncateExisting(), json.isTruncateExisting());
    assertEquals(def.isSparse(), json.isSparse());
    assertEquals(def.isSync(), json.isSync());
    assertEquals(def.isDsync(), json.isDsync());
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 5 with OpenOptions

use of io.vertx.core.file.OpenOptions 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)

Aggregations

OpenOptions (io.vertx.core.file.OpenOptions)19 Test (org.junit.Test)14 Buffer (io.vertx.core.buffer.Buffer)12 AsyncFile (io.vertx.core.file.AsyncFile)10 IOException (java.io.IOException)7 Vertx (io.vertx.core.Vertx)5 Pump (io.vertx.core.streams.Pump)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ByteBuf (io.netty.buffer.ByteBuf)3 JsonObject (io.vertx.core.json.JsonObject)3 ReadStream (io.vertx.core.streams.ReadStream)3 WriteStream (io.vertx.core.streams.WriteStream)3 Unpooled (io.netty.buffer.Unpooled)2 AsyncResult (io.vertx.core.AsyncResult)2 Handler (io.vertx.core.Handler)2 FileProps (io.vertx.core.file.FileProps)2 FileSystemException (io.vertx.core.file.FileSystemException)2 FileSystemProps (io.vertx.core.file.FileSystemProps)2 AsyncFileImpl (io.vertx.core.file.impl.AsyncFileImpl)2 Utils (io.vertx.core.impl.Utils)2