From 648e6c2a8a0896d459b1019a82688fc6302729f8 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 3 Apr 2013 12:59:23 -0700 Subject: [PATCH] Remove InputSuppliers.of(InputStream) This method breaks the contract of an InputSupplier since every call to getInput returns the same InputStream instance. This is particularly dangerous when one of the callers mutates or closes the InputStream which causes all others callers to fail. --- .../FilesystemAsyncBlobStoreTest.java | 5 +---- .../internal/BaseBlobIntegrationTest.java | 7 +++---- .../java/org/jclouds/io/InputSuppliers.java | 13 ------------- .../io/internal/BasePayloadSlicer.java | 19 ++++++++++--------- 4 files changed, 14 insertions(+), 30 deletions(-) diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java index 6a074c44cc..8e9543b498 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java @@ -52,7 +52,6 @@ import org.jclouds.filesystem.reference.FilesystemConstants; import org.jclouds.filesystem.util.Utils; import org.jclouds.filesystem.utils.TestUtils; import org.jclouds.http.HttpRequest; -import org.jclouds.io.InputSuppliers; import org.jclouds.io.Payload; import org.jclouds.io.payloads.PhantomPayload; import org.jclouds.io.payloads.StringPayload; @@ -603,9 +602,7 @@ public class FilesystemAsyncBlobStoreTest { InputSupplier expectedFile = Files.newInputStreamSupplier(new File( TARGET_CONTAINER_NAME, blobKey)); - InputSupplier actualFile = - InputSuppliers.of(resultBlob.getPayload().getInput()); - assertTrue(ByteStreams.equal(expectedFile, actualFile), + assertTrue(ByteStreams.equal(expectedFile, resultBlob.getPayload()), "Blob payload differs from file content"); // metadata are verified in the test for blobMetadata, so no need to // perform a complete test here diff --git a/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseBlobIntegrationTest.java b/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseBlobIntegrationTest.java index b2f2014241..b5bff46865 100644 --- a/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseBlobIntegrationTest.java +++ b/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseBlobIntegrationTest.java @@ -63,7 +63,6 @@ import org.jclouds.crypto.Crypto; import org.jclouds.encryption.internal.JCECrypto; import org.jclouds.http.BaseJettyTest; import org.jclouds.http.HttpResponseException; -import org.jclouds.io.InputSuppliers; import org.jclouds.io.Payload; import org.jclouds.io.Payloads; import org.jclouds.io.WriteTo; @@ -130,7 +129,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest { public void testPutFileParallel() throws InterruptedException, IOException, TimeoutException { File payloadFile = File.createTempFile("testPutFileParallel", "png"); - Files.copy(InputSuppliers.of(createTestInput()), payloadFile); + Files.copy(createTestInput(), payloadFile); payloadFile.deleteOnExit(); final Payload testPayload = Payloads.newFilePayload(payloadFile); @@ -607,13 +606,13 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest { assertEquals(metadata.getContentMetadata().getContentMD5(), md5().hashString(TEST_STRING, UTF_8).asBytes()); } - private InputStream createTestInput() throws IOException { + private File createTestInput() throws IOException { File file = File.createTempFile("testimg", "png"); file.deleteOnExit(); Random random = new Random(); byte[] buffer = new byte[random.nextInt(2 * 1024 * 1024)]; random.nextBytes(buffer); Files.copy(ByteStreams.newInputStreamSupplier(buffer), file); - return new FileInputStream(file); + return file; } } diff --git a/core/src/main/java/org/jclouds/io/InputSuppliers.java b/core/src/main/java/org/jclouds/io/InputSuppliers.java index 4e3e2f364b..15c2ae3d9e 100644 --- a/core/src/main/java/org/jclouds/io/InputSuppliers.java +++ b/core/src/main/java/org/jclouds/io/InputSuppliers.java @@ -20,7 +20,6 @@ package org.jclouds.io; import static com.google.common.base.Preconditions.checkNotNull; -import java.io.IOException; import java.io.InputStream; import com.google.common.annotations.Beta; @@ -36,18 +35,6 @@ import com.google.common.io.InputSupplier; @Beta public class InputSuppliers { - public static InputSupplier of(final InputStream in) { - checkNotNull(in, "in"); - return new InputSupplier() { - - @Override - public InputStream getInput() throws IOException { - return in; - } - - }; - } - public static InputSupplier of(String in) { byte[] bytes = checkNotNull(in, "in").getBytes(Charsets.UTF_8); return ByteStreams.newInputStreamSupplier(bytes); diff --git a/core/src/main/java/org/jclouds/io/internal/BasePayloadSlicer.java b/core/src/main/java/org/jclouds/io/internal/BasePayloadSlicer.java index e8a9081468..b1925f1f12 100644 --- a/core/src/main/java/org/jclouds/io/internal/BasePayloadSlicer.java +++ b/core/src/main/java/org/jclouds/io/internal/BasePayloadSlicer.java @@ -22,20 +22,20 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.InputStream; +import java.io.IOException; import javax.inject.Singleton; -import org.jclouds.io.InputSuppliers; import org.jclouds.io.Payload; import org.jclouds.io.PayloadSlicer; import org.jclouds.io.payloads.BaseMutableContentMetadata; +import org.jclouds.io.payloads.InputStreamPayload; import org.jclouds.io.payloads.InputStreamSupplierPayload; import com.google.common.base.Throwables; import com.google.common.io.ByteStreams; +import com.google.common.io.Files; /** * @@ -73,15 +73,16 @@ public class BasePayloadSlicer implements PayloadSlicer { } protected Payload doSlice(File content, long offset, long length) { - try { - return doSlice(new FileInputStream(content), offset, length); - } catch (FileNotFoundException e) { - throw Throwables.propagate(e); - } + return new InputStreamSupplierPayload(ByteStreams.slice(Files.newInputStreamSupplier(content), offset, length)); } protected Payload doSlice(InputStream content, long offset, long length) { - return new InputStreamSupplierPayload(ByteStreams.slice(InputSuppliers.of(content), offset, length)); + try { + ByteStreams.skipFully(content, offset); + } catch (IOException ioe) { + throw Throwables.propagate(ioe); + } + return new InputStreamPayload(ByteStreams.limit(content, length)); } protected Payload doSlice(byte[] content, long offset, long length) {