Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

HTTPClient
Class HttpOutputStream  view HttpOutputStream download HttpOutputStream.java

java.lang.Object
  extended byjava.io.OutputStream
      extended byHTTPClient.HttpOutputStream
All Implemented Interfaces:
GlobalConstants

public class HttpOutputStream
extends java.io.OutputStream
implements GlobalConstants

This class provides an output stream for requests. The stream must first be associated with a request before it may be used; this is done by passing it to one of the request methods in HTTPConnection. Example:

    OutputStream out = new HttpOutputStream(12345);
    rsp = con.Post("/cgi-bin/my_cgi", out);
    out.write(...);
    out.close();
    if (rsp.getStatusCode() >= 300)
        ...
 

There are two constructors for this class, one taking a length parameter, and one without any parameters. If the stream is created with a length then the request will be sent with the corresponding Content-length header and anything written to the stream will be written on the socket immediately. This is the preferred way. If the stream is created without a length then one of two things will happen: if, at the time of the request, the server is known to understand HTTP/1.1 then each write() will send the data immediately using the chunked encoding. If, however, either the server version is unknown (because this is first request to that server) or the server only understands HTTP/1.0 then all data will be written to a buffer first, and only when the stream is closed will the request be sent.

Another reason that using the HttpOutputStream(length) constructor is recommended over the HttpOutputStream() one is that some HTTP/1.1 servers do not allow the chunked transfer encoding to be used when POSTing to a cgi script. This is because the way the cgi API is defined the cgi script expects a Content-length environment variable. If the data is sent using the chunked transfer encoding however, then the server would have to buffer all the data before invoking the cgi so that this variable could be set correctly. Not all servers are willing to do this.

If you cannot use the HttpOutputStream(length) constructor and are having problems sending requests (usually a 411 response) then you can try setting the system property HTTPClient.dontChunkRequests to true (this needs to be done either on the command line or somewhere in the code before the HTTPConnection is first accessed). This will prevent the client from using the chunked encoding in this case and will cause the HttpOutputStream to buffer all the data instead, sending it only when close() is invoked.

The behaviour of a request sent with an output stream may differ from that of a request sent with a data parameter. The reason for this is that the various modules cannot resend a request which used an output stream. Therefore such things as authorization and retrying of requests won't be done by the HTTPClient for such requests.

Since:
V0.3
Version:
0.3-2 18/06/1999

Field Summary
private  java.io.ByteArrayOutputStream bos
          the buffer to be used if needed
static int CD_0
           
static int CD_CHUNKED
           
static int CD_CLOSE
           
static int CD_CONTLEN
           
static int CD_HDRS
           
static int CD_MP_BR
           
static int CD_NONE
          Content delimiters
private  int con_to
          the timeout to pass to SendRequest()
static boolean DebugAll
          Debug variables
static boolean DebugAuth
           
static boolean DebugConn
           
static boolean DebugDemux
           
static boolean DebugMods
           
static boolean DebugResp
           
static boolean DebugSocks
           
static boolean DebugURLC
           
private static NVPair[] empty
          null trailers
static int HTTP
          possible http protocols we (might) handle
static int HTTP_1_0
          some known http versions
static int HTTP_1_1
           
static int HTTP_NG
           
static int HTTPS
           
private  boolean ignore
          just ignore all the data if told to do so
private  int length
          the length of the data to be sent
private  java.io.OutputStream os
          the socket output stream
private  int rcvd
          the length of the data received so far
private  Request req
          the request this stream is associated with
private  Response resp
          the response from sendRequest if we stalled the request
static int SHTTP
           
private  NVPair[] trailers
          the trailers to send if using chunked encoding.
 
Constructor Summary
HttpOutputStream()
          Creates an output stream of unspecified length.
HttpOutputStream(int length)
          This creates an output stream which will take length bytes of data.
 
Method Summary
 void close()
          Closes the stream and causes the data to be sent if it has not already been done so.
 int getLength()
          Returns the number of bytes this stream is willing to accept, or -1 if it is unbounded.
(package private)  Response getResponse()
          Return the response we got from sendRequest().
 NVPair[] getTrailers()
          Gets the trailers which were set with setTrailers().
(package private)  void goAhead(Request req, java.io.OutputStream os, int con_to)
          Associates this stream with a request and the actual output stream.
(package private)  void ignoreData(Request req)
          Setup this stream to dump the data to the great bit-bucket in the sky.
 void setTrailers(NVPair[] trailers)
          Sets the trailers to be sent if the output is sent with the chunked transfer encoding.
 java.lang.String toString()
          produces a string describing this stream.
 void write(byte[] buf, int off, int len)
          Writes an array of bytes on the stream.
 void write(int b)
          Writes a single byte on the stream.
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

