From d7e0b3b10c17611cf8432b2638eddf28a47576cf Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 19 Feb 2014 13:21:26 -0800 Subject: [PATCH] Remove WriteTo Guava helpers capture this functionality in a more idiomatic way. --- .../SwiftBlobIntegrationLiveTest.java | 3 +- .../blobstore/TransientStorageStrategy.java | 7 ++-- .../JavaUrlHttpCommandExecutorService.java | 3 +- .../src/main/java/org/jclouds/io/Payload.java | 2 +- .../src/main/java/org/jclouds/io/WriteTo.java | 34 ------------------- .../io/payloads/BaseCipherPayload.java | 7 ---- .../org/jclouds/io/payloads/BasePayload.java | 20 ----------- .../io/payloads/DelegatingPayload.java | 9 ----- .../jclouds/io/payloads/PhantomPayload.java | 8 ----- .../io/payloads/MultipartFormTest.java | 7 ---- .../jclouds/http/apachehc/ApacheHCUtils.java | 7 ---- .../org/jclouds/gae/ConvertToGaeRequest.java | 6 ++-- 12 files changed, 9 insertions(+), 104 deletions(-) delete mode 100644 core/src/main/java/org/jclouds/io/WriteTo.java 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 03a4bb9684..64b828a6eb 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,7 +22,6 @@ import static org.testng.Assert.assertTrue; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; @@ -148,7 +147,7 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest { blobStore.putBlob(container, write, PutOptions.Builder.multipart()); Blob read = blobStore.getBlob(container, "const.txt"); - read.getPayload().writeTo(new FileOutputStream(outFile)); + Files.copy(read.getPayload(), outFile); assertEquals(Files.hash(outFile, Hashing.md5()), Files.hash(inFile, Hashing.md5())); diff --git a/blobstore/src/main/java/org/jclouds/blobstore/TransientStorageStrategy.java b/blobstore/src/main/java/org/jclouds/blobstore/TransientStorageStrategy.java index 09e2941119..322fd4aefb 100644 --- a/blobstore/src/main/java/org/jclouds/blobstore/TransientStorageStrategy.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/TransientStorageStrategy.java @@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.io.BaseEncoding.base16; import static org.jclouds.http.Uris.uriBuilder; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Date; import java.util.Map; @@ -48,6 +47,7 @@ import com.google.common.base.Supplier; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; import com.google.common.collect.Multimaps; +import com.google.common.io.ByteStreams; public class TransientStorageStrategy implements LocalStorageStrategy { private final ConcurrentMap> containerToBlobs = new ConcurrentHashMap>(); @@ -158,9 +158,8 @@ public class TransientStorageStrategy implements LocalStorageStrategy { try { if (payload == null || !(payload instanceof ByteArrayPayload)) { MutableContentMetadata oldMd = in.getPayload().getContentMetadata(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - in.getPayload().writeTo(out); - payload = (ByteArrayPayload) Payloads.calculateMD5(Payloads.newPayload(out.toByteArray())); + byte[] out = ByteStreams.toByteArray(in.getPayload()); + payload = (ByteArrayPayload) Payloads.calculateMD5(Payloads.newPayload(out)); HttpUtils.copy(oldMd, payload.getContentMetadata()); } else { if (payload.getContentMetadata().getContentMD5() == null) diff --git a/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java b/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java index 1859f6c329..16becd15a9 100644 --- a/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java +++ b/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java @@ -60,6 +60,7 @@ import com.google.common.base.Function; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap.Builder; +import com.google.common.io.ByteStreams; import com.google.common.io.CountingOutputStream; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.inject.Inject; @@ -304,7 +305,7 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe connection.setDoOutput(true); CountingOutputStream out = new CountingOutputStream(connection.getOutputStream()); try { - payload.writeTo(out); + ByteStreams.copy(payload, out); } catch (IOException e) { logger.error(e, "error after writing %d/%s bytes to %s", out.getCount(), lengthDesc, connection.getURL()); throw e; diff --git a/core/src/main/java/org/jclouds/io/Payload.java b/core/src/main/java/org/jclouds/io/Payload.java index 6d08ce7e23..c3f64bde85 100644 --- a/core/src/main/java/org/jclouds/io/Payload.java +++ b/core/src/main/java/org/jclouds/io/Payload.java @@ -25,7 +25,7 @@ import com.google.common.io.InputSupplier; /** * @author Adrian Cole */ -public interface Payload extends InputSupplier, WriteTo, Closeable { +public interface Payload extends InputSupplier, Closeable { /** * Creates a new InputStream object of the payload. diff --git a/core/src/main/java/org/jclouds/io/WriteTo.java b/core/src/main/java/org/jclouds/io/WriteTo.java deleted file mode 100644 index 3797f72301..0000000000 --- a/core/src/main/java/org/jclouds/io/WriteTo.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jclouds.io; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * @author Adrian Cole - */ -public interface WriteTo { - - /** - * Writes the payload content to the output stream. - * - * @throws IOException - */ - void writeTo(OutputStream outstream) throws IOException; - -} diff --git a/core/src/main/java/org/jclouds/io/payloads/BaseCipherPayload.java b/core/src/main/java/org/jclouds/io/payloads/BaseCipherPayload.java index 67864cd9ce..aa72cd8f9f 100644 --- a/core/src/main/java/org/jclouds/io/payloads/BaseCipherPayload.java +++ b/core/src/main/java/org/jclouds/io/payloads/BaseCipherPayload.java @@ -19,12 +19,10 @@ package org.jclouds.io.payloads; import static com.google.common.base.Preconditions.checkNotNull; import java.io.IOException; -import java.io.OutputStream; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; -import javax.crypto.CipherOutputStream; import org.jclouds.io.Payload; @@ -47,9 +45,4 @@ public abstract class BaseCipherPayload extends DelegatingPayload { public CipherInputStream openStream() throws IOException { return new CipherInputStream(super.openStream(), initializeCipher(key)); } - - @Override - public void writeTo(OutputStream outstream) throws IOException { - super.writeTo(new CipherOutputStream(outstream, initializeCipher(key))); - } } diff --git a/core/src/main/java/org/jclouds/io/payloads/BasePayload.java b/core/src/main/java/org/jclouds/io/payloads/BasePayload.java index 0def5397e8..d11216e6e9 100644 --- a/core/src/main/java/org/jclouds/io/payloads/BasePayload.java +++ b/core/src/main/java/org/jclouds/io/payloads/BasePayload.java @@ -17,13 +17,9 @@ package org.jclouds.io.payloads; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; -import static com.google.common.io.ByteStreams.copy; -import static org.jclouds.util.Closeables2.closeQuietly; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import com.google.common.base.Throwables; @@ -64,22 +60,6 @@ public abstract class BasePayload implements Payload { return content; } - /** - * {@inheritDoc} - */ - @Override - public void writeTo(OutputStream outstream) throws IOException { - checkState(!written || isRepeatable(), "can only write to an outputStream once"); - written = true; - InputStream in = getInput(); - try { - copy(in, outstream); - outstream.flush(); - } finally { - closeQuietly(in); - } - } - @Override public int hashCode() { final int prime = 31; diff --git a/core/src/main/java/org/jclouds/io/payloads/DelegatingPayload.java b/core/src/main/java/org/jclouds/io/payloads/DelegatingPayload.java index 00db330cf1..9c4a301abd 100644 --- a/core/src/main/java/org/jclouds/io/payloads/DelegatingPayload.java +++ b/core/src/main/java/org/jclouds/io/payloads/DelegatingPayload.java @@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import com.google.common.base.Throwables; @@ -75,14 +74,6 @@ public class DelegatingPayload implements Payload { return delegate.isRepeatable(); } - /** - * {@inheritDoc} - */ - @Override - public void writeTo(OutputStream outstream) throws IOException { - delegate.writeTo(outstream); - } - /** * {@inheritDoc} */ diff --git a/core/src/main/java/org/jclouds/io/payloads/PhantomPayload.java b/core/src/main/java/org/jclouds/io/payloads/PhantomPayload.java index 4023ee1083..e30e6bf11e 100644 --- a/core/src/main/java/org/jclouds/io/payloads/PhantomPayload.java +++ b/core/src/main/java/org/jclouds/io/payloads/PhantomPayload.java @@ -18,9 +18,7 @@ package org.jclouds.io.payloads; import static com.google.common.base.Preconditions.checkNotNull; -import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import org.jclouds.io.ContentMetadata; import org.jclouds.io.MutableContentMetadata; @@ -49,10 +47,4 @@ public class PhantomPayload extends BasePayload { public InputStream openStream() { throw new UnsupportedOperationException(); } - - @Override - public void writeTo(OutputStream outstream) throws IOException { - throw new UnsupportedOperationException(); - } - } diff --git a/core/src/test/java/org/jclouds/io/payloads/MultipartFormTest.java b/core/src/test/java/org/jclouds/io/payloads/MultipartFormTest.java index 9121c1284e..f568d36453 100644 --- a/core/src/test/java/org/jclouds/io/payloads/MultipartFormTest.java +++ b/core/src/test/java/org/jclouds/io/payloads/MultipartFormTest.java @@ -25,7 +25,6 @@ import static org.testng.Assert.assertEquals; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import org.jclouds.io.Payloads; import org.jclouds.io.payloads.Part.PartOptions; @@ -82,12 +81,6 @@ public class MultipartFormTest { public boolean isRepeatable() { return realPayload.isRepeatable(); } - - @Override - public void writeTo(OutputStream outstream) throws IOException { - realPayload.writeTo(outstream); - } - } private Part newPart(String data) { diff --git a/drivers/apachehc/src/main/java/org/jclouds/http/apachehc/ApacheHCUtils.java b/drivers/apachehc/src/main/java/org/jclouds/http/apachehc/ApacheHCUtils.java index 47d790318d..2872010bf0 100644 --- a/drivers/apachehc/src/main/java/org/jclouds/http/apachehc/ApacheHCUtils.java +++ b/drivers/apachehc/src/main/java/org/jclouds/http/apachehc/ApacheHCUtils.java @@ -19,7 +19,6 @@ package org.jclouds.http.apachehc; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Map; import java.util.Set; @@ -198,12 +197,6 @@ public class ApacheHCUtils { } catch (IOException e) { } } - - @Override - public void writeTo(OutputStream outstream) throws IOException { - super.writeTo(outstream); - } - } } diff --git a/drivers/gae/src/main/java/org/jclouds/gae/ConvertToGaeRequest.java b/drivers/gae/src/main/java/org/jclouds/gae/ConvertToGaeRequest.java index e5ec8b5b31..31ca421bdd 100644 --- a/drivers/gae/src/main/java/org/jclouds/gae/ConvertToGaeRequest.java +++ b/drivers/gae/src/main/java/org/jclouds/gae/ConvertToGaeRequest.java @@ -18,7 +18,6 @@ package org.jclouds.gae; import static com.google.appengine.api.urlfetch.FetchOptions.Builder.disallowTruncate; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -43,6 +42,7 @@ import com.google.appengine.api.urlfetch.HTTPRequest; import com.google.appengine.repackaged.com.google.common.base.Throwables; import com.google.common.base.Function; import com.google.common.collect.ImmutableSet; +import com.google.common.io.ByteStreams; /** * @@ -98,9 +98,7 @@ public class ConvertToGaeRequest implements Function { if (request.getPayload() != null) { InputStream input = request.getPayload().getInput(); try { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - request.getPayload().writeTo(out); - byte[] array = out.toByteArray(); + byte[] array = ByteStreams.toByteArray(input); if (!request.getPayload().isRepeatable()) { Payload oldPayload = request.getPayload(); request.setPayload(array);