You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by ga...@apache.org on 2016/02/26 07:01:25 UTC

jclouds-labs git commit: Properly size buffers in storeData

Repository: jclouds-labs
Updated Branches:
  refs/heads/master 0e5e7faeb -> e007ea41a


Properly size buffers in storeData

This ensures that we only persist the bytes read and not any trailing
NULs.


Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/e007ea41
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/e007ea41
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/e007ea41

Branch: refs/heads/master
Commit: e007ea41ac7d52fc276afb7f007407832eb1a7b8
Parents: 0e5e7fa
Author: Andrew Gaul <ga...@apache.org>
Authored: Thu Feb 25 21:59:10 2016 -0800
Committer: Andrew Gaul <ga...@apache.org>
Committed: Thu Feb 25 21:59:10 2016 -0800

----------------------------------------------------------------------
 .../java/org/jclouds/jdbc/service/JdbcService.java   | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/e007ea41/jdbc/src/main/java/org/jclouds/jdbc/service/JdbcService.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/jclouds/jdbc/service/JdbcService.java b/jdbc/src/main/java/org/jclouds/jdbc/service/JdbcService.java
index 80aa528..fe89d9e 100644
--- a/jdbc/src/main/java/org/jclouds/jdbc/service/JdbcService.java
+++ b/jdbc/src/main/java/org/jclouds/jdbc/service/JdbcService.java
@@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.hash.HashCode;
 import com.google.common.hash.Hashing;
 import com.google.common.hash.HashingInputStream;
+import com.google.common.io.ByteStreams;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
 import com.google.inject.persist.Transactional;
@@ -40,6 +41,7 @@ import org.jclouds.util.Closeables2;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 
@@ -243,13 +245,16 @@ public class JdbcService {
    @Transactional(rollbackOn = IOException.class)
    private List<Long> storeData(InputStream data) throws IOException {
       ImmutableList.Builder<Long> chunks = ImmutableList.builder();
-      int bytes;
-      byte[] buffer = new byte[JdbcConstants.DEFAULT_CHUNK_SIZE];
-      while ((bytes = data.read(buffer, 0, JdbcConstants.DEFAULT_CHUNK_SIZE)) != -1) {
+      while (true) {
+         byte[] buffer = new byte[JdbcConstants.DEFAULT_CHUNK_SIZE];
+         int bytes = ByteStreams.read(data, buffer, 0, JdbcConstants.DEFAULT_CHUNK_SIZE);
+         if (bytes == 0) {
+            break;
+         } else if (bytes != buffer.length) {
+            buffer = Arrays.copyOf(buffer, bytes);
+         }
          chunks.add(chunkRepository.create(new ChunkEntity(buffer, bytes)).getId());
-         buffer = new byte[JdbcConstants.DEFAULT_CHUNK_SIZE];
       }
-      data.close();
       return chunks.build();
    }
 }