java.lang.Object
org.cybergarage.http.HTTPSocket
HTTP socket wrapper providing input/output stream management
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Socket/////////////////////////////////////////////private InputStream/////////////////////////////////////////////private OutputStreamStream responses are written to.Constructor Summary
ConstructorsConstructorDescriptionHTTPSocket(Socket socket) Creates an HTTP socket wrapper around a standard socket.HTTPSocket(HTTPSocket socket) Creates an HTTP socket wrapper around another HTTP socket.Method Summary
Modifier and TypeMethodDescriptionbooleanclose()Closes the socket and all associated streams.Get the input streamGets the local address of this socket.intGets the local port of this socket.private OutputStreamGives the stream responses are written to.Returns the socket this object wraps.booleanopen()Opens the socket streams for input/output operations.private booleanpost(HTTPResponse httpRes, byte[] content, long contentOffset, long contentLength, boolean isOnlyHeader) Sends an HTTP response using byte array content.booleanpost(HTTPResponse httpRes, long contentOffset, long contentLength, boolean isOnlyHeader) Posts an HTTP response with the specified content parameters.private booleanpost(HTTPResponse httpRes, InputStream in, long contentOffset, long contentLength, boolean isOnlyHeader) Sends an HTTP response using input stream content.private voidSets the stream requests are read from.private voidSets the stream responses are written to.private voidSets the connection the request arrived on.
Field Details
socket
/////////////////////////////////////////////sockIn
/////////////////////////////////////////////sockOut
Stream responses are written to.
Constructor Details
HTTPSocket
Creates an HTTP socket wrapper around a standard socket.- Parameters:
socket- underlying socket to wrap
HTTPSocket
Creates an HTTP socket wrapper around another HTTP socket.- Parameters:
socket- HTTP socket to wrap
Method Details
setSocket
Sets the connection the request arrived on.- Parameters:
socket- the socket to use
getSocket
getLocalAddress
getLocalPort
public int getLocalPort()Gets the local port of this socket.- Returns:
- local port number
setInputStream
Sets the stream requests are read from.- Parameters:
in- the stream to use
getInputStream
setOutputStream
Sets the stream responses are written to.- Parameters:
out- the stream to use
getOutputStream
Gives the stream responses are written to.- Returns:
- the stream, null if the socket is closed
open
public boolean open()Opens the socket streams for input/output operations.- Returns:
- true if streams were opened successfully, false otherwise
close
public boolean close()Closes the socket and all associated streams.- Returns:
- true if closed successfully
post
private boolean post(HTTPResponse httpRes, byte[] content, long contentOffset, long contentLength, boolean isOnlyHeader) Sends an HTTP response using byte array content.This private helper method sends an HTTP response with the specified content. It handles both regular and chunked transfer encoding, sets appropriate headers, and manages the content offset and length for partial content responses.
- Parameters:
httpRes- the HTTP response to sendcontent- the content data as byte arraycontentOffset- the starting offset in the content arraycontentLength- the length of content to sendisOnlyHeader- if true, only send headers without content body- Returns:
- true if the response was sent successfully, false otherwise
post
private boolean post(HTTPResponse httpRes, InputStream in, long contentOffset, long contentLength, boolean isOnlyHeader) Sends an HTTP response using input stream content.This private helper method sends an HTTP response with content from an input stream. It handles both regular and chunked transfer encoding, reads content in chunks, and manages the content offset for partial content responses.
- Parameters:
httpRes- the HTTP response to sendin- the input stream containing content datacontentOffset- the offset to skip in the input stream before readingcontentLength- the total length of content to sendisOnlyHeader- if true, only send headers without content body- Returns:
- true if the response was sent successfully, false otherwise
post
public boolean post(HTTPResponse httpRes, long contentOffset, long contentLength, boolean isOnlyHeader) Posts an HTTP response with the specified content parameters.- Parameters:
httpRes- the HTTP response to postcontentOffset- the offset in the content to start fromcontentLength- the length of content to sendisOnlyHeader- true if only headers should be sent- Returns:
- true if posted successfully