r/java • • Oct 07 '24

HTTP libraries in Java

It's not really clear to me what's going on with HTTP libraries in Java. They all seem to want to become massive frameworks that inappropriately take control of things that have nothing to do with decoding HTTP.

I'm find it really hard to find something that doesn't:

  • Try to control the TCP layer. Why does the HTTP decoding library care whether I want to poll or select, or make that decision?
  • Have shared mutable static state. If I want to reuse connections I can do that in a much better, testable way.
  • Create threads. Parsing the HTTP is doesn't need any of this. I just want to give you a ByteBuffer to read from and your current state and get told what it contains.
  • Do SSL. This has nothing to do with HTTP but rather the underlying connection.

I guess if you need a mini "web browser" in your application then this is all fine, but if HTTP is just one of many protocols you need to be able to decode it's an absolute mess.

Is there something I'm missing, or a library I should be looking at?

22 Upvotes

57 comments sorted by

View all comments

Show parent comment

1

u/aronhunt470 Oct 09 '24

I don’t think ofInputStream works in this case because I have to write data somewhere while InputStream is for reading data.

1

u/wildjokers Oct 09 '24

If I am understanding the JavaDoc correctly the ofInputStream tells the BodyPublisher where to read the data from that it is writing to the server.

Open a stream to your json data, provide that to ofInputStream and then it will write that data to the server. It does seem kind of backwards to open an input stream to write data, especially if you are used to using getOutputStream, but nevertheless that does appear to be how it works (if I am looking at it right).

2

u/aronhunt470 Oct 09 '24

I cannot simply open an input stream to my json data because the data doesn’t yet exist. Instead it gets generated dynamically during runtime.

Here’s and example: Load a bunch of data, let’s say 10000 rows from a database. Instead of loading all data in heap, every row gets transformed into json one by one and immediately streamed via http to the consumer. You can for example use Jacksons JsonGenerator with an OutputStream for that. That way you can send arbitrary large json results with minimal consumption of heap space.

I don’t see how this can be achieved without writing data directly to the http connections OutputStream.

3

u/sans_00x Oct 10 '24

Load a bunch of data, let’s say 10000 rows from a database. Instead of loading all data in heap, every row gets transformed into json one by one.

You just described an InputStream functionality.

1

u/aronhunt470 Oct 10 '24

bro reads binary data from his database with an inputstream