Skip to content

Commit 333d3bc

Browse files
committed
Refactor AsyncEchoServer2
1 parent 88a8d58 commit 333d3bc

2 files changed

Lines changed: 8 additions & 9 deletions

File tree

‎core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,18 @@ class ReadWriteHandler implements CompletionHandler<Integer, Map<String, Object>
5858

5959
@Override
6060
public void completed(Integer result, Map<String, Object> attachment) {
61-
Map<String, Object> actionInfo = attachment;
62-
String action = (String) actionInfo.get("action");
61+
String action = (String) attachment.get("action");
6362
if ("read".equals(action)) {
64-
ByteBuffer buffer = (ByteBuffer) actionInfo.get("buffer");
63+
ByteBuffer buffer = (ByteBuffer) attachment.get("buffer");
6564
buffer.flip();
66-
actionInfo.put("action", "write");
67-
clientChannel.write(buffer, actionInfo, this);
65+
attachment.put("action", "write");
66+
clientChannel.write(buffer, attachment, this);
6867
buffer.clear();
6968
} else if ("write".equals(action)) {
7069
ByteBuffer buffer = ByteBuffer.allocate(32);
71-
actionInfo.put("action", "read");
72-
actionInfo.put("buffer", buffer);
73-
clientChannel.read(buffer, actionInfo, this);
70+
attachment.put("action", "read");
71+
attachment.put("buffer", buffer);
72+
clientChannel.read(buffer, attachment, this);
7473
}
7574

7675
}

‎core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void failed(Throwable exc, ByteBuffer attachment) {
6060
@Test
6161
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
6262
String fileName = UUID.randomUUID().toString();
63-
Path path = Paths.get(fileName);
63+
Path path = Paths.get(Paths.get(HOME));
6464
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE,StandardOpenOption.DELETE_ON_CLOSE);
6565

6666
ByteBuffer buffer = ByteBuffer.allocate(1024);

0 commit comments

Comments
 (0)