org.apache.http.impl

## Class BHttpConnectionBase

- java.lang.Object
- - org.apache.http.impl.BHttpConnectionBase

All Implemented Interfaces:
  Closeable, AutoCloseable, HttpConnection, HttpInetConnection

Direct Known Subclasses:
  DefaultBHttpClientConnection, DefaultBHttpServerConnection

    @NotThreadSafe
    public class BHttpConnectionBase
    extends Object
    implements HttpConnection, HttpInetConnection

This class serves as a base for all `HttpConnection` implementations and provides functionality common to both client and server HTTP connections.

Since:
  4.0

### Constructor Summary

Constructors
`protected` `BHttpConnectionBase(int buffersize, int fragmentSizeHint, CharsetDecoder chardecoder, CharsetEncoder charencoder, MessageConstraints constraints, ContentLengthStrategy incomingContentStrategy, ContentLengthStrategy outgoingContentStrategy)` Creates new instance of BHttpConnectionBase.

### Method Summary

Methods
`protected boolean` `awaitInput(int timeout)`
`protected void` `bind(Socket socket)` Binds this connection to the given `Socket`.
`void` `close()` Closes this connection gracefully.
`protected InputStream` `createInputStream(long len, SessionInputBuffer inbuffer)`
`protected OutputStream` `createOutputStream(long len, SessionOutputBuffer outbuffer)`
`protected void` `doFlush()`
`protected void` `ensureOpen()`
`InetAddress` `getLocalAddress()`
`int` `getLocalPort()`
`HttpConnectionMetrics` `getMetrics()` Returns a collection of connection metrics.
`InetAddress` `getRemoteAddress()`
`int` `getRemotePort()`
`protected SessionInputBuffer` `getSessionInputBuffer()`
`protected SessionOutputBuffer` `getSessionOutputBuffer()`
`protected Socket` `getSocket()`
`protected InputStream` `getSocketInputStream(Socket socket)`
`protected OutputStream` `getSocketOutputStream(Socket socket)`
`int` `getSocketTimeout()` Returns the socket timeout value.
`protected void` `incrementRequestCount()`
`protected void` `incrementResponseCount()`
`boolean` `isOpen()` Checks if this connection is open.
`boolean` `isStale()` Checks whether this connection has gone down.
`protected HttpEntity` `prepareInput(HttpMessage message)`
`protected OutputStream` `prepareOutput(HttpMessage message)`
`void` `setSocketTimeout(int timeout)` Sets the socket timeout value.
`void` `shutdown()` Force-closes this connection.
`String` `toString()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### BHttpConnectionBase

    protected BHttpConnectionBase(int buffersize,
                       int fragmentSizeHint,
                       CharsetDecoder chardecoder,
                       CharsetEncoder charencoder,
                       MessageConstraints constraints,
                       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. If `null` simple type cast will be used for byte to char conversion.
  `charencoder` - encoder to be used for encoding HTTP protocol elements. If `null` simple type cast will be used for char to byte conversion.
  `constraints` - Message constraints. If `null` `MessageConstraints.DEFAULT` will be used.
  `incomingContentStrategy` - incoming content length strategy. If `null` `LaxContentLengthStrategy.INSTANCE` will be used.
  `outgoingContentStrategy` - outgoing content length strategy. If `null` `StrictContentLengthStrategy.INSTANCE` will be used.

### Method Detail

#### ensureOpen

    protected void ensureOpen()
                       throws IOException

Throws:
  `IOException`

#### getSocketInputStream

    protected InputStream getSocketInputStream(Socket socket)
                                        throws IOException

Throws:
  `IOException`

#### getSocketOutputStream

    protected OutputStream getSocketOutputStream(Socket socket)
                                          throws IOException

Throws:
  `IOException`

#### bind

    protected void bind(Socket socket)
                 throws IOException

Binds this connection to the given `Socket`. 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 return `true`.

Parameters:
  `socket` - the socket.
Throws:
  `IOException` - in case of an I/O error.

#### getSessionInputBuffer

    protected SessionInputBuffer getSessionInputBuffer()

#### getSessionOutputBuffer

    protected SessionOutputBuffer getSessionOutputBuffer()

#### doFlush

    protected void doFlush()
                    throws IOException

Throws:
  `IOException`

#### isOpen

    public boolean isOpen()

Description copied from interface: `HttpConnection`
Checks if this connection is open.

Specified by:
  `isOpen` in interface `HttpConnection`
Returns:
  true if it is open, false if it is closed.

#### getSocket

    protected Socket getSocket()

#### createOutputStream

    protected OutputStream createOutputStream(long len,
                                  SessionOutputBuffer outbuffer)

#### prepareOutput

    protected OutputStream prepareOutput(HttpMessage message)
                                  throws HttpException

Throws:
  `HttpException`

#### createInputStream

    protected InputStream createInputStream(long len,
                                SessionInputBuffer inbuffer)

#### prepareInput

    protected HttpEntity prepareInput(HttpMessage message)
                               throws HttpException

Throws:
  `HttpException`

#### getLocalAddress

    public InetAddress getLocalAddress()

Specified by:
  `getLocalAddress` in interface `HttpInetConnection`

#### getLocalPort

    public int getLocalPort()

Specified by:
  `getLocalPort` in interface `HttpInetConnection`

#### getRemoteAddress

    public InetAddress getRemoteAddress()

Specified by:
  `getRemoteAddress` in interface `HttpInetConnection`

#### getRemotePort

    public int getRemotePort()

Specified by:
  `getRemotePort` in interface `HttpInetConnection`

#### setSocketTimeout

    public void setSocketTimeout(int timeout)

Description copied from interface: `HttpConnection`
Sets the socket timeout value.

Specified by:
  `setSocketTimeout` in interface `HttpConnection`
Parameters:
  `timeout` - timeout value in milliseconds

#### getSocketTimeout

    public int getSocketTimeout()

Description copied from interface: `HttpConnection`
Returns the socket timeout value.

Specified by:
  `getSocketTimeout` in interface `HttpConnection`
Returns:
  positive value in milliseconds if a timeout is set, `0` if timeout is disabled or `-1` if timeout is undefined.

#### shutdown

    public void shutdown()
                  throws IOException

Description copied from interface: `HttpConnection`
Force-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:
  `shutdown` in interface `HttpConnection`
Throws:
  `IOException`

#### close

    public void close()
               throws IOException

Description copied from interface: `HttpConnection`
Closes 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. Use `shutdown` instead.

Specified by:
  `close` in interface `Closeable`
Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `HttpConnection`
Throws:
  `IOException`

#### awaitInput

    protected boolean awaitInput(int timeout)
                          throws IOException

Throws:
  `IOException`

#### isStale

    public boolean isStale()

Description copied from interface: `HttpConnection`
Checks 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:
  `isStale` in interface `HttpConnection`
Returns:
  `true` if attempts to use this connection are likely to succeed, or `false` if 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: `HttpConnection`
Returns a collection of connection metrics.

Specified by:
  `getMetrics` in interface `HttpConnection`
Returns:
  HttpConnectionMetrics

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`