Search in sources :

Example 6 with AsyncFile

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

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

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

Example 9 with AsyncFile

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

the class FileSystemExamples method asyncFilePump.

public void asyncFilePump() {
    Vertx vertx = Vertx.vertx();
    final AsyncFile output = vertx.fileSystem().openBlocking("target/classes/plagiary.txt", new OpenOptions());
    vertx.fileSystem().open("target/classes/les_miserables.txt", new OpenOptions(), result -> {
        if (result.succeeded()) {
            AsyncFile file = result.result();
            Pump.pump(file, output).start();
            file.endHandler((r) -> {
                System.out.println("Copy done");
            });
        } else {
            System.err.println("Cannot open file " + result.cause());
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) Vertx(io.vertx.core.Vertx)

Example 10 with AsyncFile

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

the class FileSystemExamples method asyncFileRead.

public void asyncFileRead() {
    Vertx vertx = Vertx.vertx();
    vertx.fileSystem().open("target/classes/les_miserables.txt", new OpenOptions(), result -> {
        if (result.succeeded()) {
            AsyncFile file = result.result();
            Buffer buff = Buffer.buffer(1000);
            for (int i = 0; i < 10; i++) {
                file.read(buff, i * 100, i * 100, 100, ar -> {
                    if (ar.succeeded()) {
                        System.out.println("Read 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)

Aggregations

AsyncFile (io.vertx.core.file.AsyncFile)10 OpenOptions (io.vertx.core.file.OpenOptions)10 Buffer (io.vertx.core.buffer.Buffer)7 Test (org.junit.Test)5 Vertx (io.vertx.core.Vertx)4 IOException (java.io.IOException)3 ByteBuf (io.netty.buffer.ByteBuf)2 AsyncResult (io.vertx.core.AsyncResult)2 Handler (io.vertx.core.Handler)2 FileSystemException (io.vertx.core.file.FileSystemException)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 PosixFilePermissions (java.nio.file.attribute.PosixFilePermissions)2 HashSet (java.util.HashSet)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Unpooled (io.netty.buffer.Unpooled)1 Future (io.vertx.core.Future)1 FileProps (io.vertx.core.file.FileProps)1 FileSystemProps (io.vertx.core.file.FileSystemProps)1