Remove WriteTo

Guava helpers capture this functionality in a more idiomatic way.
This commit is contained in:
Andrew Gaul 2014-02-19 13:21:26 -08:00
parent 9965fbcadb
commit d7e0b3b10c
12 changed files with 9 additions and 104 deletions

View File

@ -22,7 +22,6 @@ import static org.testng.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
@ -148,7 +147,7 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
blobStore.putBlob(container, write, PutOptions.Builder.multipart()); blobStore.putBlob(container, write, PutOptions.Builder.multipart());
Blob read = blobStore.getBlob(container, "const.txt"); 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())); assertEquals(Files.hash(outFile, Hashing.md5()), Files.hash(inFile, Hashing.md5()));

View File

@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base16; import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.http.Uris.uriBuilder; import static org.jclouds.http.Uris.uriBuilder;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
@ -48,6 +47,7 @@ import com.google.common.base.Supplier;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimaps; import com.google.common.collect.Multimaps;
import com.google.common.io.ByteStreams;
public class TransientStorageStrategy implements LocalStorageStrategy { public class TransientStorageStrategy implements LocalStorageStrategy {
private final ConcurrentMap<String, ConcurrentMap<String, Blob>> containerToBlobs = new ConcurrentHashMap<String, ConcurrentMap<String, Blob>>(); private final ConcurrentMap<String, ConcurrentMap<String, Blob>> containerToBlobs = new ConcurrentHashMap<String, ConcurrentMap<String, Blob>>();
@ -158,9 +158,8 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
try { try {
if (payload == null || !(payload instanceof ByteArrayPayload)) { if (payload == null || !(payload instanceof ByteArrayPayload)) {
MutableContentMetadata oldMd = in.getPayload().getContentMetadata(); MutableContentMetadata oldMd = in.getPayload().getContentMetadata();
ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] out = ByteStreams.toByteArray(in.getPayload());
in.getPayload().writeTo(out); payload = (ByteArrayPayload) Payloads.calculateMD5(Payloads.newPayload(out));
payload = (ByteArrayPayload) Payloads.calculateMD5(Payloads.newPayload(out.toByteArray()));
HttpUtils.copy(oldMd, payload.getContentMetadata()); HttpUtils.copy(oldMd, payload.getContentMetadata());
} else { } else {
if (payload.getContentMetadata().getContentMD5() == null) if (payload.getContentMetadata().getContentMD5() == null)

View File

@ -60,6 +60,7 @@ import com.google.common.base.Function;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder; import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.io.ByteStreams;
import com.google.common.io.CountingOutputStream; import com.google.common.io.CountingOutputStream;
import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -304,7 +305,7 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
connection.setDoOutput(true); connection.setDoOutput(true);
CountingOutputStream out = new CountingOutputStream(connection.getOutputStream()); CountingOutputStream out = new CountingOutputStream(connection.getOutputStream());
try { try {
payload.writeTo(out); ByteStreams.copy(payload, out);
} catch (IOException e) { } catch (IOException e) {
logger.error(e, "error after writing %d/%s bytes to %s", out.getCount(), lengthDesc, connection.getURL()); logger.error(e, "error after writing %d/%s bytes to %s", out.getCount(), lengthDesc, connection.getURL());
throw e; throw e;

View File

@ -25,7 +25,7 @@ import com.google.common.io.InputSupplier;
/** /**
* @author Adrian Cole * @author Adrian Cole
*/ */
public interface Payload extends InputSupplier<InputStream>, WriteTo, Closeable { public interface Payload extends InputSupplier<InputStream>, Closeable {
/** /**
* Creates a new InputStream object of the payload. * Creates a new InputStream object of the payload.

View File

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

View File

@ -19,12 +19,10 @@ package org.jclouds.io.payloads;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.security.Key; import java.security.Key;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.CipherInputStream; import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
@ -47,9 +45,4 @@ public abstract class BaseCipherPayload extends DelegatingPayload {
public CipherInputStream openStream() throws IOException { public CipherInputStream openStream() throws IOException {
return new CipherInputStream(super.openStream(), initializeCipher(key)); return new CipherInputStream(super.openStream(), initializeCipher(key));
} }
@Override
public void writeTo(OutputStream outstream) throws IOException {
super.writeTo(new CipherOutputStream(outstream, initializeCipher(key)));
}
} }

View File

@ -17,13 +17,9 @@
package org.jclouds.io.payloads; package org.jclouds.io.payloads;
import static com.google.common.base.Preconditions.checkNotNull; 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
@ -64,22 +60,6 @@ public abstract class BasePayload<V> implements Payload {
return content; 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 @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;

View File

@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
@ -75,14 +74,6 @@ public class DelegatingPayload implements Payload {
return delegate.isRepeatable(); return delegate.isRepeatable();
} }
/**
* {@inheritDoc}
*/
@Override
public void writeTo(OutputStream outstream) throws IOException {
delegate.writeTo(outstream);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -18,9 +18,7 @@ package org.jclouds.io.payloads;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import org.jclouds.io.ContentMetadata; import org.jclouds.io.ContentMetadata;
import org.jclouds.io.MutableContentMetadata; import org.jclouds.io.MutableContentMetadata;
@ -49,10 +47,4 @@ public class PhantomPayload extends BasePayload<Object> {
public InputStream openStream() { public InputStream openStream() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void writeTo(OutputStream outstream) throws IOException {
throw new UnsupportedOperationException();
}
} }

View File

@ -25,7 +25,6 @@ import static org.testng.Assert.assertEquals;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import org.jclouds.io.Payloads; import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.Part.PartOptions; import org.jclouds.io.payloads.Part.PartOptions;
@ -82,12 +81,6 @@ public class MultipartFormTest {
public boolean isRepeatable() { public boolean isRepeatable() {
return realPayload.isRepeatable(); return realPayload.isRepeatable();
} }
@Override
public void writeTo(OutputStream outstream) throws IOException {
realPayload.writeTo(outstream);
}
} }
private Part newPart(String data) { private Part newPart(String data) {

View File

@ -19,7 +19,6 @@ package org.jclouds.http.apachehc;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -198,12 +197,6 @@ public class ApacheHCUtils {
} catch (IOException e) { } catch (IOException e) {
} }
} }
@Override
public void writeTo(OutputStream outstream) throws IOException {
super.writeTo(outstream);
}
} }
} }

View File

@ -18,7 +18,6 @@ package org.jclouds.gae;
import static com.google.appengine.api.urlfetch.FetchOptions.Builder.disallowTruncate; import static com.google.appengine.api.urlfetch.FetchOptions.Builder.disallowTruncate;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; 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.appengine.repackaged.com.google.common.base.Throwables;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
/** /**
* *
@ -98,9 +98,7 @@ public class ConvertToGaeRequest implements Function<HttpRequest, HTTPRequest> {
if (request.getPayload() != null) { if (request.getPayload() != null) {
InputStream input = request.getPayload().getInput(); InputStream input = request.getPayload().getInput();
try { try {
ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] array = ByteStreams.toByteArray(input);
request.getPayload().writeTo(out);
byte[] array = out.toByteArray();
if (!request.getPayload().isRepeatable()) { if (!request.getPayload().isRepeatable()) {
Payload oldPayload = request.getPayload(); Payload oldPayload = request.getPayload();
request.setPayload(array); request.setPayload(array);