0

I want to start sending data to HTTP server before whole data is created.

This is quite easy when you use java.net.HttpURLConnection:

urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);

dos = new DataOutputStream(urlConnection.getOutputStream());
...
dos.writeShort(s);
...

But for some reasons I want to do it using org.apache.http packages (I have to develop a library based on package org.apache.http). I have read its documentation, but I didn't find anything similar to the code above. Is it possible to send data to HTTP server using org.apache.http packages in chunks before knowing final data size?

Thanks in advance for all suggestions ;)

Grzes
  • 971
  • 1
  • 13
  • 28
  • HttpCore seems to support Chunk coding. An HttpEntity can be sent in chunks if `isChunked()` returns true. How to use them or what appropriate entity to use I do not yet know. – E_net4 Sep 04 '12 at 10:59
  • I knew earlier that org.apache.http packages support sending content in chunks, but I could't find a way to send content which size is unknown when http connection is established. But I went deeper into documentation, I found a method setContent(InputStream instream) in BasicHttpEnitity (http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/entity/BasicHttpEntity.html). And this is it ;) – Grzes Sep 04 '12 at 20:42
  • Go ahead and answer your own question then. It will most likely help other people trying to do the same. :) – E_net4 Sep 04 '12 at 22:49

1 Answers1

2

Sending data in chunk not knowing its final size is also quite easy using Apache library. Here is a simple example:

DataInputStream dis;
...
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080");

BasicHttpEntity entity = new BasicHttpEntity();
entity.setChunked(true);
entity.setContentLength(-1);
entity.setContent(dis);

httppost.setEntity(entity);
HttpResponse response = null;
try {
     response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
    // TODO
} catch (IOException e) {
    // TODO
}
...
// processing http response....

dis is a stream which should contain entity body. You can pipe dis input stream with an output stream using piped streams. Therefore, one thread might be creating data (e.g. recording sound from microphone) and the other one might send it to the server.

// creating piped streams
private PipedInputStream pis;
private PipedOutputStream pos;
private DataOutputStream dos;
private DataInputStream dis;

...

pos = new PipedOutputStream();
pis = new PipedInputStream(pos);
dos = new DataOutputStream(pos);
dis = new DataInputStream(pis);

// in thread creating data dynamically
try {
    // writing data to dos stream
    ...
    dos.write(b);
    ...
} catch (IOException e) {
    // TODO
}

// Before finishing thread, we have to flush and close dos stream
// then dis stream will know that all data have been written and will finish
// streaming data to server.
try {
    dos.flush();
    dos.close();
} catch (Exception e) {
    // TODO
}

dos should be passed to thread which creates data dynamically, dis to the one sending data to the server.

See also: http://www.androidadb.com/class/ba/BasicHttpEntity.html

Grzes
  • 971
  • 1
  • 13
  • 28
  • I honestly dislike those PipedStreams, but I suppose the key trick to making chunks is there. – E_net4 Sep 06 '12 at 14:34
  • I will be grateful to you if you suggest better solution for sending data from one thread to another ;) – Grzes Sep 06 '12 at 19:03
  • My opinion on these streams seems well explained in this question. http://stackoverflow.com/questions/484119/why-doesnt-more-java-code-use-pipedinputstream-pipedoutputstream I've also learned that closing the pipe from one end will immediately close the other, preventing all the remaining data from reaching the input stream pipe end. In one of my projects I made my own buffer class and wrapped it with locks and conditions, but there may be other, better and high-level ways. – E_net4 Sep 06 '12 at 20:14