Refactor AsyncEchoServer2
This commit is contained in:
parent
88a8d5838f
commit
333d3bc122
|
@ -58,19 +58,18 @@ public class AsyncEchoServer2 {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void completed(Integer result, Map<String, Object> attachment) {
|
public void completed(Integer result, Map<String, Object> attachment) {
|
||||||
Map<String, Object> actionInfo = attachment;
|
String action = (String) attachment.get("action");
|
||||||
String action = (String) actionInfo.get("action");
|
|
||||||
if ("read".equals(action)) {
|
if ("read".equals(action)) {
|
||||||
ByteBuffer buffer = (ByteBuffer) actionInfo.get("buffer");
|
ByteBuffer buffer = (ByteBuffer) attachment.get("buffer");
|
||||||
buffer.flip();
|
buffer.flip();
|
||||||
actionInfo.put("action", "write");
|
attachment.put("action", "write");
|
||||||
clientChannel.write(buffer, actionInfo, this);
|
clientChannel.write(buffer, attachment, this);
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
} else if ("write".equals(action)) {
|
} else if ("write".equals(action)) {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(32);
|
ByteBuffer buffer = ByteBuffer.allocate(32);
|
||||||
actionInfo.put("action", "read");
|
attachment.put("action", "read");
|
||||||
actionInfo.put("buffer", buffer);
|
attachment.put("buffer", buffer);
|
||||||
clientChannel.read(buffer, actionInfo, this);
|
clientChannel.read(buffer, attachment, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ public class AsyncFileTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
|
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
|
||||||
String fileName = UUID.randomUUID().toString();
|
String fileName = UUID.randomUUID().toString();
|
||||||
Path path = Paths.get(fileName);
|
Path path = Paths.get(Paths.get(HOME));
|
||||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE,StandardOpenOption.DELETE_ON_CLOSE);
|
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE,StandardOpenOption.DELETE_ON_CLOSE);
|
||||||
|
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||||
|
|
Loading…
Reference in New Issue