This is an automated email from the ASF dual-hosted git repository.

gwlee 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 62fb70f  [NEMO-369] DirectByteArrayOutputStream usage refactoring 
(#206)
62fb70f is described below

commit 62fb70fdf514d6b312be09184331b2d6cfeb2349
Author: Haeyoon Cho <[email protected]>
AuthorDate: Tue Mar 26 22:40:01 2019 +0900

    [NEMO-369] DirectByteArrayOutputStream usage refactoring (#206)
    
    JIRA: [NEMO-369: DirectByteArrayOutputStream usage 
refactoring](https://issues.apache.org/jira/projects/NEMO/issues/NEMO-369)
    
    **Major changes:**
    - Replaced usage of duplicated method getCount() to size()
    - deleted getCount() method
    
    **Minor changes to note:**
    - None
    
    **Tests for the changes:**
    - None
    
    **Other comments:**
    - None
    
    Closes #206
---
 .../org/apache/nemo/common/DirectByteArrayOutputStream.java    | 10 +++-------
 .../java/org/apache/nemo/common/coder/BytesDecoderFactory.java |  5 ++---
 .../java/org/apache/nemo/runtime/executor/data/DataUtil.java   |  4 +++-
 .../runtime/executor/data/partition/SerializedPartition.java   |  2 +-
 4 files changed, 9 insertions(+), 12 deletions(-)

diff --git 
a/common/src/main/java/org/apache/nemo/common/DirectByteArrayOutputStream.java 
b/common/src/main/java/org/apache/nemo/common/DirectByteArrayOutputStream.java
index 5d5600d..84b9e3e 100644
--- 
a/common/src/main/java/org/apache/nemo/common/DirectByteArrayOutputStream.java
+++ 
b/common/src/main/java/org/apache/nemo/common/DirectByteArrayOutputStream.java
@@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream;
 /**
  * This class represents a custom implementation of {@link 
ByteArrayOutputStream},
  * which enables to get bytes buffer directly (without memory copy).
+ * TODO #370: Substitute ByteArrayOutputStream with java.nio.ByteBuffer
  */
 public final class DirectByteArrayOutputStream extends ByteArrayOutputStream {
 
@@ -42,16 +43,11 @@ public final class DirectByteArrayOutputStream extends 
ByteArrayOutputStream {
   }
 
   /**
+   * Note that serializedBytes include invalid bytes.
+   * So we have to use it with the actualLength by using size() whenever 
needed.
    * @return the buffer where data is stored.
    */
   public byte[] getBufDirectly() {
     return buf;
   }
-
-  /**
-   * @return the number of valid bytes in the buffer.
-   */
-  public int getCount() {
-    return count;
-  }
 }
diff --git 
a/common/src/main/java/org/apache/nemo/common/coder/BytesDecoderFactory.java 
b/common/src/main/java/org/apache/nemo/common/coder/BytesDecoderFactory.java
index 14d02b0..45c47b3 100644
--- a/common/src/main/java/org/apache/nemo/common/coder/BytesDecoderFactory.java
+++ b/common/src/main/java/org/apache/nemo/common/coder/BytesDecoderFactory.java
@@ -88,7 +88,7 @@ public final class BytesDecoderFactory implements 
DecoderFactory<byte[]> {
         b = inputStream.read();
       }
 
-      final int lengthToRead = byteOutputStream.getCount();
+      final int lengthToRead = byteOutputStream.size();
       if (lengthToRead == 0) {
         if (!returnedArray) {
           returnedArray = true;
@@ -97,8 +97,7 @@ public final class BytesDecoderFactory implements 
DecoderFactory<byte[]> {
           throw new EOFException("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);
+      final byte[] resultBytes = byteOutputStream.toByteArray();
 
       returnedArray = true;
       return resultBytes;
diff --git 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/DataUtil.java
 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/DataUtil.java
index 80e83df..29b61c8 100644
--- 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/DataUtil.java
+++ 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/DataUtil.java
@@ -119,8 +119,10 @@ public final class DataUtil {
         // We need to close wrappedStream on here, because 
DirectByteArrayOutputStream:getBufDirectly() returns
         // inner buffer directly, which can be an unfinished(not flushed) 
buffer.
         wrappedStream.close();
+        // Note that serializedBytes include invalid bytes.
+        // So we have to use it with the actualLength by using size() whenever 
needed.
         final byte[] serializedBytes = bytesOutputStream.getBufDirectly();
-        final int actualLength = bytesOutputStream.getCount();
+        final int actualLength = bytesOutputStream.size();
         serializedPartitions.add(
             new SerializedPartition<>(partitionToConvert.getKey(), 
serializedBytes, actualLength));
       }
diff --git 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/partition/SerializedPartition.java
 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/partition/SerializedPartition.java
index 71541c2..1bcde51 100644
--- 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/partition/SerializedPartition.java
+++ 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/partition/SerializedPartition.java
@@ -117,7 +117,7 @@ public final class SerializedPartition<K> implements 
Partition<byte[], K> {
       wrappedStream.close();
       this.serializedData = bytesOutputStream.getBufDirectly();
 
-      this.length = bytesOutputStream.getCount();
+      this.length = bytesOutputStream.size();
       this.committed = true;
     }
   }

Reply via email to