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.
This commit is contained in:
Andrew Gaul 2013-04-03 12:59:23 -07:00
parent f0a1054958
commit 648e6c2a8a
4 changed files with 14 additions and 30 deletions

View File

@ -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<FileInputStream> expectedFile =
Files.newInputStreamSupplier(new File(
TARGET_CONTAINER_NAME, blobKey));
InputSupplier<? extends InputStream> 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

View File

@ -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;
}
}

View File

@ -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<? extends InputStream> of(final InputStream in) {
checkNotNull(in, "in");
return new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return in;
}
};
}
public static InputSupplier<? extends InputStream> of(String in) {
byte[] bytes = checkNotNull(in, "in").getBytes(Charsets.UTF_8);
return ByteStreams.newInputStreamSupplier(bytes);

View File

@ -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) {