/** * Tell the operating system to stream a file as specified by {@code filename} directly from disk to the outgoing connection, * bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files. * * @param filename file name of the file to send * @param offset offset * @return a reference to this, so the API can be used fluently */ @Fluent default NetSocket sendFile(String filename, long offset) { return sendFile(filename, offset, Long.MAX_VALUE); }
/** * Tell the operating system to stream a file as specified by {@code filename} directly from disk to the outgoing connection, * bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files. * * @param filename file name of the file to send * @return a reference to this, so the API can be used fluently */ @Fluent default NetSocket sendFile(String filename) { return sendFile(filename, 0, Long.MAX_VALUE); }
/** * Same as {@link #sendFile(String)} but also takes a handler that will be called when the send has completed or * a failure has occurred * * @param filename file name of the file to send * @param resultHandler handler * @return a reference to this, so the API can be used fluently */ @Fluent default NetSocket sendFile(String filename, Handler<AsyncResult<Void>> resultHandler) { return sendFile(filename, 0, Long.MAX_VALUE, resultHandler); }
/** * Same as {@link #sendFile(String, long)} but also takes a handler that will be called when the send has completed or * a failure has occurred * * @param filename file name of the file to send * @param offset offset * @param resultHandler handler * @return a reference to this, so the API can be used fluently */ @Fluent default NetSocket sendFile(String filename, long offset, Handler<AsyncResult<Void>> resultHandler) { return sendFile(filename, offset, Long.MAX_VALUE, resultHandler); }
@Test public void testSendFileDirectory() throws Exception { File fDir = testFolder.newFolder(); server.connectHandler(socket -> { socket.handler(buff -> { fail("Should not receive any data"); }); }).listen(testAddress, ar -> { assertTrue(ar.succeeded()); client.connect(testAddress, result -> { assertTrue(result.succeeded()); NetSocket socket = result.result(); try { socket.sendFile(fDir.getAbsolutePath().toString()); // should throw exception and never hit the assert fail("Should throw exception"); } catch (IllegalArgumentException e) { testComplete(); } }); }); await(); }
@Test public void sendFileClientToServer() throws Exception { File fDir = testFolder.newFolder(); String content = TestUtils.randomUnicodeString(10000); File file = setupFile(fDir.toString(), "some-file.txt", content); Buffer expected = Buffer.buffer(content); Buffer received = Buffer.buffer(); server.connectHandler(sock -> { sock.handler(buff -> { received.appendBuffer(buff); if (received.length() == expected.length()) { assertEquals(expected, received); testComplete(); } }); // Send some data to the client to trigger the sendfile sock.write("foo"); }); server.listen(testAddress, ar -> { assertTrue(ar.succeeded()); client.connect(testAddress, ar2 -> { assertTrue(ar2.succeeded()); NetSocket sock = ar2.result(); sock.handler(buf -> { sock.sendFile(file.getAbsolutePath()); }); }); }); await(); }
@Test public void sendFileServerToClient() throws Exception { File fDir = testFolder.newFolder(); String content = TestUtils.randomUnicodeString(10000); File file = setupFile(fDir.toString(), "some-file.txt", content); Buffer expected = Buffer.buffer(content); Buffer received = Buffer.buffer(); server.connectHandler(sock -> { sock.handler(buf -> { sock.sendFile(file.getAbsolutePath()); }); }); server.listen(testAddress, ar -> { assertTrue(ar.succeeded()); client.connect(testAddress, ar2 -> { assertTrue(ar2.succeeded()); NetSocket sock = ar2.result(); sock.handler(buff -> { received.appendBuffer(buff); if (received.length() == expected.length()) { assertEquals(expected, received); testComplete(); } }); sock.write("foo"); }); }); await(); }
/** * Tell the operating system to stream a file as specified by {@code filename} directly from disk to the outgoing connection, * bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files. * * @param filename file name of the file to send * @return a reference to this, so the API can be used fluently */ @Fluent default NetSocket sendFile(String filename) { return sendFile(filename, 0, Long.MAX_VALUE); }
/** * Same as {@link #sendFile(String)} but also takes a handler that will be called when the send has completed or * a failure has occurred * * @param filename file name of the file to send * @param resultHandler handler * @return a reference to this, so the API can be used fluently */ @Fluent default NetSocket sendFile(String filename, Handler<AsyncResult<Void>> resultHandler) { return sendFile(filename, 0, Long.MAX_VALUE, resultHandler); }
/** * Tell the operating system to stream a file as specified by {@code filename} directly from disk to the outgoing connection, * bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files. * * @param filename file name of the file to send * @param offset offset * @return a reference to this, so the API can be used fluently */ @Fluent default NetSocket sendFile(String filename, long offset) { return sendFile(filename, offset, Long.MAX_VALUE); }
/** * Same as {@link #sendFile(String, long)} but also takes a handler that will be called when the send has completed or * a failure has occurred * * @param filename file name of the file to send * @param offset offset * @param resultHandler handler * @return a reference to this, so the API can be used fluently */ @Fluent default NetSocket sendFile(String filename, long offset, Handler<AsyncResult<Void>> resultHandler) { return sendFile(filename, offset, Long.MAX_VALUE, resultHandler); }
server.close(); Consumer<NetSocket> sender = so -> { so.sendFile(sent.getAbsolutePath()); }; Consumer<NetSocket> receiver = so -> {
/** * Tell the operating system to stream a file as specified by <code>filename</code> directly from disk to the outgoing connection, * bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files. * @param filename file name of the file to send * @param offset offset * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.core.net.NetSocket sendFile(String filename, long offset) { delegate.sendFile(filename, offset); return this; }
/** * Same as {@link io.vertx.rxjava.core.net.NetSocket#sendFile} but also takes a handler that will be called when the send has completed or * a failure has occurred * @param filename file name of the file to send * @param resultHandler handler * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.core.net.NetSocket sendFile(String filename, Handler<AsyncResult<Void>> resultHandler) { delegate.sendFile(filename, resultHandler); return this; }
/** * Tell the operating system to stream a file as specified by <code>filename</code> directly from disk to the outgoing connection, * bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files. * @param filename file name of the file to send * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.core.net.NetSocket sendFile(String filename) { delegate.sendFile(filename); return this; }
/** * Same as {@link io.vertx.rxjava.core.net.NetSocket#sendFile} but also takes a handler that will be called when the send has completed or * a failure has occurred * @param filename file name of the file to send * @param offset offset * @param resultHandler handler * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.core.net.NetSocket sendFile(String filename, long offset, Handler<AsyncResult<Void>> resultHandler) { delegate.sendFile(filename, offset, resultHandler); return this; }
@Test public void testSendFileDirectory() throws Exception { File fDir = testFolder.newFolder(); server.connectHandler(socket -> { socket.handler(buff -> { fail("Should not receive any data"); }); }).listen(testAddress, ar -> { assertTrue(ar.succeeded()); client.connect(testAddress, result -> { assertTrue(result.succeeded()); NetSocket socket = result.result(); try { socket.sendFile(fDir.getAbsolutePath().toString()); // should throw exception and never hit the assert fail("Should throw exception"); } catch (IllegalArgumentException e) { testComplete(); } }); }); await(); }
@Test public void sendFileServerToClient() throws Exception { File fDir = testFolder.newFolder(); String content = TestUtils.randomUnicodeString(10000); File file = setupFile(fDir.toString(), "some-file.txt", content); Buffer expected = Buffer.buffer(content); Buffer received = Buffer.buffer(); server.connectHandler(sock -> { sock.handler(buf -> { sock.sendFile(file.getAbsolutePath()); }); }); server.listen(testAddress, ar -> { assertTrue(ar.succeeded()); client.connect(testAddress, ar2 -> { assertTrue(ar2.succeeded()); NetSocket sock = ar2.result(); sock.handler(buff -> { received.appendBuffer(buff); if (received.length() == expected.length()) { assertEquals(expected, received); testComplete(); } }); sock.write("foo"); }); }); await(); }
@Test public void sendFileClientToServer() throws Exception { File fDir = testFolder.newFolder(); String content = TestUtils.randomUnicodeString(10000); File file = setupFile(fDir.toString(), "some-file.txt", content); Buffer expected = Buffer.buffer(content); Buffer received = Buffer.buffer(); server.connectHandler(sock -> { sock.handler(buff -> { received.appendBuffer(buff); if (received.length() == expected.length()) { assertEquals(expected, received); testComplete(); } }); // Send some data to the client to trigger the sendfile sock.write("foo"); }); server.listen(testAddress, ar -> { assertTrue(ar.succeeded()); client.connect(testAddress, ar2 -> { assertTrue(ar2.succeeded()); NetSocket sock = ar2.result(); sock.handler(buf -> { sock.sendFile(file.getAbsolutePath()); }); }); }); await(); }
server.close(); Consumer<NetSocket> sender = so -> { so.sendFile(sent.getAbsolutePath()); }; Consumer<NetSocket> receiver = so -> {