You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/10/23 14:46:24 UTC
svn commit: r587483 -
/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/DatagramChannelImpl.java
Author: tellison
Date: Tue Oct 23 05:46:23 2007
New Revision: 587483
URL: http://svn.apache.org/viewvc?rev=587483&view=rev
Log:
Code tidy-up.
Modified:
harmony/enhanced/classlib/trunk/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/DatagramChannelImpl.java
Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/DatagramChannelImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/DatagramChannelImpl.java?rev=587483&r1=587482&r2=587483&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/DatagramChannelImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/DatagramChannelImpl.java Tue Oct 23 05:46:23 2007
@@ -46,15 +46,10 @@
/*
* The default implementation class of java.nio.channels.DatagramChannel.
- *
*/
class DatagramChannelImpl extends DatagramChannel implements
FileDescriptorHandler {
- // -------------------------------------------------------------------
- // Class variables
- // -------------------------------------------------------------------
-
// The singleton to do the native network operation.
private static final INetworkSystem networkSystem = Platform
.getNetworkSystem();
@@ -63,12 +58,8 @@
private static final int DEFAULT_TIMEOUT = 1;
private static final int ERRCODE_SOCKET_NONBLOCKING_WOULD_BLOCK = -211;
-
- private static final byte[] stubArray = new byte[0];
- // -------------------------------------------------------------------
- // Instance variables
- // -------------------------------------------------------------------
+ private static final byte[] stubArray = new byte[0];
// The fd to interact with native code
private FileDescriptor fd;
@@ -88,19 +79,17 @@
// whether the socket is bound
boolean isBound = false;
- private final Object readLock = new Object();
+ private static class ReadLock {}
+ private final Object readLock = new ReadLock();
- private final Object writeLock = new Object();
+ private static class WriteLock {}
+ private final Object writeLock = new WriteLock();
// used to store the trafficClass value which is simply returned
// as the value that was set. We also need it to pass it to methods
// that specify an address packets are going to be sent to
private int trafficClass = 0;
- // -------------------------------------------------------------------
- // Constructor
- // -------------------------------------------------------------------
-
/*
* Constructor
*/
@@ -115,16 +104,12 @@
* for native call
*/
@SuppressWarnings("unused")
- private DatagramChannelImpl(){
+ private DatagramChannelImpl() {
super(SelectorProvider.provider());
fd = new FileDescriptor();
connectAddress = new InetSocketAddress(0);
}
- // -------------------------------------------------------------------
- // Methods for getting internal DatagramSocket.
- // -------------------------------------------------------------------
-
/*
* Getting the internal DatagramSocket If we have not the socket, we create
* a new one.
@@ -150,12 +135,7 @@
.preferIPv6Addresses());
}
- // -------------------------------------------------------------------
- // Methods for connect and disconnect
- // -------------------------------------------------------------------
-
- /*
- *
+ /**
* @see java.nio.channels.DatagramChannel#isConnected()
*/
@Override
@@ -163,8 +143,7 @@
return connected;
}
- /*
- *
+ /**
* @see java.nio.channels.DatagramChannel#connect(java.net.SocketAddress)
*/
@Override
@@ -209,8 +188,7 @@
return this;
}
- /*
- *
+ /**
* @see java.nio.channels.DatagramChannel#disconnect()
*/
@Override
@@ -227,12 +205,7 @@
return this;
}
- // -------------------------------------------------------------------
- // Methods for send and receive
- // -------------------------------------------------------------------
-
- /*
- *
+ /**
* @see java.nio.channels.DatagramChannel#receive(java.nio.ByteBuffer)
*/
@Override
@@ -267,7 +240,7 @@
}
return retAddr;
}
-
+
private SocketAddress receiveImpl(ByteBuffer target, boolean loop)
throws IOException {
SocketAddress retAddr = null;
@@ -278,15 +251,16 @@
receivePacket = new DatagramPacket(target.array(), target
.position()
+ target.arrayOffset(), target.remaining());
- } else {
- receivePacket = new DatagramPacket(new byte[target.remaining()], target.remaining());
+ } else {
+ receivePacket = new DatagramPacket(new byte[target.remaining()],
+ target.remaining());
}
do {
if (isConnected()) {
- received = networkSystem.recvConnectedDatagram(fd, receivePacket,
- receivePacket.getData(), receivePacket.getOffset(),
- receivePacket.getLength(), isBlocking() ? 0
- : DEFAULT_TIMEOUT, false);
+ received = networkSystem.recvConnectedDatagram(fd,
+ receivePacket, receivePacket.getData(), receivePacket
+ .getOffset(), receivePacket.getLength(),
+ isBlocking() ? 0 : DEFAULT_TIMEOUT, false);
} else {
received = networkSystem.receiveDatagram(fd, receivePacket,
receivePacket.getData(), receivePacket.getOffset(),
@@ -306,7 +280,7 @@
}
}
if (null != receivePacket && null != receivePacket.getAddress()) {
-
+
if (received > 0) {
if (target.hasArray()) {
target.position(oldposition + received);
@@ -321,25 +295,24 @@
} while (loop);
return retAddr;
}
-
- private SocketAddress receiveDirectImpl(ByteBuffer target, boolean loop) throws IOException
- {
- SocketAddress retAddr = null;
- DatagramPacket receivePacket = new DatagramPacket(
- stubArray, 0);
+
+ private SocketAddress receiveDirectImpl(ByteBuffer target, boolean loop)
+ throws IOException {
+ SocketAddress retAddr = null;
+ DatagramPacket receivePacket = new DatagramPacket(stubArray, 0);
int oldposition = target.position();
int received = 0;
do {
long address = AddressUtil.getDirectBufferAddress(target);
if (isConnected()) {
- received = networkSystem.recvConnectedDatagramDirect(fd, receivePacket,
- address, target.position(),
- target.remaining(), isBlocking() ? 0
+ received = networkSystem.recvConnectedDatagramDirect(fd,
+ receivePacket, address, target.position(), target
+ .remaining(), isBlocking() ? 0
: DEFAULT_TIMEOUT, false);
} else {
- received = networkSystem.receiveDatagramDirect(fd, receivePacket,
- address, target.position(),
- target.remaining(), isBlocking() ? 0
+ received = networkSystem.receiveDatagramDirect(fd,
+ receivePacket, address, target.position(), target
+ .remaining(), isBlocking() ? 0
: DEFAULT_TIMEOUT, false);
}
@@ -347,15 +320,14 @@
SecurityManager sm = System.getSecurityManager();
if (!isConnected() && null != sm) {
try {
- sm.checkAccept(receivePacket.getAddress()
- .getHostAddress(), receivePacket.getPort());
+ sm.checkAccept(receivePacket.getAddress().getHostAddress(),
+ receivePacket.getPort());
} catch (SecurityException e) {
// do discard the datagram packet
receivePacket = null;
}
}
- if (null != receivePacket
- && null != receivePacket.getAddress()) {
+ if (null != receivePacket && null != receivePacket.getAddress()) {
// copy the data of received packet
if (received > 0) {
target.position(oldposition + received);
@@ -367,7 +339,7 @@
return retAddr;
}
- /*
+ /**
* @see java.nio.channels.DatagramChannel#send(java.nio.ByteBuffer,
* java.net.SocketAddress)
*/
@@ -440,12 +412,7 @@
}
}
- // -------------------------------------------------------------------
- // Methods for read and write.
- // -------------------------------------------------------------------
-
- /*
- *
+ /**
* @see java.nio.channels.DatagramChannel#read(java.nio.ByteBuffer)
*/
@Override
@@ -462,10 +429,10 @@
return 0;
}
- int readCount = 0;
+ int readCount = 0;
if (target.isDirect() || target.hasArray()) {
readCount = readImpl(target);
- if(readCount > 0){
+ if (readCount > 0) {
target.position(target.position() + readCount);
}
@@ -473,15 +440,14 @@
byte[] readArray = new byte[target.remaining()];
ByteBuffer readBuffer = ByteBuffer.wrap(readArray);
readCount = readImpl(readBuffer);
- if(readCount > 0){
+ if (readCount > 0) {
target.put(readArray, 0, readCount);
}
}
return readCount;
}
- /*
- *
+ /**
* @see java.nio.channels.DatagramChannel#read(java.nio.ByteBuffer[], int,
* int)
*/
@@ -525,7 +491,7 @@
* read from channel, and store the result in the target.
*/
private int readImpl(ByteBuffer readBuffer) throws IOException {
- synchronized(readLock){
+ synchronized (readLock) {
int readCount = 0;
try {
begin();
@@ -535,10 +501,12 @@
int start = readBuffer.position();
int length = readBuffer.remaining();
if (readBuffer.isDirect()) {
- long address = AddressUtil.getDirectBufferAddress(readBuffer);
+ long address = AddressUtil
+ .getDirectBufferAddress(readBuffer);
if (isConnected()) {
- readCount = networkSystem.recvConnectedDatagramDirect(fd,
- null, address, start, length, timeout, false);
+ readCount = networkSystem.recvConnectedDatagramDirect(
+ fd, null, address, start, length, timeout,
+ false);
} else {
readCount = networkSystem.receiveDatagramDirect(fd,
null, address, start, length, timeout, false);
@@ -548,11 +516,11 @@
byte[] target = readBuffer.array();
start += readBuffer.arrayOffset();
if (isConnected()) {
- readCount = networkSystem.recvConnectedDatagram(fd, null,
- target, start, length, timeout, false);
+ readCount = networkSystem.recvConnectedDatagram(fd,
+ null, target, start, length, timeout, false);
} else {
- readCount = networkSystem.receiveDatagram(fd, null, target,
- start, length, timeout, false);
+ readCount = networkSystem.receiveDatagram(fd, null,
+ target, start, length, timeout, false);
}
}
return readCount;
@@ -565,7 +533,7 @@
}
}
- /*
+ /**
* @see java.nio.channels.DatagramChannel#write(java.nio.ByteBuffer)
*/
@Override
@@ -597,7 +565,7 @@
return result;
}
- /*
+ /**
* @see java.nio.channels.DatagramChannel#write(java.nio.ByteBuffer[], int,
* int)
*/
@@ -616,7 +584,7 @@
return 0;
}
ByteBuffer writeBuf = ByteBuffer.allocate(count);
- for (int val = offset; val < length+offset; val++) {
+ for (int val = offset; val < length + offset; val++) {
ByteBuffer source = sources[val];
int oldPosition = source.position();
writeBuf.put(source);
@@ -637,25 +605,25 @@
}
/*
- * write the source. return the count of bytes written.
+ * Write the source. Return the count of bytes written.
*/
private int writeImpl(ByteBuffer buf) throws IOException {
- synchronized(writeLock){
+ synchronized (writeLock) {
int result = 0;
try {
begin();
int length = buf.remaining();
int start = buf.position();
-
+
if (buf.isDirect()) {
long address = AddressUtil.getDirectBufferAddress(buf);
- result = networkSystem.sendConnectedDatagramDirect(fd, address,
- start, length, isBound);
+ result = networkSystem.sendConnectedDatagramDirect(fd,
+ address, start, length, isBound);
} else {
// buf is assured to have array.
start += buf.arrayOffset();
- result = networkSystem.sendConnectedDatagram(fd, buf.array(),
- start, length, isBound);
+ result = networkSystem.sendConnectedDatagram(fd, buf
+ .array(), start, length, isBound);
}
return result;
} catch (SocketException e) {
@@ -672,12 +640,8 @@
}
}
- // -------------------------------------------------------------------
- // Protected Inherited methods
- // -------------------------------------------------------------------
-
/*
- * do really closing action here
+ * Do really closing action here.
*/
@Override
synchronized protected void implCloseSelectableChannel() throws IOException {
@@ -689,8 +653,7 @@
}
}
- /*
- *
+ /**
* @see java.nio.channels.spi.AbstractSelectableChannel#implConfigureBlocking(boolean)
*/
@Override
@@ -702,12 +665,8 @@
// decided by isBlocking() method.
}
- // -------------------------------------------------------------------
- // Share methods for checking.
- // -------------------------------------------------------------------
-
/*
- * status check, must be open.
+ * Status check, must be open.
*/
private void checkOpen() throws IOException {
if (!isOpen()) {
@@ -716,7 +675,7 @@
}
/*
- * status check, must be open and connected, for read and write.
+ * Status check, must be open and connected, for read and write.
*/
private void checkOpenConnected() throws IOException {
checkOpen();
@@ -726,7 +685,7 @@
}
/*
- * buffer check, must not null
+ * Buffer check, must not null
*/
private void checkNotNull(ByteBuffer source) {
if (null == source) {
@@ -735,7 +694,7 @@
}
/*
- * buffer check, must not null and not read only buffer, for read and
+ * Buffer check, must not null and not read only buffer, for read and
* receive.
*/
private void checkWritable(ByteBuffer target) {
@@ -745,12 +704,8 @@
}
}
- // -------------------------------------------------------------------
- // Adapter classes for internal socket.
- // -------------------------------------------------------------------
-
/*
- * get the fd for internal use.
+ * Get the fd for internal use.
*/
public FileDescriptor getFD() {
return fd;
@@ -776,7 +731,7 @@
private DatagramChannelImpl channelImpl;
/*
- * init the datagramSocketImpl and datagramChannelImpl
+ * Constructor initialize the datagramSocketImpl and datagramChannelImpl
*/
DatagramSocketAdapter(DatagramSocketImpl socketimpl,
DatagramChannelImpl channelImpl) {
@@ -785,14 +740,14 @@
}
/*
- * get the internal datagramChannelImpl
+ * Get the internal datagramChannelImpl
*/
@Override
public DatagramChannel getChannel() {
return channelImpl;
}
- /*
+ /**
* @see java.net.DatagramSocket#isBound()
*/
@Override
@@ -800,7 +755,7 @@
return channelImpl.isBound;
}
- /*
+ /**
* @see java.net.DatagramSocket#isConnected()
*/
@Override
@@ -808,7 +763,7 @@
return channelImpl.isConnected();
}
- /*
+ /**
* @see java.net.DatagramSocket#getInetAddress()
*/
@Override
@@ -819,7 +774,7 @@
return channelImpl.connectAddress.getAddress();
}
- /*
+ /**
* @see java.net.DatagramSocket#getLocalAddress()
*/
@Override
@@ -827,7 +782,7 @@
return channelImpl.getLocalAddress();
}
- /*
+ /**
* @see java.net.DatagramSocket#getPort()
*/
@Override
@@ -838,7 +793,7 @@
return channelImpl.connectAddress.getPort();
}
- /*
+ /**
* @see java.net.DatagramSocket#bind(java.net.SocketAddress)
*/
@Override
@@ -850,7 +805,7 @@
channelImpl.isBound = true;
}
- /*
+ /**
* @see java.net.DatagramSocket#receive(java.net.DatagramPacket)
*/
@Override
@@ -861,7 +816,7 @@
super.receive(packet);
}
- /*
+ /**
* @see java.net.DatagramSocket#send(java.net.DatagramPacket)
*/
@Override
@@ -872,7 +827,7 @@
super.send(packet);
}
- /*
+ /**
* @see java.net.DatagramSocket#close()
*/
@Override
@@ -889,7 +844,7 @@
}
}
- /*
+ /**
* @see java.net.DatagramSocket#disconnect()
*/
@Override