Package org.jboss.logmanager.handlers

## Class TcpOutputStream

- java.lang.Object
- - java.io.OutputStream
  - - org.jboss.logmanager.handlers.TcpOutputStream

All Implemented Interfaces:
  `java.io.Closeable`, `java.io.Flushable`, `java.lang.AutoCloseable`, `FlushableCloseable`

Direct Known Subclasses:
  `SslTcpOutputStream`

    public class TcpOutputStream
    extends java.io.OutputStream
    implements FlushableCloseable

An output stream that writes data to a `socket`.

If an `IOException` occurs during a `write(byte[], int, int)` and a `socket factory` was defined the stream will attempt to reconnect indefinitely. By default additional writes are discarded when reconnecting. If you set the `block on reconnect` to `true`, then the reconnect will indefinitely block until the TCP stream is reconnected.

You can optionally get a collection of the errors that occurred during a write or reconnect.

Author:
  James R. Perkins

### Field Summary

Fields
`protected java.lang.Object` `outputLock`

### Constructor Summary

Constructors
`` `TcpOutputStream​(java.net.InetAddress address, int port)` Creates a TCP output stream.
`` `TcpOutputStream​(java.net.InetAddress address, int port, boolean blockOnReconnect)` Creates a TCP output stream.
`protected` `TcpOutputStream​(java.net.Socket socket)` Deprecated. Use `TcpOutputStream(javax.net.SocketFactory, java.net.InetAddress, int)`
`protected` `TcpOutputStream​(javax.net.SocketFactory socketFactory, java.net.InetAddress address, int port)` Creates a new TCP output stream.
`protected` `TcpOutputStream​(javax.net.SocketFactory socketFactory, java.net.InetAddress address, int port, boolean blockOnReconnect)` Creates a new TCP output stream.

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `close()`
`void` `flush()`
`java.util.Collection<java.lang.Exception>` `getErrors()` Retrieves the errors occurred, if any, during a write or reconnect.
`boolean` `isBlockOnReconnect()` Indicates whether or not the output stream is set to block when attempting to reconnect a TCP connection.
`boolean` `isConnected()` Returns the connected state of the TCP stream.
`void` `setBlockOnReconnect​(boolean blockOnReconnect)` Enables or disables blocking when attempting to reconnect the socket.
`void` `write​(byte[] b)`
`void` `write​(byte[] b, int off, int len)`
`void` `write​(int b)`

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

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

### Field Detail

#### outputLock

    protected final java.lang.Object outputLock

### Constructor Detail

#### TcpOutputStream

    public TcpOutputStream​(java.net.InetAddress address,
                           int port)
                    throws java.io.IOException

Creates a TCP output stream.

Uses the `default socket factory` to create the socket.

Parameters:
  `address` - the address to connect to
  `port` - the port to connect to
Throws:
  `java.io.IOException` - no longer throws an exception. If an exception occurs while attempting to connect the socket a reconnect will be attempted on the next write.

#### TcpOutputStream

    public TcpOutputStream​(java.net.InetAddress address,
                           int port,
                           boolean blockOnReconnect)
                    throws java.io.IOException

Creates a TCP output stream.

Uses the `default socket factory` to create the socket.

Parameters:
  `address` - the address to connect to
  `port` - the port to connect to
  `blockOnReconnect` - `true` to block when attempting to reconnect the socket or `false` to reconnect asynchronously
Throws:
  `java.io.IOException` - no longer throws an exception. If an exception occurs while attempting to connect the socket a reconnect will be attempted on the next write.

#### TcpOutputStream

    @Deprecated
    protected TcpOutputStream​(java.net.Socket socket)

Deprecated.
Use `TcpOutputStream(javax.net.SocketFactory, java.net.InetAddress, int)`
Creates a new TCP output stream.

Using this constructor does not allow for any kind of reconnect if the connection is dropped.

Parameters:
  `socket` - the socket used to write the output to

#### TcpOutputStream

    protected TcpOutputStream​(javax.net.SocketFactory socketFactory,
                              java.net.InetAddress address,
                              int port)
                       throws java.io.IOException

Creates a new TCP output stream.

Creates a `socket` from the `socketFactory` argument.

Parameters:
  `socketFactory` - the factory used to create the socket
  `address` - the address to connect to
  `port` - the port to connect to
Throws:
  `java.io.IOException` - no longer throws an exception. If an exception occurs while attempting to connect the socket a reconnect will be attempted on the next write.

#### TcpOutputStream

    protected TcpOutputStream​(javax.net.SocketFactory socketFactory,
                              java.net.InetAddress address,
                              int port,
                              boolean blockOnReconnect)
                       throws java.io.IOException

Creates a new TCP output stream.

Creates a `socket` from the `socketFactory` argument.

Parameters:
  `socketFactory` - the factory used to create the socket
  `address` - the address to connect to
  `port` - the port to connect to
  `blockOnReconnect` - `true` to block when attempting to reconnect the socket or `false` to reconnect asynchronously
Throws:
  `java.io.IOException` - no longer throws an exception. If an exception occurs while attempting to connect the socket a reconnect will be attempted on the next write.

### Method Detail

#### write

    public void write​(int b)
               throws java.io.IOException

Specified by:
  `write` in class `java.io.OutputStream`
Throws:
  `java.io.IOException`

#### write

    public void write​(byte[] b)
               throws java.io.IOException

Overrides:
  `write` in class `java.io.OutputStream`
Throws:
  `java.io.IOException`

#### write

    public void write​(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException

Overrides:
  `write` in class `java.io.OutputStream`
Throws:
  `java.io.IOException`

#### flush

    public void flush()
               throws java.io.IOException

Specified by:
  `flush` in interface `java.io.Flushable`
Overrides:
  `flush` in class `java.io.OutputStream`
Throws:
  `java.io.IOException`

#### close

    public void close()
               throws java.io.IOException

Specified by:
  `close` in interface `java.lang.AutoCloseable`
Specified by:
  `close` in interface `java.io.Closeable`
Overrides:
  `close` in class `java.io.OutputStream`
Throws:
  `java.io.IOException`

#### isBlockOnReconnect

    public boolean isBlockOnReconnect()

Indicates whether or not the output stream is set to block when attempting to reconnect a TCP connection.

Returns:
  `true` if blocking is enabled, otherwise `false`

#### setBlockOnReconnect

    public void setBlockOnReconnect​(boolean blockOnReconnect)

Enables or disables blocking when attempting to reconnect the socket.

If set to `true` the `write` methods will block when attempting to reconnect. This is only advisable to be set to `true` if using an asynchronous handler.

Parameters:
  `blockOnReconnect` - `true` to block when reconnecting or `false` to reconnect asynchronously discarding any new messages coming in

#### isConnected

    public boolean isConnected()

Returns the connected state of the TCP stream.

The stream is said to be disconnected when an `IOException` occurs during a write. Otherwise a stream is considered connected.

Returns:
  `true` if the stream is connected, otherwise `false`

#### getErrors

    public java.util.Collection<java.lang.Exception> getErrors()

Retrieves the errors occurred, if any, during a write or reconnect.

Returns:
  a collection of errors or an empty list