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