Skip to content

Commit

Permalink
Remove pump from examples and replace it with pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Dec 11, 2023
1 parent 0a8845a commit b6194db
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
4 changes: 2 additions & 2 deletions core-examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ crafted URI paths.

=== Http request body upload

This examples demonstrates an HTTP server receiving a request and pumping the request body to a file on disk without
This examples demonstrates an HTTP server receiving a request and piping the request body to a file on disk without
ever storing the entire request body fully in memory.

There's also a client which sends a request to the server and pumps a file from disk to the HTTP request body. The file
There's also a client which sends a request to the server and pipes a file from disk to the HTTP request body. The file
is uploaded successfully even if the file is very large (GigaBytes).

link:src/main/java/io/vertx/example/core/http/upload/Server.java[Java upload server example]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public void start() throws Exception {
String filename = "io/vertx/example/core/http/upload/upload.txt";
FileSystem fs = vertx.fileSystem();
return fs.props(filename).compose(props -> {
System.out.println("props is " + props);
long size = props.size();
req.headers().set("content-length", "" + size);
return fs.open(filename, new OpenOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Launcher;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.AsyncFile;
import io.vertx.core.file.OpenOptions;
import io.vertx.core.streams.Pump;
import io.vertx.core.streams.Pipe;

import java.util.UUID;

Expand All @@ -20,18 +21,23 @@ public static void main(String[] args) {
@Override
public void start() throws Exception {
vertx.createHttpServer().requestHandler(req -> {
req.pause();
Pipe<Buffer> pipe = req
.pipe()
.endOnComplete(true);
String filename = UUID.randomUUID() + ".uploaded";
vertx.fileSystem().open(filename, new OpenOptions(), ares -> {
AsyncFile file = ares.result();
Pump pump = Pump.pump(req, file);
req.endHandler(v1 -> file.close(v2 -> {
System.out.println("Uploaded to " + filename);
req.response().end();
}));
pump.start();
req.resume();
});
vertx.fileSystem()
.open(filename, new OpenOptions())
.transform(ar -> {
if (ar.succeeded()) {
return pipe.to(ar.result()).onComplete(ar2 -> {
System.out.println("Uploaded to " + filename);
});
} else {
return req.response()
.setStatusCode(500)
.end("Could not upload file");
}
});
}).listen(8080);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Launcher;
import io.vertx.core.streams.Pump;

/*
* @author <a href="http://tfox.org">Tim Fox</a>
Expand All @@ -18,8 +17,8 @@ public void start() throws Exception {

vertx.createNetServer().connectHandler(sock -> {

// Create a pump
Pump.pump(sock, sock).start();
// Create a pipe
sock.pipeTo(sock);

}).listen(1234);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.vertx.core.Launcher;
import io.vertx.core.net.JksOptions;
import io.vertx.core.net.NetServerOptions;
import io.vertx.core.streams.Pump;

/*
* @author <a href="http://tfox.org">Tim Fox</a>
Expand All @@ -26,8 +25,8 @@ public void start() throws Exception {

vertx.createNetServer(options).connectHandler(sock -> {

// Create a pump
Pump.pump(sock, sock).start();
// Create a pipe
sock.pipeTo(sock);

}).listen(1234);

Expand Down
2 changes: 1 addition & 1 deletion web-client-examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ link:src/main/java/io/vertx/example/webclient/send/multipartform/Client.java[Jav

This examples shows how to send a vertx read stream with the web client.

The web client takes care of setting up the pump between the read stream and the http client request.
The web client takes care of setting up the pipe between the read stream and the http client request.

First you need to run the server then you can run the client.

Expand Down

0 comments on commit b6194db

Please sign in to comment.