From 333d3bc1226787a45fe3113d8f7999db76e738e7 Mon Sep 17 00:00:00 2001 From: pivovarit Date: Mon, 28 Nov 2016 22:15:24 +0100 Subject: [PATCH] Refactor AsyncEchoServer2 --- .../java/nio2/async/AsyncEchoServer2.java | 15 +++++++-------- .../baeldung/java/nio2/async/AsyncFileTest.java | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java index 03ce233ce9..172d8036de 100644 --- a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java +++ b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java @@ -58,19 +58,18 @@ public class AsyncEchoServer2 { @Override public void completed(Integer result, Map attachment) { - Map 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); } } diff --git a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java index 948c93ff0b..fcffc524b1 100644 --- a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java +++ b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java @@ -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);