Refactor AsyncEchoServer2

This commit is contained in:
pivovarit 2016-11-28 22:15:24 +01:00
parent 88a8d5838f
commit 333d3bc122
2 changed files with 8 additions and 9 deletions

View File

@ -58,19 +58,18 @@ public class AsyncEchoServer2 {
@Override
public void completed(Integer result, Map<String, Object> attachment) {
Map<String, Object> actionInfo = attachment;
String action = (String) actionInfo.get("action");
String action = (String) attachment.get("action");
if ("read".equals(action)) {
ByteBuffer buffer = (ByteBuffer) actionInfo.get("buffer");
ByteBuffer buffer = (ByteBuffer) attachment.get("buffer");
buffer.flip();
actionInfo.put("action", "write");
clientChannel.write(buffer, actionInfo, this);
attachment.put("action", "write");
clientChannel.write(buffer, attachment, this);
buffer.clear();
} else if ("write".equals(action)) {
ByteBuffer buffer = ByteBuffer.allocate(32);
actionInfo.put("action", "read");
actionInfo.put("buffer", buffer);
clientChannel.read(buffer, actionInfo, this);
attachment.put("action", "read");
attachment.put("buffer", buffer);
clientChannel.read(buffer, attachment, this);
}
}

View File

@ -60,7 +60,7 @@ public class AsyncFileTest {
@Test
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
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);
ByteBuffer buffer = ByteBuffer.allocate(1024);