0

I am new to VertX and I want to read a pdf using the "GET" method. I know that buffer will be used. But there are no resources on the internet on how to do that.

1 Answers1

0

Omitting the details of how you would get the file from your data store (couchbase DB), it is fair to assume the data is read correctly into a byte[].

Once the data is read, you can feed it to an io.vertx.core.buffer.Buffer that can be used to shuffle data to the HttpServerResponse as follows:

public void sendPDFFile(byte[] fileBytes, HttpServerResponse response) {
    Buffer buffer = Buffer.buffer(fileBytes);
    response.putHeader("Content-Type", "application/pdf")
            .putHeader("Content-Length", String.valueOf(buffer.length()))
            .setStatusCode(200)
            .end(buffer);
}
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • What is the purpose of storing it in byte[] array and then using Buffer.buffer() ? – Utkal keshari Sahu Oct 11 '21 at 01:56
  • Because `io.vertx.core.buffer.Buffer` is the internal data wrapper used by *Vert.x* to move data around and to achieve common data access interface. Additionally, using the specific `io.vertx.core.buffer.Buffer` type allows to pipe and stream data as needed instead of sending batched data. – tmarwen Oct 11 '21 at 10:25
  • Got it. Thank you. – Utkal keshari Sahu Oct 11 '21 at 15:18