You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/04/27 02:19:45 UTC

[james-project] 09/14: JAMES-3140 Size buffer to the threshold

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

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 7ed1ab15e19a06491c2c74a8c766aac61fe20aef
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Apr 23 11:46:27 2020 +0700

    JAMES-3140 Size buffer to the threshold
    
    Array copy is still needed to resize the returned bytes array to the
    actual data size, avoiding padding with zeros
---
 .../apache/james/blob/cassandra/cache/CachedBlobStore.java   | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CachedBlobStore.java b/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CachedBlobStore.java
index 421386f..2639c64 100644
--- a/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CachedBlobStore.java
+++ b/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CachedBlobStore.java
@@ -30,6 +30,7 @@ import java.util.Optional;
 import javax.inject.Inject;
 import javax.inject.Named;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.blob.api.BlobId;
 import org.apache.james.blob.api.BlobStore;
 import org.apache.james.blob.api.BucketName;
@@ -136,11 +137,11 @@ public class CachedBlobStore implements BlobStore {
     }
 
     private Optional<byte[]> fullyReadSmallStream(PushbackInputStream pushbackInputStream) throws IOException {
-        int sizeToRead = sizeThresholdInBytes + 1;
-        byte[] bytes = new byte[sizeToRead];
-        int readByteCount = pushbackInputStream.read(bytes, 0, sizeToRead);
+        byte[] bytes = new byte[sizeThresholdInBytes];
+        int readByteCount = IOUtils.read(pushbackInputStream, bytes);
+        int extraByte = pushbackInputStream.read();
         try {
-            if (readByteCount > sizeThresholdInBytes) {
+            if (extraByte >= 0) {
                 return Optional.empty();
             }
             if (readByteCount < 0) {
@@ -148,6 +149,9 @@ public class CachedBlobStore implements BlobStore {
             }
             return Optional.of(Arrays.copyOf(bytes, readByteCount));
         } finally {
+            if (extraByte >= 0) {
+                pushbackInputStream.unread(extraByte);
+            }
             if (readByteCount > 0) {
                 pushbackInputStream.unread(bytes, 0, readByteCount);
             }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org