提交 e65635d8 编写于 作者: L lei.yul 提交者: yulei

[Wisp] Refactoring WispSocket iostream creation

Summary:
Refactoring WispSocket's InputStream and OutputStream
creating code for readability.

Test Plan: all wisp tests

Reviewed-by: joeyleeeeeee97 shiyuexw

Issue: alibaba/dragonwell8#124
上级 8f74c702
......@@ -171,20 +171,12 @@ public class WispSocketImpl
}
}
public InputStream getInputStream() throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
if (!isConnected())
throw new SocketException("Socket is not connected");
if (isInputShutdown())
throw new SocketException("Socket input is shutdown");
if (socketInputStream == null) {
try {
socketInputStream = AccessController.doPrivileged(
new PrivilegedExceptionAction<InputStream>() {
public InputStream run() throws IOException {
return new InputStream() {
protected final SocketChannel ch = getChannelImpl();
private class WispSocketInputStream extends InputStream {
WispSocketInputStream(SocketChannel ch) {
this.ch = ch;
}
protected final SocketChannel ch;
private ByteBuffer bb = null;
// Invoker's previous array
private byte[] bs = null;
......@@ -254,7 +246,8 @@ public class WispSocketImpl
}
if (so.getSoTimeout() > 0) {
WEA.addTimer(System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(so.getSoTimeout()));
WEA.addTimer(System.nanoTime() +
TimeUnit.MILLISECONDS.toNanos(so.getSoTimeout()));
}
do {
......@@ -263,7 +256,6 @@ public class WispSocketImpl
if (so.getSoTimeout() > 0 && WEA.isTimeout()) {
throw new SocketTimeoutException("time out");
}
} while ((n = ch.read(bb)) == 0);
} finally {
......@@ -295,7 +287,21 @@ public class WispSocketImpl
public void close() throws IOException {
WispSocketImpl.this.close();
}
};
}
public InputStream getInputStream() throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
if (!isConnected())
throw new SocketException("Socket is not connected");
if (isInputShutdown())
throw new SocketException("Socket input is shutdown");
if (socketInputStream == null) {
try {
socketInputStream = AccessController.doPrivileged(
new PrivilegedExceptionAction<InputStream>() {
public InputStream run() throws IOException {
return new WispSocketInputStream(getChannelImpl());
}
});
} catch (java.security.PrivilegedActionException e) {
......@@ -305,19 +311,13 @@ public class WispSocketImpl
return socketInputStream;
}
public OutputStream getOutputStream() throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
if (!isConnected())
throw new SocketException("Socket is not connected");
if (isOutputShutdown())
throw new SocketException("Socket output is shutdown");
try {
return AccessController.doPrivileged(
new PrivilegedExceptionAction<OutputStream>() {
public OutputStream run() throws IOException {
return new OutputStream() {
protected final SocketChannel ch = getChannelImpl();
private class WispSocketOutputStream extends OutputStream {
WispSocketOutputStream(SocketChannel ch) {
this.ch = ch;
}
protected final SocketChannel ch;
private ByteBuffer bb = null;
// Invoker's previous array
private byte[] bs = null;
......@@ -371,7 +371,8 @@ public class WispSocketImpl
}
if (so.getSoTimeout() > 0) {
WEA.addTimer(System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(so.getSoTimeout()));
WEA.addTimer(System.nanoTime() +
TimeUnit.MILLISECONDS.toNanos(so.getSoTimeout()));
}
do {
......@@ -395,7 +396,20 @@ public class WispSocketImpl
public void close() throws IOException {
WispSocketImpl.this.close();
}
};
}
public OutputStream getOutputStream() throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
if (!isConnected())
throw new SocketException("Socket is not connected");
if (isOutputShutdown())
throw new SocketException("Socket output is shutdown");
try {
return AccessController.doPrivileged(
new PrivilegedExceptionAction<OutputStream>() {
public OutputStream run() throws IOException {
return new WispSocketOutputStream(getChannelImpl());
}
});
} catch (java.security.PrivilegedActionException e) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册