From 5c8bdcdfbb01ac876d0ea31b4fd2e539cf9a0085 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Tue, 22 Jul 2014 16:25:42 -0700 Subject: [PATCH] Remove ByteSources.asByteSource This method breaks the contract of ByteSource which specifies that openStream can produce multiple independent streams. --- .../SwiftBlobIntegrationLiveTest.java | 4 ++-- .../main/java/org/jclouds/io/ByteSources.java | 21 ------------------- ...CommandExecutorServiceIntegrationTest.java | 4 ++-- .../java/org/jclouds/http/BaseJettyTest.java | 4 ++-- .../jclouds/http/internal/WireLiveTest.java | 6 +++--- 5 files changed, 9 insertions(+), 30 deletions(-) diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/integration/SwiftBlobIntegrationLiveTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/integration/SwiftBlobIntegrationLiveTest.java index 0ab06a9586..36dba5643e 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/integration/SwiftBlobIntegrationLiveTest.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/integration/SwiftBlobIntegrationLiveTest.java @@ -22,13 +22,13 @@ import static org.testng.Assert.assertTrue; import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; import java.util.Properties; import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.integration.internal.BaseBlobIntegrationTest; import org.jclouds.blobstore.options.PutOptions; -import org.jclouds.io.ByteSources; import org.jclouds.io.ByteStreams2; import org.jclouds.io.Payload; import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties; @@ -153,7 +153,7 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest { // download and check if correct Blob read = blobStore.getBlob(containerName, objectName); Payload readPayload = read.getPayload(); - assertTrue(inputSource.contentEquals(ByteSources.asByteSource(readPayload.openStream()))); + assertTrue(Arrays.equals(inputSource.read(), ByteStreams2.toByteArrayAndClose(readPayload.openStream()))); } finally { returnContainer(containerName); } diff --git a/core/src/main/java/org/jclouds/io/ByteSources.java b/core/src/main/java/org/jclouds/io/ByteSources.java index 61ac07d2b7..b8abd79bb3 100644 --- a/core/src/main/java/org/jclouds/io/ByteSources.java +++ b/core/src/main/java/org/jclouds/io/ByteSources.java @@ -16,11 +16,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; import com.google.common.collect.Iterables; import com.google.common.io.ByteSource; @@ -31,22 +26,6 @@ import com.google.common.io.ByteSource; @Beta public class ByteSources { - /** - * always returns the same stream - * - * @param in - * stream to always return - */ - public static ByteSource asByteSource(final InputStream in) { - checkNotNull(in, "in"); - return new ByteSource() { - @Override - public InputStream openStream() throws IOException { - return in; - } - }; - } - /** Create an infinite-length ByteSource which repeats its input. */ public static ByteSource repeatingArrayByteSource(final byte[] input) { return ByteSource.concat(Iterables.cycle(ByteSource.wrap(input))); diff --git a/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java b/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java index fbb2412e12..708301ccff 100644 --- a/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java +++ b/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java @@ -21,7 +21,6 @@ import static com.google.common.io.BaseEncoding.base64; import static com.google.common.io.Closeables.close; import static com.google.common.io.Files.asByteSource; import static org.jclouds.http.options.GetOptions.Builder.tail; -import static org.jclouds.io.ByteSources.asByteSource; import static org.jclouds.io.Payloads.newByteSourcePayload; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; @@ -35,6 +34,7 @@ import java.net.URLDecoder; import java.util.Random; import java.util.concurrent.TimeUnit; +import org.jclouds.io.ByteStreams2; import org.jclouds.io.Payload; import org.jclouds.util.Strings2; import org.jclouds.utils.TestUtils; @@ -229,7 +229,7 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base } private void assertValidMd5(final InputStream input, String md5) throws IOException { - assertEquals(base64().encode(asByteSource(input).hash(md5()).asBytes()), md5); + assertEquals(base64().encode(ByteStreams2.hashAndClose(input, md5()).asBytes()), md5); } private static class MD5CheckDispatcher extends Dispatcher { diff --git a/core/src/test/java/org/jclouds/http/BaseJettyTest.java b/core/src/test/java/org/jclouds/http/BaseJettyTest.java index 420c54eea1..ee2b031e84 100644 --- a/core/src/test/java/org/jclouds/http/BaseJettyTest.java +++ b/core/src/test/java/org/jclouds/http/BaseJettyTest.java @@ -27,7 +27,6 @@ import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterrup import static javax.servlet.http.HttpServletResponse.SC_OK; import static org.jclouds.Constants.PROPERTY_RELAX_HOSTNAME; import static org.jclouds.Constants.PROPERTY_TRUST_ALL_CERTS; -import static org.jclouds.io.ByteSources.asByteSource; import static org.jclouds.util.Closeables2.closeQuietly; import static org.jclouds.util.Strings2.toStringAndClose; @@ -52,6 +51,7 @@ import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.ssl.SslSelectChannelConnector; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.jclouds.ContextBuilder; +import org.jclouds.io.ByteStreams2; import org.jclouds.providers.AnonymousProviderMetadata; import org.jclouds.utils.TestUtils; import org.testng.annotations.AfterClass; @@ -173,7 +173,7 @@ public abstract class BaseJettyTest { if (request.getHeader("Content-MD5") != null) { String expectedMd5 = request.getHeader("Content-MD5"); String realMd5FromRequest; - realMd5FromRequest = base64().encode(asByteSource(body).hash(md5()).asBytes()); + realMd5FromRequest = base64().encode(ByteStreams2.hashAndClose(body, md5()).asBytes()); boolean matched = expectedMd5.equals(realMd5FromRequest); if (matched) { response.setStatus(SC_OK); diff --git a/core/src/test/java/org/jclouds/http/internal/WireLiveTest.java b/core/src/test/java/org/jclouds/http/internal/WireLiveTest.java index 6e7584c658..95e000e3e9 100644 --- a/core/src/test/java/org/jclouds/http/internal/WireLiveTest.java +++ b/core/src/test/java/org/jclouds/http/internal/WireLiveTest.java @@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.hash.Hashing.md5; import static com.google.common.io.BaseEncoding.base16; import static java.util.concurrent.Executors.newCachedThreadPool; -import static org.jclouds.io.ByteSources.asByteSource; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -33,6 +32,7 @@ import java.util.concurrent.TimeUnit; import com.google.common.io.ByteStreams; +import org.jclouds.io.ByteStreams2; import org.jclouds.logging.Logger; import org.testng.annotations.Test; @@ -132,7 +132,7 @@ public class WireLiveTest { URLConnection connection = url.openConnection(); HttpWire wire = setUp(); InputStream in = wire.input(connection.getInputStream()); - byte[] compare = asByteSource(in).hash(md5()).asBytes(); + byte[] compare = ByteStreams2.hashAndClose(in, md5()).asBytes(); Thread.sleep(100); assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5)); assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484); @@ -157,7 +157,7 @@ public class WireLiveTest { URLConnection connection = url.openConnection(); HttpWire wire = setUpSynch(); InputStream in = wire.input(connection.getInputStream()); - byte[] compare = asByteSource(in).hash(md5()).asBytes(); + byte[] compare = ByteStreams2.hashAndClose(in, md5()).asBytes(); Thread.sleep(100); assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5)); assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484);