org.vertx.java.core.net

## Class NetSocket

- java.lang.Object
- - org.vertx.java.core.net.impl.ConnectionBase
  - - org.vertx.java.core.net.NetSocket

All Implemented Interfaces:
  ReadStream, WriteStream

Direct Known Subclasses:
  DefaultNetSocket

    public abstract class NetSocket
    extends ConnectionBase
    implements ReadStream, WriteStream

Represents a socket-like interface to a TCP/SSL connection on either the client or the server side.

Instances of this class are created on the client side by an `NetClient` when a connection to a server is made, or on the server side by a `NetServer` when a server accepts a connection.

It implements both `ReadStream` and `WriteStream` so it can be used with `Pump` to pump data with flow control.

Instances of this class are not thread-safe.

### Field Summary

Fields
`java.lang.String` `writeHandlerID` When a `NetSocket` is created it automatically registers an event handler with the event bus, the ID of that handler is given by `writeHandlerID`.

### Fields inherited from class org.vertx.java.core.net.impl.ConnectionBase

`channel, closedHandler, context, exceptionHandler, vertx`

### Constructor Summary

Constructors
`protected` `NetSocket(VertxInternal vertx, org.jboss.netty.channel.Channel channel, java.lang.String writeHandlerID, Context context)`

### Method Summary

Methods
`abstract void` `dataHandler(Handler<Buffer> dataHandler)` Set a data handler.
`abstract void` `drainHandler(Handler<java.lang.Void> drainHandler)` Set a drain handler on the stream.
`abstract void` `endHandler(Handler<java.lang.Void> endHandler)` Set an end handler.
`abstract void` `sendFile(java.lang.String filename)` Tell the kernel to stream a file as specified by `filename` directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system.
`abstract NetSocket` `write(Buffer data)` Write a `Buffer` to the request body.
`abstract NetSocket` `write(Buffer data, Handler<java.lang.Void> doneHandler)` Write a `Buffer` to the connection.
`abstract NetSocket` `write(java.lang.String str)` Write a `String` to the connection, encoded in UTF-8.
`abstract NetSocket` `write(java.lang.String str, Handler<java.lang.Void> doneHandler)` Write a `String` to the connection, encoded in UTF-8.
`abstract NetSocket` `write(java.lang.String str, java.lang.String enc)` Write a `String` to the connection, encoded using the encoding `enc`.
`abstract NetSocket` `write(java.lang.String str, java.lang.String enc, Handler<java.lang.Void> doneHandler)` Write a `String` to the connection, encoded with encoding `enc`.
`abstract void` `writeBuffer(Buffer data)` Write some data to the stream.

### Methods inherited from class org.vertx.java.core.net.impl.ConnectionBase

`addFuture, close, closedHandler, exceptionHandler, getContext, handleClosed, handleException, handleHandlerException, isSSL, pause, resume, sendFile, setContext, setWriteQueueMaxSize, writeQueueFull`

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

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

### Methods inherited from interface org.vertx.java.core.streams.ReadStream

`exceptionHandler, pause, resume`

### Methods inherited from interface org.vertx.java.core.streams.WriteStream

`exceptionHandler, setWriteQueueMaxSize, writeQueueFull`

### Field Detail

#### writeHandlerID

    public final java.lang.String writeHandlerID

When a `NetSocket` is created it automatically registers an event handler with the event bus, the ID of that handler is given by `writeHandlerID`.

Given this ID, a different event loop can send a buffer to that event handler using the event bus and that buffer will be received by this instance in its own event loop and written to the underlying connection. This allows you to write data to other connections which are owned by different event loops.

### Constructor Detail

#### NetSocket

    protected NetSocket(VertxInternal vertx,
             org.jboss.netty.channel.Channel channel,
             java.lang.String writeHandlerID,
             Context context)

### Method Detail

#### writeBuffer

    public abstract void writeBuffer(Buffer data)

Write some data to the stream. The data is put on an internal write queue, and the write actually happens asynchronously. To avoid running out of memory by putting too much on the write queue, check the `WriteStream.writeQueueFull()` method before writing. This is done automatically if using a `Pump`.

Specified by:
  `writeBuffer` in interface `WriteStream`

#### write

    public abstract NetSocket write(Buffer data)

Write a `Buffer` to the request body.

Returns:
  A reference to this, so multiple method calls can be chained.

#### write

    public abstract NetSocket write(java.lang.String str)

Write a `String` to the connection, encoded in UTF-8.

Returns:
  A reference to this, so multiple method calls can be chained.

#### write

    public abstract NetSocket write(java.lang.String str,
                  java.lang.String enc)

Write a `String` to the connection, encoded using the encoding `enc`.

Returns:
  A reference to this, so multiple method calls can be chained.

#### write

    public abstract NetSocket write(Buffer data,
                  Handler<java.lang.Void> doneHandler)

Write a `Buffer` to the connection. The `doneHandler` is called after the buffer is actually written to the wire.

Returns:
  A reference to this, so multiple method calls can be chained.

#### write

    public abstract NetSocket write(java.lang.String str,
                  Handler<java.lang.Void> doneHandler)

Write a `String` to the connection, encoded in UTF-8. The `doneHandler` is called after the buffer is actually written to the wire.

Returns:
  A reference to this, so multiple method calls can be chained.

#### write

    public abstract NetSocket write(java.lang.String str,
                  java.lang.String enc,
                  Handler<java.lang.Void> doneHandler)

Write a `String` to the connection, encoded with encoding `enc`. The `doneHandler` is called after the buffer is actually written to the wire.

Returns:
  A reference to this, so multiple method calls can be chained.

#### sendFile

    public abstract void sendFile(java.lang.String filename)

Tell the kernel to stream a file as specified by `filename` directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to stream files.

#### dataHandler

    public abstract void dataHandler(Handler<Buffer> dataHandler)

Set a data handler. As data is read, the handler will be called with the data.

Specified by:
  `dataHandler` in interface `ReadStream`

#### endHandler

    public abstract void endHandler(Handler<java.lang.Void> endHandler)

Set an end handler. Once the stream has ended, and there is no more data to be read, this handler will be called.

Specified by:
  `endHandler` in interface `ReadStream`

#### drainHandler

    public abstract void drainHandler(Handler<java.lang.Void> drainHandler)

Set a drain handler on the stream. If the write queue is full, then the handler will be called when the write queue has been reduced to maxSize / 2. See `Pump` for an example of this being used.

Specified by:
  `drainHandler` in interface `WriteStream`