Package org.apache.http.impl
Class BHttpConnectionBase
- java.lang.Object
-
- org.apache.http.impl.BHttpConnectionBase
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,HttpConnection,HttpInetConnection
- Direct Known Subclasses:
DefaultBHttpClientConnection,DefaultBHttpServerConnection
public class BHttpConnectionBase extends java.lang.Object implements HttpInetConnection
This class serves as a base for allHttpConnectionimplementations and provides functionality common to both client and server HTTP connections.- Since:
- 4.0
-
-
Field Summary
Fields Modifier and Type Field Description private HttpConnectionMetricsImplconnMetricsprivate SessionInputBufferImplinBufferprivate ContentLengthStrategyincomingContentStrategyprivate MessageConstraintsmessageConstraintsprivate SessionOutputBufferImploutbufferprivate ContentLengthStrategyoutgoingContentStrategyprivate java.util.concurrent.atomic.AtomicReference<java.net.Socket>socketHolder
-
Constructor Summary
Constructors Modifier Constructor Description protectedBHttpConnectionBase(int bufferSize, int fragmentSizeHint, java.nio.charset.CharsetDecoder charDecoder, java.nio.charset.CharsetEncoder charEncoder, MessageConstraints messageConstraints, ContentLengthStrategy incomingContentStrategy, ContentLengthStrategy outgoingContentStrategy)Creates new instance of BHttpConnectionBase.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanawaitInput(int timeout)protected voidbind(java.net.Socket socket)Binds this connection to the givenSocket.voidclose()Closes this connection gracefully.protected java.io.InputStreamcreateInputStream(long len, SessionInputBuffer inBuffer)protected java.io.OutputStreamcreateOutputStream(long len, SessionOutputBuffer outbuffer)protected voiddoFlush()protected voidensureOpen()private intfillInputBuffer(int timeout)java.net.InetAddressgetLocalAddress()intgetLocalPort()HttpConnectionMetricsgetMetrics()Returns a collection of connection metrics.java.net.InetAddressgetRemoteAddress()intgetRemotePort()protected SessionInputBuffergetSessionInputBuffer()protected SessionOutputBuffergetSessionOutputBuffer()protected java.net.SocketgetSocket()protected java.io.InputStreamgetSocketInputStream(java.net.Socket socket)protected java.io.OutputStreamgetSocketOutputStream(java.net.Socket socket)intgetSocketTimeout()Returns the socket timeout value.protected voidincrementRequestCount()protected voidincrementResponseCount()booleanisOpen()Checks if this connection is open.booleanisStale()Checks whether this connection has gone down.protected HttpEntityprepareInput(HttpMessage message)protected java.io.OutputStreamprepareOutput(HttpMessage message)voidsetSocketTimeout(int timeout)Sets the socket timeout value.voidshutdown()Force-closes this connection.java.lang.StringtoString()
-
-
-
Field Detail
-
inBuffer
private final SessionInputBufferImpl inBuffer
-
outbuffer
private final SessionOutputBufferImpl outbuffer
-
messageConstraints
private final MessageConstraints messageConstraints
-
connMetrics
private final HttpConnectionMetricsImpl connMetrics
-
incomingContentStrategy
private final ContentLengthStrategy incomingContentStrategy
-
outgoingContentStrategy
private final ContentLengthStrategy outgoingContentStrategy
-
socketHolder
private final java.util.concurrent.atomic.AtomicReference<java.net.Socket> socketHolder
-
-
Constructor Detail
-
BHttpConnectionBase
protected BHttpConnectionBase(int bufferSize, int fragmentSizeHint, java.nio.charset.CharsetDecoder charDecoder, java.nio.charset.CharsetEncoder charEncoder, MessageConstraints messageConstraints, ContentLengthStrategy incomingContentStrategy, ContentLengthStrategy outgoingContentStrategy)Creates new instance of BHttpConnectionBase.- Parameters:
bufferSize- buffer size. Must be a positive number.fragmentSizeHint- fragment size hint.charDecoder- decoder to be used for decoding HTTP protocol elements. Ifnullsimple type cast will be used for byte to char conversion.charEncoder- encoder to be used for encoding HTTP protocol elements. Ifnullsimple type cast will be used for char to byte conversion.messageConstraints- Message constraints. IfnullMessageConstraints.DEFAULTwill be used.incomingContentStrategy- incoming content length strategy. IfnullLaxContentLengthStrategy.INSTANCEwill be used.outgoingContentStrategy- outgoing content length strategy. IfnullStrictContentLengthStrategy.INSTANCEwill be used.
-
-
Method Detail
-
ensureOpen
protected void ensureOpen() throws java.io.IOException- Throws:
java.io.IOException
-
getSocketInputStream
protected java.io.InputStream getSocketInputStream(java.net.Socket socket) throws java.io.IOException- Throws:
java.io.IOException
-
getSocketOutputStream
protected java.io.OutputStream getSocketOutputStream(java.net.Socket socket) throws java.io.IOException- Throws:
java.io.IOException
-
bind
protected void bind(java.net.Socket socket) throws java.io.IOExceptionBinds this connection to the givenSocket. This socket will be used by the connection to send and receive data.After this method's execution the connection status will be reported as open and the
isOpen()will returntrue.- Parameters:
socket- the socket.- Throws:
java.io.IOException- in case of an I/O error.
-
getSessionInputBuffer
protected SessionInputBuffer getSessionInputBuffer()
-
getSessionOutputBuffer
protected SessionOutputBuffer getSessionOutputBuffer()
-
doFlush
protected void doFlush() throws java.io.IOException- Throws:
java.io.IOException
-
isOpen
public boolean isOpen()
Description copied from interface:HttpConnectionChecks if this connection is open.- Specified by:
isOpenin interfaceHttpConnection- Returns:
- true if it is open, false if it is closed.
-
getSocket
protected java.net.Socket getSocket()
-
createOutputStream
protected java.io.OutputStream createOutputStream(long len, SessionOutputBuffer outbuffer)
-
prepareOutput
protected java.io.OutputStream prepareOutput(HttpMessage message) throws HttpException
- Throws:
HttpException
-
createInputStream
protected java.io.InputStream createInputStream(long len, SessionInputBuffer inBuffer)
-
prepareInput
protected HttpEntity prepareInput(HttpMessage message) throws HttpException
- Throws:
HttpException
-
getLocalAddress
public java.net.InetAddress getLocalAddress()
- Specified by:
getLocalAddressin interfaceHttpInetConnection
-
getLocalPort
public int getLocalPort()
- Specified by:
getLocalPortin interfaceHttpInetConnection
-
getRemoteAddress
public java.net.InetAddress getRemoteAddress()
- Specified by:
getRemoteAddressin interfaceHttpInetConnection
-
getRemotePort
public int getRemotePort()
- Specified by:
getRemotePortin interfaceHttpInetConnection
-
setSocketTimeout
public void setSocketTimeout(int timeout)
Description copied from interface:HttpConnectionSets the socket timeout value.- Specified by:
setSocketTimeoutin interfaceHttpConnection- Parameters:
timeout- timeout value in milliseconds
-
getSocketTimeout
public int getSocketTimeout()
Description copied from interface:HttpConnectionReturns the socket timeout value.- Specified by:
getSocketTimeoutin interfaceHttpConnection- Returns:
- positive value in milliseconds if a timeout is set,
0if timeout is disabled or-1if timeout is undefined.
-
shutdown
public void shutdown() throws java.io.IOExceptionDescription copied from interface:HttpConnectionForce-closes this connection. This is the only method of a connection which may be called from a different thread to terminate the connection. This method will not attempt to flush the transmitter's internal buffer prior to closing the underlying socket.- Specified by:
shutdownin interfaceHttpConnection- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionDescription copied from interface:HttpConnectionCloses this connection gracefully. This method will attempt to flush the internal output buffer prior to closing the underlying socket. This method MUST NOT be called from a different thread to force shutdown of the connection. Useshutdowninstead.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceHttpConnection- Throws:
java.io.IOException
-
fillInputBuffer
private int fillInputBuffer(int timeout) throws java.io.IOException- Throws:
java.io.IOException
-
awaitInput
protected boolean awaitInput(int timeout) throws java.io.IOException- Throws:
java.io.IOException
-
isStale
public boolean isStale()
Description copied from interface:HttpConnectionChecks whether this connection has gone down. Network connections may get closed during some time of inactivity for several reasons. The next time a read is attempted on such a connection it will throw an IOException. This method tries to alleviate this inconvenience by trying to find out if a connection is still usable. Implementations may do that by attempting a read with a very small timeout. Thus this method may block for a small amount of time before returning a result. It is therefore an expensive operation.- Specified by:
isStalein interfaceHttpConnection- Returns:
trueif attempts to use this connection are likely to succeed, orfalseif they are likely to fail and this connection should be closed
-
incrementRequestCount
protected void incrementRequestCount()
-
incrementResponseCount
protected void incrementResponseCount()
-
getMetrics
public HttpConnectionMetrics getMetrics()
Description copied from interface:HttpConnectionReturns a collection of connection metrics.- Specified by:
getMetricsin interfaceHttpConnection- Returns:
- HttpConnectionMetrics
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-