Prefer ByteStreams2 over ByteSources.asByteSource

The latter 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:50:46 -07:00
parent 18467a118f
commit 011aed71ed
2 changed files with 4 additions and 4 deletions

View File

@ -42,7 +42,7 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.internal.SignatureWire;
import org.jclouds.io.ByteSources;
import org.jclouds.io.ByteStreams2;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.MultipartForm;
@ -162,7 +162,7 @@ public class SignedHeaderAuth implements HttpRequestFilter {
checkArgument(payload != null, "payload was null");
checkArgument(payload.isRepeatable(), "payload must be repeatable: " + payload);
try {
return base64().encode(ByteSources.asByteSource(payload.getInput()).hash(sha1()).asBytes());
return base64().encode(ByteStreams2.hashAndClose(payload.getInput(), sha1()).asBytes());
} catch (Exception e) {
Throwables.propagateIfPossible(e);
throw new HttpException("error creating sigature for payload: " + payload, e);

View File

@ -21,7 +21,6 @@ import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.isEmpty;
import static com.google.common.hash.Hashing.md5;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.jclouds.io.ByteSources.asByteSource;
import static org.jclouds.util.Predicates2.retry;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
@ -55,6 +54,7 @@ import org.jclouds.chef.domain.UploadSandbox;
import org.jclouds.chef.options.CreateClientOptions;
import org.jclouds.chef.options.SearchOptions;
import org.jclouds.crypto.Pems;
import org.jclouds.io.ByteStream2;
import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.FilePayload;
import org.jclouds.rest.ResourceNotFoundException;
@ -167,7 +167,7 @@ public abstract class BaseChefApiLiveTest<A extends ChefApi> extends BaseChefLiv
InputStream stream = api.getResourceContents(resource);
assertNotNull(stream, "Resource contents are null for resource: " + resource.getName());
byte[] md5 = asByteSource(stream).hash(md5()).asBytes();
byte[] md5 = ByteStream2.hashAndClose(stream, md5()).asBytes();
assertEquals(md5, resource.getChecksum());
}
}