net.noderunner.http
Class GeneralDataPoster

java.lang.Object
  extended by net.noderunner.http.GeneralDataPoster
All Implemented Interfaces:
DataPoster

public class GeneralDataPoster
extends java.lang.Object
implements DataPoster

A utility class for sending binary data to an HTTP server or client. Example usage:

 HttpClient client = new RetryHttpClient("http://example.net");
 InputStream fileIS = new FileInputStream("somefile.jpeg");
 DataPoster dataPoster = new GeneralDataPoster(fileIS, -1);
 RequestLine rl = new RequestLineImpl( ... );
 ClientRequest request = new ClientRequestImpl(rl, dataPoster);

 client.writeRequest(request);
 ClientResponse response = client.readResponse();
 


Field Summary
protected  int DEFAULT_BUFFER_SIZE
          Default copy buffer size to create upon initialization.
 
Constructor Summary
GeneralDataPoster()
          Constructs a new, uninitialized GeneralDataPoster.
GeneralDataPoster(java.io.InputStream is, int len)
          Constructs a new GeneralDataPoster that outputs data from the specified stream.
 
Method Summary
 void init(java.io.InputStream is, int len)
          Sets the input stream to use and the number of bytes to send.
 void sendData(java.io.OutputStream os)
          Copies our input stream data to the supplied output stream.
 java.lang.String toString()
          Returns a debug string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

protected int DEFAULT_BUFFER_SIZE
Default copy buffer size to create upon initialization. Default size is 1024 bytes.

Constructor Detail

GeneralDataPoster

public GeneralDataPoster()
Constructs a new, uninitialized GeneralDataPoster. The init method must be called before this object can be used.


GeneralDataPoster

public GeneralDataPoster(java.io.InputStream is,
                         int len)
Constructs a new GeneralDataPoster that outputs data from the specified stream. Calls init.

Method Detail

init

public void init(java.io.InputStream is,
                 int len)
Sets the input stream to use and the number of bytes to send.

Parameters:
is - input stream to read for output
len - if zero, we do nothing, if < 0, we send chunked data, if > 0 we sent only len number of bytes

sendData

public void sendData(java.io.OutputStream os)
              throws java.io.IOException
Copies our input stream data to the supplied output stream. Does nothing if the number of bytes to send is zero.

Specified by:
sendData in interface DataPoster
Parameters:
os - output stream to send data to
Throws:
IllegalHttpStateException - if the input stream was never set
HttpException - if the data was already read from our stream, and the input stream cannot be reset
java.io.IOException - if data could not be written

toString

public java.lang.String toString()
Returns a debug string.

Overrides:
toString in class java.lang.Object


Copyright © 2010. All Rights Reserved.