Search in sources :

Example 16 with OpenOptions

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

the class FileSystemTest method testWriteStreamWithCompositeBuffer.

@Test
public void testWriteStreamWithCompositeBuffer() throws Exception {
    String fileName = "some-file.dat";
    int chunkSize = 1000;
    int chunks = 10;
    byte[] content1 = TestUtils.randomByteArray(chunkSize * (chunks / 2));
    byte[] content2 = TestUtils.randomByteArray(chunkSize * (chunks / 2));
    ByteBuf byteBuf = Unpooled.wrappedBuffer(content1, content2);
    Buffer buff = Buffer.buffer(byteBuf);
    vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
        if (ar.succeeded()) {
            WriteStream<Buffer> ws = ar.result();
            ws.exceptionHandler(t -> fail(t.getMessage()));
            ws.write(buff);
            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));
                    byteBuf.release();
                    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) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 17 with OpenOptions

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

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

the class FileSystemTest method testReadAsync.

@Test
public void testReadAsync() throws Exception {
    String fileName = "some-file.dat";
    int chunkSize = 1000;
    int chunks = 10;
    byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
    Buffer expected = Buffer.buffer(content);
    createFile(fileName, content);
    AtomicInteger reads = new AtomicInteger();
    vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), arr -> {
        if (arr.succeeded()) {
            Buffer buff = Buffer.buffer(chunks * chunkSize);
            for (int i = 0; i < chunks; i++) {
                arr.result().read(buff, i * chunkSize, i * chunkSize, chunkSize, arb -> {
                    if (arb.succeeded()) {
                        if (reads.incrementAndGet() == chunks) {
                            arr.result().close(ar -> {
                                if (ar.failed()) {
                                    fail(ar.cause().getMessage());
                                } else {
                                    assertEquals(expected, buff);
                                    assertEquals(buff, arb.result());
                                    testComplete();
                                }
                            });
                        }
                    } else {
                        fail(arb.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) Test(org.junit.Test)

Example 19 with OpenOptions

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

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