diff --git a/blobstore/src/main/java/org/jclouds/blobstore/domain/BlobBuilder.java b/blobstore/src/main/java/org/jclouds/blobstore/domain/BlobBuilder.java index ed42cd8599..ad27986666 100644 --- a/blobstore/src/main/java/org/jclouds/blobstore/domain/BlobBuilder.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/domain/BlobBuilder.java @@ -25,6 +25,7 @@ import java.util.Map; import org.jclouds.blobstore.domain.internal.BlobBuilderImpl; import org.jclouds.io.Payload; +import com.google.common.io.ByteSource; import com.google.inject.ImplementedBy; /** @@ -77,21 +78,32 @@ public interface BlobBuilder { * * @param payload * payload you wish to construct the {@link Blob} with. + * @deprecated see payload(ByteSource.wrap(byte[]) */ + @Deprecated PayloadBlobBuilder payload(byte[] payload); + /** + * @param payload payload you wish to construct the {@link Blob} with. + */ + PayloadBlobBuilder payload(ByteSource byteSource); + /** * * @param payload * payload you wish to construct the {@link Blob} with. + * @deprecated see payload(ByteSource.wrap(String.getBytes())) */ + @Deprecated PayloadBlobBuilder payload(String payload); /** * * @param payload * payload you wish to construct the {@link Blob} with. + * @deprecated see payload(Files.asByteSource(File)) */ + @Deprecated PayloadBlobBuilder payload(File payload); /** diff --git a/blobstore/src/main/java/org/jclouds/blobstore/domain/internal/BlobBuilderImpl.java b/blobstore/src/main/java/org/jclouds/blobstore/domain/internal/BlobBuilderImpl.java index 2069220abf..62a81e1bda 100644 --- a/blobstore/src/main/java/org/jclouds/blobstore/domain/internal/BlobBuilderImpl.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/domain/internal/BlobBuilderImpl.java @@ -35,6 +35,7 @@ import org.jclouds.io.Payloads; import org.jclouds.io.payloads.PhantomPayload; import com.google.common.collect.Maps; +import com.google.common.io.ByteSource; /** * @author Adrian Cole @@ -89,6 +90,11 @@ public class BlobBuilderImpl implements BlobBuilder { return payload(newPayload(checkNotNull(data, "data"))); } + @Override + public PayloadBlobBuilder payload(ByteSource data) { + return payload(newPayload(checkNotNull(data, "data"))); + } + /** * {@inheritDoc} */ @@ -161,6 +167,11 @@ public class BlobBuilderImpl implements BlobBuilder { return builder.payload(payload); } + @Override + public PayloadBlobBuilder payload(ByteSource payload) { + return builder.payload(payload); + } + @Override public PayloadBlobBuilder payload(String payload) { return builder.payload(payload); diff --git a/core/src/main/java/org/jclouds/http/HttpMessage.java b/core/src/main/java/org/jclouds/http/HttpMessage.java index 014a252bdc..4e3102dfa5 100644 --- a/core/src/main/java/org/jclouds/http/HttpMessage.java +++ b/core/src/main/java/org/jclouds/http/HttpMessage.java @@ -33,6 +33,7 @@ import com.google.common.base.Objects; import com.google.common.base.Objects.ToStringHelper; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Multimap; +import com.google.common.io.ByteSource; /** * Represents a request that can be executed within {@link HttpCommandExecutorService} @@ -65,15 +66,28 @@ public class HttpMessage extends PayloadEnclosingImpl { /** * @see HttpMessage#getPayload() + * @deprecated see payload(ByteSource.wrap(byte[])) */ + @Deprecated public T payload(byte [] payload) { this.payload = Payloads.newByteArrayPayload(checkNotNull(payload, "payload")); return self(); } + + /** + * @see HttpMessage#getPayload() + */ + @Deprecated + public T payload(ByteSource payload) { + this.payload = Payloads.newByteSourcePayload(checkNotNull(payload, "payload")); + return self(); + } /** * @see HttpMessage#getPayload() + * @deprecated see payload(Files.asByteSource(File)) */ + @Deprecated public T payload(File payload) { this.payload = Payloads.newFilePayload(checkNotNull(payload, "payload")); return self(); @@ -89,7 +103,9 @@ public class HttpMessage extends PayloadEnclosingImpl { /** * @see HttpMessage#getPayload() + * @deprecated see payload(ByteSource.wrap(String.getBytes())) */ + @Deprecated public T payload(String payload) { this.payload = Payloads.newStringPayload(checkNotNull(payload, "payload")); return self(); diff --git a/core/src/main/java/org/jclouds/http/HttpUtils.java b/core/src/main/java/org/jclouds/http/HttpUtils.java index 592205a3a2..6e41625cf9 100644 --- a/core/src/main/java/org/jclouds/http/HttpUtils.java +++ b/core/src/main/java/org/jclouds/http/HttpUtils.java @@ -64,6 +64,7 @@ import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; import com.google.common.collect.ImmutableSet.Builder; +import com.google.common.io.ByteSource; import com.google.common.reflect.Invokable; import com.google.inject.Inject; @@ -161,7 +162,7 @@ public class HttpUtils { public static byte[] closeClientButKeepContentStream(PayloadEnclosing response) { byte[] returnVal = toByteArrayOrNull(response); if (returnVal != null && !response.getPayload().isRepeatable()) { - Payload newPayload = Payloads.newByteArrayPayload(returnVal); + Payload newPayload = Payloads.newByteSourcePayload(ByteSource.wrap(returnVal)); MutableContentMetadata fromMd = response.getPayload().getContentMetadata(); MutableContentMetadata toMd = newPayload.getContentMetadata(); copy(fromMd, toMd); diff --git a/core/src/main/java/org/jclouds/io/Payloads.java b/core/src/main/java/org/jclouds/io/Payloads.java index 620a5144f7..f322d76ec0 100644 --- a/core/src/main/java/org/jclouds/io/Payloads.java +++ b/core/src/main/java/org/jclouds/io/Payloads.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.InputStream; import org.jclouds.io.payloads.ByteArrayPayload; +import org.jclouds.io.payloads.ByteSourcePayload; import org.jclouds.io.payloads.FilePayload; import org.jclouds.io.payloads.InputStreamPayload; import org.jclouds.io.payloads.StringPayload; @@ -31,6 +32,7 @@ import org.jclouds.io.payloads.UrlEncodedFormPayload; import com.google.common.collect.Multimap; import com.google.common.io.ByteStreams; +import com.google.common.io.ByteSource; /** * @@ -40,6 +42,10 @@ public class Payloads { private Payloads() { } + /** + * @deprecated see newPayload(ByteSource) or newPayload(InputStream) + */ + @Deprecated public static Payload newPayload(Object data) { checkNotNull(data, "data"); if (data instanceof Payload) { @@ -48,6 +54,8 @@ public class Payloads { return newInputStreamPayload((InputStream) data); } else if (data instanceof byte[]) { return newByteArrayPayload((byte[]) data); + } else if (data instanceof ByteSource) { + return newByteSourcePayload((ByteSource) data); } else if (data instanceof String) { return newStringPayload((String) data); } else if (data instanceof File) { @@ -61,14 +69,30 @@ public class Payloads { return new InputStreamPayload(checkNotNull(data, "data")); } + /** + * @deprecated see newPayload(ByteSource) + */ + @Deprecated public static ByteArrayPayload newByteArrayPayload(byte[] data) { return new ByteArrayPayload(checkNotNull(data, "data")); } + public static ByteSourcePayload newByteSourcePayload(ByteSource data) { + return new ByteSourcePayload(checkNotNull(data, "data")); + } + + /** + * @deprecated see newPayload(ByteSource) + */ + @Deprecated public static StringPayload newStringPayload(String data) { return new StringPayload(checkNotNull(data, "data")); } + /** + * @deprecated see newPayload(ByteSource) + */ + @Deprecated public static FilePayload newFilePayload(File data) { return new FilePayload(checkNotNull(data, "data")); } diff --git a/core/src/main/java/org/jclouds/io/payloads/ByteArrayPayload.java b/core/src/main/java/org/jclouds/io/payloads/ByteArrayPayload.java index 68aeaf78e9..3b8a23e738 100644 --- a/core/src/main/java/org/jclouds/io/payloads/ByteArrayPayload.java +++ b/core/src/main/java/org/jclouds/io/payloads/ByteArrayPayload.java @@ -24,7 +24,9 @@ import java.io.InputStream; /** * @author Adrian Cole + * @deprecated see ByteSourcePayload */ +@Deprecated public class ByteArrayPayload extends BasePayload { public ByteArrayPayload(byte[] content) { this(content, null); diff --git a/core/src/main/java/org/jclouds/io/payloads/ByteSourcePayload.java b/core/src/main/java/org/jclouds/io/payloads/ByteSourcePayload.java new file mode 100644 index 0000000000..e6a111d785 --- /dev/null +++ b/core/src/main/java/org/jclouds/io/payloads/ByteSourcePayload.java @@ -0,0 +1,56 @@ +/* + * 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.payloads; + +import static com.google.common.io.Closeables.closeQuietly; + +import java.io.IOException; +import java.io.InputStream; + +import com.google.common.io.ByteSource; +import com.google.common.io.Closer; + +/** + * A repeatable, ByteSource-backed Payload. + * + * @author Andrew Gaul + */ +public class ByteSourcePayload extends BasePayload { + private final Closer closer = Closer.create(); + + public ByteSourcePayload(ByteSource content) { + super(content); + } + + @Override + public InputStream openStream() throws IOException { + return closer.register(content.openStream()); + } + + @Override + public boolean isRepeatable() { + return true; + } + + /** + * if we created the stream, then it is already consumed on close. + */ + @Override + public void release() { + closeQuietly(closer); + } +} diff --git a/core/src/main/java/org/jclouds/io/payloads/FilePayload.java b/core/src/main/java/org/jclouds/io/payloads/FilePayload.java index 430f5d8529..a0b0133f19 100644 --- a/core/src/main/java/org/jclouds/io/payloads/FilePayload.java +++ b/core/src/main/java/org/jclouds/io/payloads/FilePayload.java @@ -26,7 +26,9 @@ import java.io.IOException; /** * @author Adrian Cole + * @deprecated see ByteSourcePayload */ +@Deprecated public class FilePayload extends BasePayload { public FilePayload(File content) { diff --git a/core/src/main/java/org/jclouds/io/payloads/InputStreamSupplierPayload.java b/core/src/main/java/org/jclouds/io/payloads/InputStreamSupplierPayload.java index 54f0123be7..b962840c7c 100644 --- a/core/src/main/java/org/jclouds/io/payloads/InputStreamSupplierPayload.java +++ b/core/src/main/java/org/jclouds/io/payloads/InputStreamSupplierPayload.java @@ -20,16 +20,17 @@ import static com.google.common.io.Closeables.closeQuietly; import java.io.IOException; import java.io.InputStream; -import java.util.List; -import com.google.common.collect.Lists; +import com.google.common.io.Closer; import com.google.common.io.InputSupplier; /** * @author Adrian Cole + * @deprecated see ByteSourcePayload */ +@Deprecated public class InputStreamSupplierPayload extends BasePayload> { - private List toClose = Lists.newArrayList(); + private final Closer closer = Closer.create(); public InputStreamSupplierPayload(InputSupplier content) { super(content); @@ -40,9 +41,7 @@ public class InputStreamSupplierPayload extends BasePayload 0) - for (InputStream content = toClose.remove(0); toClose.size() > 0; content = toClose.remove(0)) - closeQuietly(content); + closeQuietly(closer); } - } diff --git a/core/src/main/java/org/jclouds/io/payloads/StringPayload.java b/core/src/main/java/org/jclouds/io/payloads/StringPayload.java index 2452a944b9..70ea1bea06 100644 --- a/core/src/main/java/org/jclouds/io/payloads/StringPayload.java +++ b/core/src/main/java/org/jclouds/io/payloads/StringPayload.java @@ -26,7 +26,9 @@ import com.google.common.base.Charsets; * a different encoding, please use {@link ByteArrayPayload}. * * @author Adrian Cole + * @deprecated see ByteSourcePayload */ +@Deprecated public class StringPayload extends BasePayload { private final byte[] bytes;