Remove ByteSources.asByteSource

This method breaks the contract of ByteSource which specifies that
openStream can produce multiple independent streams.
This commit is contained in:
Andrew Gaul 2014-07-22 16:25:42 -07:00 committed by Jeremy Daggett
parent 957f6f16c7
commit 5c8bdcdfbb
5 changed files with 9 additions and 30 deletions

View File

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

View File

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

View File

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

View File

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

View File

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