nemo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jan...@apache.org
Subject [incubator-nemo] branch master updated: [NEMO-177] Fix BytesDecoder to handle empty stream properly (#96)
Date Thu, 09 Aug 2018 05:44:35 GMT
This is an automated email from the ASF dual-hosted git repository.

jangho pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nemo.git


The following commit(s) were added to refs/heads/master by this push:
     new 54dc2ca  [NEMO-177] Fix BytesDecoder to handle empty stream properly (#96)
54dc2ca is described below

commit 54dc2ca4d478df6b16b766a93fe50c6993a956ae
Author: Sanha Lee <sanhaleehana@gmail.com>
AuthorDate: Thu Aug 9 14:44:33 2018 +0900

    [NEMO-177] Fix BytesDecoder to handle empty stream properly (#96)
    
    JIRA: [NEMO-177: Fix BytesDecoder to handle empty stream properly](https://issues.apache.org/jira/projects/NEMO/issues/NEMO-177)
    
    **Major changes:**
    - Make `BytesDecoder` of `BytesDecoderFactory` to return empty byte[] at first, and throw
EoF for the next time if it receives empty byte array.
    
    **Minor changes to note:**
    - N/A.
    
    **Tests for the changes:**
    - Add `CoderFactoryTest` to test this feature.
    
    **Other comments:**
    - N/A.
    
    resolves [NEMO-177](https://issues.apache.org/jira/projects/NEMO/issues/NEMO-177)
---
 .../snu/nemo/common/coder/BytesDecoderFactory.java | 10 ++-
 .../snu/nemo/common/coder/CoderFactoryTest.java    | 74 ++++++++++++++++++++++
 2 files changed, 83 insertions(+), 1 deletion(-)

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 e62e0b8..a01dc19 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 final class BytesDecoderFactory implements DecoderFactory<byte[]>
{
   private final class BytesDecoder implements Decoder<byte[]> {
 
     private final InputStream inputStream;
+    private boolean returnedArray;
 
     /**
      * Constructor.
@@ -60,6 +61,7 @@ public final class BytesDecoderFactory implements DecoderFactory<byte[]>
{
      */
     private BytesDecoder(final InputStream inputStream) {
       this.inputStream = inputStream;
+      this.returnedArray = false;
     }
 
     @Override
@@ -75,11 +77,17 @@ public final class BytesDecoderFactory implements DecoderFactory<byte[]>
{
 
       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 0000000..078b7f9
--- /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;
+  }
+}


Mime
View raw message