Search in sources :

Example 6 with OpenOptions

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

the class FileSystemExamples method example3.

public void example3(FileSystem fileSystem) {
    OpenOptions options = new OpenOptions();
    fileSystem.open("myfile.txt", options, res -> {
        if (res.succeeded()) {
            AsyncFile file = res.result();
        } else {
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile)

Example 7 with OpenOptions

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

the class FileSystemTest method testWriteAsync.

@Test
public void testWriteAsync() throws Exception {
    String fileName = "some-file.dat";
    int chunkSize = 1000;
    int chunks = 10;
    byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
    Buffer buff = Buffer.buffer(content);
    AtomicInteger count = new AtomicInteger();
    vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), arr -> {
        if (arr.succeeded()) {
            for (int i = 0; i < chunks; i++) {
                Buffer chunk = buff.getBuffer(i * chunkSize, (i + 1) * chunkSize);
                assertEquals(chunkSize, chunk.length());
                arr.result().write(chunk, i * chunkSize, ar -> {
                    if (ar.succeeded()) {
                        if (count.incrementAndGet() == chunks) {
                            arr.result().close(ar2 -> {
                                if (ar2.failed()) {
                                    fail(ar2.cause().getMessage());
                                } else {
                                    assertTrue(fileExists(fileName));
                                    byte[] readBytes;
                                    try {
                                        readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName));
                                    } catch (IOException e) {
                                        fail(e.getMessage());
                                        return;
                                    }
                                    Buffer read = Buffer.buffer(readBytes);
                                    assertEquals(buff, read);
                                    testComplete();
                                }
                            });
                        }
                    } else {
                        fail(ar.cause().getMessage());
                    }
                });
            }
        } else {
            fail(arr.cause().getMessage());
        }
    });
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) OpenOptions(io.vertx.core.file.OpenOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with OpenOptions

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

the class FileSystemTest method testWriteStreamAppend.

@Test
public void testWriteStreamAppend() throws Exception {
    String fileName = "some-file.dat";
    int chunkSize = 1000;
    int chunks = 10;
    byte[] existing = TestUtils.randomByteArray(1000);
    createFile(fileName, existing);
    byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
    Buffer buff = Buffer.buffer(content);
    vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
        if (ar.succeeded()) {
            AsyncFile ws = ar.result();
            long size = vertx.fileSystem().propsBlocking(testDir + pathSep + fileName).size();
            ws.setWritePos(size);
            ws.exceptionHandler(t -> fail(t.getMessage()));
            for (int i = 0; i < chunks; i++) {
                Buffer chunk = buff.getBuffer(i * chunkSize, (i + 1) * chunkSize);
                assertEquals(chunkSize, chunk.length());
                ws.write(chunk);
            }
            ar.result().close(ar2 -> {
                if (ar2.failed()) {
                    fail(ar2.cause().getMessage());
                } else {
                    assertTrue(fileExists(fileName));
                    byte[] readBytes;
                    try {
                        readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName));
                    } catch (IOException e) {
                        fail(e.getMessage());
                        return;
                    }
                    assertEquals(Buffer.buffer(existing).appendBuffer(buff), Buffer.buffer(readBytes));
                    testComplete();
                }
            });
        } else {
            fail(ar.cause().getMessage());
        }
    });
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with OpenOptions

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

the class FileSystemTest method testReadStream.

@Test
public void testReadStream() 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()) {
            ReadStream<Buffer> rs = ar.result();
            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(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) Test(org.junit.Test)

Example 10 with OpenOptions

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

the class FileSystemTest method testOpenOptions.

@Test
public void testOpenOptions() {
    OpenOptions opts = new OpenOptions();
    assertNull(opts.getPerms());
    String perms = "rwxrwxrwx";
    assertEquals(opts, opts.setPerms(perms));
    assertEquals(perms, opts.getPerms());
    assertTrue(opts.isCreate());
    assertEquals(opts, opts.setCreate(false));
    assertFalse(opts.isCreate());
    assertFalse(opts.isCreateNew());
    assertEquals(opts, opts.setCreateNew(true));
    assertTrue(opts.isCreateNew());
    assertTrue(opts.isRead());
    assertEquals(opts, opts.setRead(false));
    assertFalse(opts.isRead());
    assertTrue(opts.isWrite());
    assertEquals(opts, opts.setWrite(false));
    assertFalse(opts.isWrite());
    assertFalse(opts.isDsync());
    assertEquals(opts, opts.setDsync(true));
    assertTrue(opts.isDsync());
    assertFalse(opts.isSync());
    assertEquals(opts, opts.setSync(true));
    assertTrue(opts.isSync());
    assertFalse(opts.isDeleteOnClose());
    assertEquals(opts, opts.setDeleteOnClose(true));
    assertTrue(opts.isDeleteOnClose());
    assertFalse(opts.isTruncateExisting());
    assertEquals(opts, opts.setTruncateExisting(true));
    assertTrue(opts.isTruncateExisting());
    assertFalse(opts.isSparse());
    assertEquals(opts, opts.setSparse(true));
    assertTrue(opts.isSparse());
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) 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