Issue 301: added Closeable to payload so that it works better in clojure

This commit is contained in:
Adrian Cole 2010-07-13 18:27:45 -05:00
parent 541ccdae51
commit 1274a25e17
4 changed files with 64 additions and 8 deletions

View File

@ -0,0 +1,41 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.aws.s3.blobstore.functions;
import static org.testng.Assert.assertEquals;
import org.jclouds.blobstore.functions.BlobToHttpGetOptions;
import org.jclouds.http.options.GetOptions;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
@Test(groups = "unit", testName = "s3.BlobToHttpGetOptionsTest")
public class BlobToHttpGetOptionsTest {
@Test
void testOneRange() {
BlobToHttpGetOptions converter = new BlobToHttpGetOptions();
org.jclouds.blobstore.options.GetOptions blobGet = new org.jclouds.blobstore.options.GetOptions()
.range(2, 5);
GetOptions httpGet = converter.apply(blobGet);
assertEquals(httpGet.buildRequestHeaders().get("Range"), ImmutableSet
.of("bytes=2-5"));
}
}

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.http;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -29,7 +30,7 @@ import com.google.common.io.InputSupplier;
/**
* @author Adrian Cole
*/
public interface Payload extends InputSupplier<InputStream> {
public interface Payload extends InputSupplier<InputStream>, Closeable{
/**
* Creates a new InputStream object of the payload.

View File

@ -41,10 +41,11 @@ public abstract class BasePayload<V> implements Payload {
protected byte[] contentMD5;
protected transient volatile boolean written;
protected BasePayload(V content, @Nullable String contentType, @Nullable Long contentLength,
@Nullable byte[] contentMD5) {
protected BasePayload(V content, @Nullable String contentType,
@Nullable Long contentLength, @Nullable byte[] contentMD5) {
this.content = checkNotNull(content, "content");
this.contentType = contentType == null ? "application/unknown" : contentType;
this.contentType = contentType == null ? "application/unknown"
: contentType;
this.contentLength = contentLength;
if (contentMD5 != null)
setContentMD5(contentMD5);
@ -121,7 +122,8 @@ public abstract class BasePayload<V> implements Payload {
*/
@Override
public void writeTo(OutputStream outstream) throws IOException {
checkState(!written || isRepeatable(), "can only be writted to an outputstream once");
checkState(!written || isRepeatable(),
"can only be writted to an outputstream once");
written = true;
InputStream in = getInput();
try {
@ -158,9 +160,9 @@ public abstract class BasePayload<V> implements Payload {
@Override
public String toString() {
return "[content=" + (content != null) + ", contentLength=" + contentLength + ", contentMD5="
+ (contentMD5 != null) + ", contentType=" + contentType + ", written=" + written
+ "]";
return "[content=" + (content != null) + ", contentLength="
+ contentLength + ", contentMD5=" + (contentMD5 != null)
+ ", contentType=" + contentType + ", written=" + written + "]";
}
/**
@ -178,4 +180,11 @@ public abstract class BasePayload<V> implements Payload {
public void release() {
}
/**
* Delegates to release()
*/
@Override
public void close() {
release();
}
}

View File

@ -143,4 +143,9 @@ public class DelegatingPayload implements Payload {
public void release() {
delegate.release();
}
@Override
public void close() throws IOException {
delegate.close();
}
}