empty

private static final NVPair[] empty
null trailers


length

private int length
the length of the data to be sent


rcvd

private int rcvd
the length of the data received so far


req

private Request req
the request this stream is associated with


resp

private Response resp
the response from sendRequest if we stalled the request


os

private java.io.OutputStream os
the socket output stream


bos

private java.io.ByteArrayOutputStream bos
the buffer to be used if needed


trailers

private NVPair[] trailers
the trailers to send if using chunked encoding.


con_to

private int con_to
the timeout to pass to SendRequest()


ignore

private boolean ignore
just ignore all the data if told to do so


DebugAll

public static final boolean DebugAll
Debug variables

See Also:
Constant Field Values

DebugConn

public static final boolean DebugConn
See Also:
Constant Field Values

DebugResp

public static final boolean DebugResp
See Also:
Constant Field Values

DebugDemux

public static final boolean DebugDemux
See Also:
Constant Field Values

DebugAuth

public static final boolean DebugAuth
See Also:
Constant Field Values

DebugSocks

public static final boolean DebugSocks
See Also:
Constant Field Values

DebugMods

public static final boolean DebugMods
See Also:
Constant Field Values

DebugURLC

public static final boolean DebugURLC
See Also:
Constant Field Values

HTTP

public static final int HTTP
possible http protocols we (might) handle

See Also:
Constant Field Values

HTTPS

public static final int HTTPS
See Also:
Constant Field Values

SHTTP

public static final int SHTTP
See Also:
Constant Field Values

HTTP_NG

public static final int HTTP_NG
See Also:
Constant Field Values

HTTP_1_0

public static final int HTTP_1_0
some known http versions

See Also:
Constant Field Values

HTTP_1_1

public static final int HTTP_1_1
See Also:
Constant Field Values

CD_NONE

public static final int CD_NONE
Content delimiters

See Also:
Constant Field Values

CD_HDRS

public static final int CD_HDRS
See Also:
Constant Field Values

CD_0

public static final int CD_0
See Also:
Constant Field Values

CD_CLOSE

public static final int CD_CLOSE
See Also:
Constant Field Values

CD_CONTLEN

public static final int CD_CONTLEN
See Also:
Constant Field Values

CD_CHUNKED

public static final int CD_CHUNKED
See Also:
Constant Field Values

CD_MP_BR

public static final int CD_MP_BR
See Also:
Constant Field Values
Constructor Detail

HttpOutputStream

public HttpOutputStream()
Creates an output stream of unspecified length. Note that it is highly recommended that this constructor be avoided where possible and HttpOutputStream(int) used instead.


HttpOutputStream

public HttpOutputStream(int length)
This creates an output stream which will take length bytes of data.

Method Detail

goAhead

void goAhead(Request req,
             java.io.OutputStream os,
             int con_to)
Associates this stream with a request and the actual output stream. No other methods in this class may be invoked until this method has been invoked by the HTTPConnection.


ignoreData

void ignoreData(Request req)
Setup this stream to dump the data to the great bit-bucket in the sky. This is needed for when a module handles the request directly.


getResponse

Response getResponse()
Return the response we got from sendRequest(). This waits until the request has actually been sent.


getLength

public int getLength()
Returns the number of bytes this stream is willing to accept, or -1 if it is unbounded.


getTrailers

public NVPair[] getTrailers()
Gets the trailers which were set with setTrailers().


setTrailers

public void setTrailers(NVPair[] trailers)
Sets the trailers to be sent if the output is sent with the chunked transfer encoding. These must be set before the output stream is closed for them to be sent.

Any trailers set here should be mentioned in a Trailer header in the request (see section 14.40 of draft-ietf-http-v11-spec-rev-06.txt).

This method (and its related getTrailers())) are in this class and not in Request because setting trailers is something an application may want to do, not only modules.


write

public void write(int b)
           throws java.io.IOException,
                  java.lang.IllegalAccessError
Writes a single byte on the stream. It is subject to the same rules as write(byte[], int, int).


write

public void write(byte[] buf,
                  int off,
                  int len)
           throws java.io.IOException,
                  java.lang.IllegalAccessError
Writes an array of bytes on the stream. This method may not be used until this stream has been passed to one of the methods in HTTPConnection (i.e. until it has been associated with a request).


close

public void close()
           throws java.io.IOException,
                  java.lang.IllegalAccessError
Closes the stream and causes the data to be sent if it has not already been done so. This method must be invoked when all data has been written.


toString

public java.lang.String toString()
produces a string describing this stream.