Search in sources :

Example 11 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 12 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)

Example 13 with OpenOptions

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

the class FileSystemTest method testDrainNotCalledAfterClose.

@Test
public void testDrainNotCalledAfterClose() throws Exception {
    String fileName = "some-file.dat";
    vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), onSuccess(file -> {
        Buffer buf = TestUtils.randomBuffer(1024 * 1024);
        file.write(buf);
        AtomicBoolean drainAfterClose = new AtomicBoolean();
        file.drainHandler(v -> {
            drainAfterClose.set(true);
        });
        file.close(ar -> {
            assertFalse(drainAfterClose.get());
            testComplete();
        });
    }));
    await();
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) 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) Buffer(io.vertx.core.buffer.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 14 with OpenOptions

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

the class FileSystemTest method testWriteStream.

@Test
public void testWriteStream() throws Exception {
    String fileName = "some-file.dat";
    int chunkSize = 1000;
    int chunks = 10;
    byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
    Buffer buff = Buffer.buffer(content);
    vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
        if (ar.succeeded()) {
            WriteStream<Buffer> ws = ar.result();
            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(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) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with OpenOptions

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

the class FileSystemTest method testPumpFileStreams.

@Test
@SuppressWarnings("unchecked")
public void testPumpFileStreams() throws Exception {
    String fileName1 = "some-file.dat";
    String fileName2 = "some-other-file.dat";
    //Non integer multiple of buffer size
    int fileSize = (int) (AsyncFileImpl.DEFAULT_READ_BUFFER_SIZE * 1000.3);
    byte[] content = TestUtils.randomByteArray(fileSize);
    createFile(fileName1, content);
    vertx.fileSystem().open(testDir + pathSep + fileName1, new OpenOptions(), arr -> {
        if (arr.succeeded()) {
            ReadStream rs = arr.result();
            vertx.fileSystem().open(testDir + pathSep + fileName2, new OpenOptions(), ar -> {
                if (ar.succeeded()) {
                    WriteStream ws = ar.result();
                    Pump p = Pump.pump(rs, ws);
                    p.start();
                    rs.endHandler(v -> {
                        arr.result().close(car -> {
                            if (car.failed()) {
                                fail(ar.cause().getMessage());
                            } else {
                                ar.result().close(ar2 -> {
                                    if (ar2.failed()) {
                                        fail(ar2.cause().getMessage());
                                    } else {
                                        assertTrue(fileExists(fileName2));
                                        byte[] readBytes;
                                        try {
                                            readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName2));
                                        } catch (IOException e) {
                                            fail(e.getMessage());
                                            return;
                                        }
                                        assertEquals(Buffer.buffer(content), Buffer.buffer(readBytes));
                                        testComplete();
                                    }
                                });
                            }
                        });
                    });
                } else {
                    fail(ar.cause().getMessage());
                }
            });
        } else {
            fail(arr.cause().getMessage());
        }
    });
    await();
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) ReadStream(io.vertx.core.streams.ReadStream) WriteStream(io.vertx.core.streams.WriteStream) IOException(java.io.IOException) Pump(io.vertx.core.streams.Pump) 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