Repository: deepjavalibrary/djl
Pull request #2007 (2022-09-11 10:30:49)

[fix] DynamicBuffer position error when expand for the firtst time
## Description ##

When the DynamicBuffer exapnd for the first time, its FloatBuffer.position would incorrectly increase by 1 resulting in an error in the location of subsequent data.

**details** 
This can reproduce the error
```java
DynamicBuffer bb = new DynamicBuffer();
for (int i = 0; i < 129; i++) {
    bb.put((float) i);
}
FloatBuffer buf = bb.getBuffer();
float value = buf.get(128); // This value should be 128f but got 0f.
```
This problem arises because DynamicBuffer expands when it is about to be full but not yet full, and adds the content before the expansion to the expanded Buffer, **but because it is not yet full, the last position before the expansion of the element is not filled, and it is also added to the new Buffer**, resulting in the position error of subsequent elements

codecov-commenter: # [Codecov](https://codecov.io/gh/deepjavalibrary/djl/pull/2007?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) Report
> Merging [#2007](https://codecov.io/gh/deepjavalibrary/djl/pull/2007?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) (b8183ea) into [master](https://codecov.io/gh/deepjavalibrary/djl/commit/bb5073f7ffc33f1883621fc1783dbb01e70b418f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) (bb5073f) will **decrease** coverage by `2.28%`.
> The diff coverage is `68.19%`.

```diff
@@             Coverage Diff              @@
##             master    #2007      +/-   ##
============================================
- Coverage     72.08%   69.80%   -2.29%     
- Complexity     5126     5932     +806     
============================================
  Files           473      587     +114     
  Lines         21970    26312    +4342     
  Branches       2351     2855     +504     
============================================
+ Hits          15838    18366    +2528     
- Misses         4925     6567    +1642     
- Partials       1207     1379     +172     
```


| [Impacted Files](https://codecov.io/gh/deepjavalibrary/djl/pull/2007?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) | Coverage Δ | |
|---|---|---|
| [api/src/main/java/ai/djl/modality/cv/Image.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L0ltYWdlLmphdmE=) | `69.23% <ø> (-4.11%)` | :arrow_down: |
| [...rc/main/java/ai/djl/modality/cv/MultiBoxPrior.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L011bHRpQm94UHJpb3IuamF2YQ==) | `76.00% <ø> (ø)` | |
| [...rc/main/java/ai/djl/modality/cv/output/Joints.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9Kb2ludHMuamF2YQ==) | `71.42% <ø> (ø)` | |
| [.../main/java/ai/djl/modality/cv/output/Landmark.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9MYW5kbWFyay5qYXZh) | `100.00% <ø> (ø)` | |
| [...main/java/ai/djl/modality/cv/output/Rectangle.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9SZWN0YW5nbGUuamF2YQ==) | `72.41% <0.00%> (ø)` | |
| [...i/djl/modality/cv/translator/BigGANTranslator.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvQmlnR0FOVHJhbnNsYXRvci5qYXZh) | `21.42% <0.00%> (-5.24%)` | :arrow_down: |
| [...odality/cv/translator/BigGANTranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvQmlnR0FOVHJhbnNsYXRvckZhY3RvcnkuamF2YQ==) | `33.33% <0.00%> (+8.33%)` | :arrow_up: |
| [.../cv/translator/InstanceSegmentationTranslator.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvSW5zdGFuY2VTZWdtZW50YXRpb25UcmFuc2xhdG9yLmphdmE=) | `0.00% <0.00%> (-86.59%)` | :arrow_down: |
| [...nslator/InstanceSegmentationTranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvSW5zdGFuY2VTZWdtZW50YXRpb25UcmFuc2xhdG9yRmFjdG9yeS5qYXZh) | `7.14% <0.00%> (-11.04%)` | :arrow_down: |
| [.../cv/translator/SemanticSegmentationTranslator.java](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvU2VtYW50aWNTZWdtZW50YXRpb25UcmFuc2xhdG9yLmphdmE=) | `0.00% <0.00%> (ø)` | |
| ... and [503 more](https://codecov.io/gh/deepjavalibrary/djl/pull/2007/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary)


Patch:
diff --git a/basicdataset/src/main/java/ai/djl/basicdataset/tabular/utils/DynamicBuffer.java b/basicdataset/src/main/java/ai/djl/basicdataset/tabular/utils/DynamicBuffer.java
index 3ab3e9ed4..f9fec157f 100644
--- a/basicdataset/src/main/java/ai/djl/basicdataset/tabular/utils/DynamicBuffer.java
+++ b/basicdataset/src/main/java/ai/djl/basicdataset/tabular/utils/DynamicBuffer.java
@@ -33,13 +33,13 @@ public class DynamicBuffer {
      */
     public DynamicBuffer put(float f) {
         ++length;
+        buffer.put(f);
         if (buffer.capacity() == length) {
             FloatBuffer buf = buffer;
             buf.rewind();
             buffer = FloatBuffer.allocate(length * 2);
             buffer.put(buf);
         }
-        buffer.put(f);
         return this;
     }
 


Test patch:
diff --git a/basicdataset/src/test/java/ai/djl/basicdataset/DatasetUtilsTest.java b/basicdataset/src/test/java/ai/djl/basicdataset/DatasetUtilsTest.java
index 56f27dfeb..f8b30a69d 100644
--- a/basicdataset/src/test/java/ai/djl/basicdataset/DatasetUtilsTest.java
+++ b/basicdataset/src/test/java/ai/djl/basicdataset/DatasetUtilsTest.java
@@ -13,6 +13,7 @@
 package ai.djl.basicdataset;
 
 import ai.djl.Device;
+import ai.djl.basicdataset.tabular.utils.DynamicBuffer;
 import ai.djl.ndarray.NDArray;
 import ai.djl.ndarray.NDList;
 import ai.djl.ndarray.NDManager;
@@ -24,6 +25,7 @@ import ai.djl.translate.Batchifier;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
+import java.nio.FloatBuffer;
 import java.util.Locale;
 
 public class DatasetUtilsTest {
@@ -161,4 +163,34 @@ public class DatasetUtilsTest {
             }
         }
     }
+
+    @Test
+    public void testDynamicBuffer() {
+        DynamicBuffer bb = new DynamicBuffer();
+        // Do not expand
+        for (int i = 0; i < 128; i++) {
+            bb.put((float) i);
+        }
+        FloatBuffer buf = bb.getBuffer();
+        Assert.assertEquals(buf.limit(), 128);
+        Assert.assertEquals(buf.get(127), 127f);
+
+        bb = new DynamicBuffer();
+        // expand 1 time
+        for (int i = 0; i < 129; i++) {
+            bb.put((float) i);
+        }
+        buf = bb.getBuffer();
+        Assert.assertEquals(buf.limit(), 129);
+        Assert.assertEquals(buf.get(128), 128f);
+
+        bb = new DynamicBuffer();
+        // expand 2 times
+        for (int i = 0; i < 257; i++) {
+            bb.put((float) i);
+        }
+        buf = bb.getBuffer();
+        Assert.assertEquals(buf.limit(), 257);
+        Assert.assertEquals(buf.get(256), 256f);
+    }
 }