Methods Summary |
---|
private int | calculateByteBufferArray(java.nio.ByteBuffer[] sources, int offset, int length)
int sum = 0;
for (int val = offset; val < offset + length; val++) {
sum = sum + sources[val].remaining();
}
return sum;
|
private synchronized void | checkOpenConnected()
if (!isOpen()) {
throw new ClosedChannelException();
}
if (!isConnected()) {
throw new NotYetConnectedException();
}
|
private synchronized void | checkUnconnected()
if (!isOpen()) {
throw new ClosedChannelException();
}
if (status == SOCKET_STATUS_CONNECTED) {
throw new AlreadyConnectedException();
}
if (status == SOCKET_STATUS_PENDING) {
throw new ConnectionPendingException();
}
|
public boolean | connect(java.net.SocketAddress socketAddress)
// status must be open and unconnected
checkUnconnected();
// check the address
InetSocketAddress inetSocketAddress = validateAddress(socketAddress);
int port = inetSocketAddress.getPort();
String hostName = inetSocketAddress.getAddress().getHostName();
// security check
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkConnect(hostName, port);
}
// connect result
int result = EOF;
boolean finished = false;
try {
if (!isBound) {
// bind
networkSystem.bind2(fd, 0, true, InetAddress
.getByAddress(new byte[] { 0, 0, 0, 0 }));
isBound = true;
}
if (isBlocking()) {
begin();
result = networkSystem.connect(fd, trafficClass,
inetSocketAddress.getAddress(), inetSocketAddress
.getPort());
} else {
result = networkSystem.connectWithTimeout(fd, 0, trafficClass,
inetSocketAddress.getAddress(), inetSocketAddress
.getPort(), HY_SOCK_STEP_START, connectContext);
// set back to nonblocking to work around with a bug in portlib
if (!this.isBlocking()) {
networkSystem.setNonBlocking(fd, true);
}
}
finished = (CONNECT_SUCCESS == result);
isBound = finished;
} catch (IOException e) {
if (e instanceof ConnectException && !isBlocking()) {
status = SOCKET_STATUS_PENDING;
} else {
if (isOpen()) {
close();
finished = true;
}
throw e;
}
} finally {
if (isBlocking()) {
end(finished);
}
}
// set local port
localPort = networkSystem.getSocketLocalPort(fd, false);
localAddress = networkSystem.getSocketLocalAddress(fd, false);
// set the connected address.
connectAddress = inetSocketAddress;
synchronized (this) {
if (isBlocking()) {
status = (finished ? SOCKET_STATUS_CONNECTED
: SOCKET_STATUS_UNCONNECTED);
} else {
status = SOCKET_STATUS_PENDING;
}
}
return finished;
|
public boolean | finishConnect()
// status check
synchronized (this) {
if (!isOpen()) {
throw new ClosedChannelException();
}
if (status == SOCKET_STATUS_CONNECTED) {
return true;
}
if (status != SOCKET_STATUS_PENDING) {
throw new NoConnectionPendingException();
}
}
// finish result
int result = EOF;
boolean finished = false;
try {
begin();
result = networkSystem.connectWithTimeout(fd,
isBlocking() ? -1 : 0, trafficClass, connectAddress
.getAddress(), connectAddress.getPort(),
HY_PORT_SOCKET_STEP_CHECK, connectContext);
finished = (result == CONNECT_SUCCESS);
isBound = finished;
localAddress = networkSystem.getSocketLocalAddress(fd, false);
} catch (ConnectException e) {
if (isOpen()) {
close();
finished = true;
}
throw e;
} finally {
end(finished);
}
synchronized (this) {
status = (finished ? SOCKET_STATUS_CONNECTED : status);
isBound = finished;
}
return finished;
|
public java.io.FileDescriptor | getFD()
return fd;
|
public java.net.InetAddress | getLocalAddress()
byte[] any_bytes = { 0, 0, 0, 0 };
if (!isBound) {
return InetAddress.getByAddress(any_bytes);
}
return localAddress;
|
protected synchronized void | implCloseSelectableChannel()
if (SOCKET_STATUS_CLOSED != status) {
status = SOCKET_STATUS_CLOSED;
if (null != socket && !socket.isClosed()) {
socket.close();
} else {
networkSystem.socketClose(fd);
}
}
|
protected void | implConfigureBlocking(boolean blockMode)
synchronized (blockingLock()) {
networkSystem.setNonBlocking(fd, !blockMode);
}
|
public synchronized boolean | isConnected()
return status == SOCKET_STATUS_CONNECTED;
|
public synchronized boolean | isConnectionPending()
return status == SOCKET_STATUS_PENDING;
|
private boolean | isIndexValid(java.nio.ByteBuffer[] targets, int offset, int length)
return (length >= 0) && (offset >= 0)
&& ((long)length + (long)offset <= targets.length);
|
public int | read(java.nio.ByteBuffer target)
if (null == target) {
throw new NullPointerException();
}
checkOpenConnected();
if (!target.hasRemaining()) {
return 0;
}
int readCount;
if (target.isDirect() || target.hasArray()) {
readCount = readImpl(target);
if (readCount > 0) {
target.position(target.position() + readCount);
}
} else {
ByteBuffer readBuffer = null;
byte[] readArray = null;
readArray = new byte[target.remaining()];
readBuffer = ByteBuffer.wrap(readArray);
readCount = readImpl(readBuffer);
if (readCount > 0) {
target.put(readArray, 0, readCount);
}
}
return readCount;
|
public long | read(java.nio.ByteBuffer[] targets, int offset, int length)
if (!isIndexValid(targets, offset, length)) {
throw new IndexOutOfBoundsException();
}
checkOpenConnected();
int totalCount = calculateByteBufferArray(targets, offset, length);
if (0 == totalCount) {
return 0;
}
byte[] readArray = new byte[totalCount];
ByteBuffer readBuffer = ByteBuffer.wrap(readArray);
int readCount;
// read data to readBuffer, and then transfer data from readBuffer to
// targets.
readCount = readImpl(readBuffer);
if (readCount > 0) {
int left = readCount;
int index = offset;
// transfer data from readArray to targets
while (left > 0) {
int putLength = Math.min(targets[index].remaining(), left);
targets[index].put(readArray, readCount - left, putLength);
index++;
left -= putLength;
}
}
return readCount;
|
private int | readImpl(java.nio.ByteBuffer target)
synchronized(readLock){
int readCount = 0;
try {
if (isBlocking()) {
begin();
}
int offset = target.position();
int length = target.remaining();
if (target.isDirect()) {
int address = AddressUtil.getDirectBufferAddress(target);
readCount = networkSystem.readDirect(fd, address, offset,
length, (isBlocking() ? TIMEOUT_BLOCK
: TIMEOUT_NONBLOCK));
} else {
// target is assured to have array.
byte[] array = target.array();
offset += target.arrayOffset();
readCount = networkSystem.read(fd, array, offset, length,
(isBlocking() ? TIMEOUT_BLOCK : TIMEOUT_NONBLOCK));
}
return readCount;
} finally {
if (isBlocking()) {
end(readCount > 0);
}
}
}
|
void | setBound(boolean flag)
isBound = flag;
|
synchronized void | setConnected()
status = SOCKET_STATUS_CONNECTED;
|
public synchronized java.net.Socket | socket()
if (null == socket) {
try {
InetAddress addr = null;
int port = 0;
if (connectAddress != null) {
addr = connectAddress.getAddress();
port = connectAddress.getPort();
}
socket = new SocketAdapter(SocketImplProvider.getSocketImpl(fd,
localPort, addr, port), this);
} catch (SocketException e) {
return null;
}
}
return socket;
|
static java.net.InetSocketAddress | validateAddress(java.net.SocketAddress socketAddress)
if (null == socketAddress) {
throw new IllegalArgumentException();
}
if (!(socketAddress instanceof InetSocketAddress)) {
throw new UnsupportedAddressTypeException();
}
InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
if (inetSocketAddress.isUnresolved()) {
throw new UnresolvedAddressException();
}
return inetSocketAddress;
|
public int | write(java.nio.ByteBuffer source)
if (null == source) {
throw new NullPointerException();
}
checkOpenConnected();
if (!source.hasRemaining()) {
return 0;
}
return writeImpl(source);
|
public long | write(java.nio.ByteBuffer[] sources, int offset, int length)
if (!isIndexValid(sources, offset, length)) {
throw new IndexOutOfBoundsException();
}
checkOpenConnected();
int count = calculateByteBufferArray(sources, offset, length);
if (0 == count) {
return 0;
}
ByteBuffer writeBuf = ByteBuffer.allocate(count);
for (int val = offset; val < length+offset; val++) {
ByteBuffer source = sources[val];
int oldPosition = source.position();
writeBuf.put(source);
source.position(oldPosition);
}
writeBuf.flip();
int result = writeImpl(writeBuf);
int val = offset;
int written = result;
while (result > 0) {
ByteBuffer source = sources[val];
int gap = Math.min(result, source.remaining());
source.position(source.position() + gap);
val++;
result -= gap;
}
return written;
|
private int | writeImpl(java.nio.ByteBuffer source)
synchronized(writeLock){
if (!source.hasRemaining()) {
return 0;
}
int writeCount = 0;
try {
int pos = source.position();
int length = source.remaining();
if (isBlocking()) {
begin();
}
if (source.isDirect()) {
int address = AddressUtil.getDirectBufferAddress(source);
writeCount = networkSystem
.writeDirect(fd, address, pos, length);
} else if (source.hasArray()) {
pos += source.arrayOffset();
writeCount = networkSystem.write(fd, source.array(), pos,
length);
} else {
byte[] array = new byte[length];
source.get(array);
writeCount = networkSystem.write(fd, array, 0, length);
}
source.position(pos + writeCount);
} catch (SocketException e) {
if (e.getCause() instanceof ErrorCodeException) {
if (ERRCODE_SOCKET_NONBLOCKING_WOULD_BLOCK == ((ErrorCodeException) e
.getCause()).getErrorCode()) {
return writeCount;
}
}
throw e;
} finally {
if (isBlocking()) {
end(writeCount >= 0);
}
}
return writeCount;
}
|