seojangho closed pull request #96: [NEMO-177] Fix BytesDecoder to handle empty 
stream properly
URL: https://github.com/apache/incubator-nemo/pull/96
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/common/src/main/java/edu/snu/nemo/common/coder/BytesDecoderFactory.java 
b/common/src/main/java/edu/snu/nemo/common/coder/BytesDecoderFactory.java
index e62e0b86e..a01dc19a3 100644
--- a/common/src/main/java/edu/snu/nemo/common/coder/BytesDecoderFactory.java
+++ b/common/src/main/java/edu/snu/nemo/common/coder/BytesDecoderFactory.java
@@ -52,6 +52,7 @@ public static BytesDecoderFactory of() {
   private final class BytesDecoder implements Decoder<byte[]> {
 
     private final InputStream inputStream;
+    private boolean returnedArray;
 
     /**
      * Constructor.
@@ -60,6 +61,7 @@ public static BytesDecoderFactory of() {
      */
     private BytesDecoder(final InputStream inputStream) {
       this.inputStream = inputStream;
+      this.returnedArray = false;
     }
 
     @Override
@@ -75,11 +77,17 @@ private BytesDecoder(final InputStream inputStream) {
 
       final int lengthToRead = byteOutputStream.getCount();
       if (lengthToRead == 0) {
-        throw new IOException("EoF (empty partition)!"); // TODO #120: use EOF 
exception instead of IOException.
+        if (!returnedArray) {
+          returnedArray = true;
+          return new byte[0];
+        } else {
+          throw new IOException("EoF (empty partition)!"); // TODO #120: use 
EOF exception instead of IOException.
+        }
       }
       final byte[] resultBytes = new byte[lengthToRead]; // Read the size of 
this byte array.
       System.arraycopy(byteOutputStream.getBufDirectly(), 0, resultBytes, 0, 
lengthToRead);
 
+      returnedArray = true;
       return resultBytes;
     }
   }
diff --git 
a/common/src/test/java/edu/snu/nemo/common/coder/CoderFactoryTest.java 
b/common/src/test/java/edu/snu/nemo/common/coder/CoderFactoryTest.java
new file mode 100644
index 000000000..078b7f9ca
--- /dev/null
+++ b/common/src/test/java/edu/snu/nemo/common/coder/CoderFactoryTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2018 Seoul National University
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package edu.snu.nemo.common.coder;
+
+import edu.snu.nemo.common.ContextImpl;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+/**
+ * Tests {@link ContextImpl}.
+ */
+public class CoderFactoryTest {
+
+  @Test
+  public void testBytesCoderFactories() throws Exception {
+    final BytesEncoderFactory encoderFactory = BytesEncoderFactory.of();
+    final BytesDecoderFactory decoderFactory = BytesDecoderFactory.of();
+
+    // Test empty bytes.
+    byte[] elementToTest = new byte[0];
+    byte[] decodedBytes = encodeAndDecodeElement(encoderFactory, 
decoderFactory, elementToTest);
+    Assert.assertArrayEquals(elementToTest, decodedBytes);
+
+    // Test filled bytes.
+    elementToTest = "Hello NEMO!".getBytes();
+    decodedBytes = encodeAndDecodeElement(encoderFactory, decoderFactory, 
elementToTest);
+    Assert.assertArrayEquals(elementToTest, decodedBytes);
+  }
+
+  /**
+   * Encode and decode an element through the given factories and return the 
result elements.
+   *
+   * @param encoderFactory the encoder factory to test.
+   * @param decoderFactory the decoder factory to test.
+   * @param element        the element to test.
+   * @param <T>            the type of the element.
+   * @return the decoded element.
+   */
+  private <T> T encodeAndDecodeElement(final EncoderFactory<T> encoderFactory,
+                                                     final DecoderFactory<T> 
decoderFactory,
+                                                     final T element) throws 
Exception {
+    final byte[] encodedElement;
+    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+      final EncoderFactory.Encoder<T> encoder = encoderFactory.create(out);
+      encoder.encode(element);
+      encodedElement = out.toByteArray();
+    }
+
+    final T decodedElement;
+    try (final ByteArrayInputStream in = new 
ByteArrayInputStream(encodedElement)) {
+      final DecoderFactory.Decoder<T> decoder = decoderFactory.create(in);
+      decodedElement = decoder.decode();
+    }
+
+    return decodedElement;
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to