1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.io.payloads; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.io.IOException; |
24 | import java.io.InputStream; |
25 | import java.io.OutputStream; |
26 | |
27 | import org.jclouds.io.MutableContentMetadata; |
28 | import org.jclouds.io.Payload; |
29 | import org.jclouds.io.WriteTo; |
30 | |
31 | /** |
32 | * Note that not all services accept streaming payloads. For example, Rackspace CloudFiles accepts |
33 | * streaming while Amazon S3 does not. |
34 | * |
35 | * @author Adrian Cole |
36 | */ |
37 | public class StreamingPayload implements Payload { |
38 | |
39 | protected transient volatile boolean written; |
40 | protected final WriteTo writeTo; |
41 | protected MutableContentMetadata contentMetadata; |
42 | |
43 | public StreamingPayload(WriteTo writeTo) { |
44 | this(writeTo, new BaseMutableContentMetadata()); |
45 | } |
46 | |
47 | protected StreamingPayload(WriteTo writeTo, MutableContentMetadata contentMetadata) { |
48 | this.writeTo = checkNotNull(writeTo, "writeTo"); |
49 | this.contentMetadata = checkNotNull(contentMetadata, "contentMetadata"); |
50 | } |
51 | |
52 | /** |
53 | * @throws UnsupportedOperationException |
54 | * this payload is for streaming writes only |
55 | */ |
56 | @Override |
57 | public Object getRawContent() { |
58 | throw new UnsupportedOperationException("this payload is for streaming writes only"); |
59 | } |
60 | |
61 | /** |
62 | * @throws UnsupportedOperationException |
63 | * this payload is for streaming writes only |
64 | */ |
65 | @Override |
66 | public InputStream getInput() { |
67 | throw new UnsupportedOperationException("this payload is for streaming writes only"); |
68 | } |
69 | |
70 | /** |
71 | * {@inheritDoc} |
72 | */ |
73 | @Override |
74 | public void writeTo(OutputStream outstream) throws IOException { |
75 | writeTo.writeTo(outstream); |
76 | } |
77 | |
78 | @Override |
79 | public String toString() { |
80 | return "[contentMetadata=" + contentMetadata + ", written=" + written + "]"; |
81 | } |
82 | |
83 | /** |
84 | * By default we are not repeatable. |
85 | */ |
86 | @Override |
87 | public boolean isRepeatable() { |
88 | return true; |
89 | } |
90 | |
91 | @Override |
92 | public int hashCode() { |
93 | final int prime = 31; |
94 | int result = 1; |
95 | result = prime * result + ((contentMetadata == null) ? 0 : contentMetadata.hashCode()); |
96 | return result; |
97 | } |
98 | |
99 | @Override |
100 | public boolean equals(Object obj) { |
101 | if (this == obj) |
102 | return true; |
103 | if (obj == null) |
104 | return false; |
105 | if (getClass() != obj.getClass()) |
106 | return false; |
107 | StreamingPayload other = (StreamingPayload) obj; |
108 | if (contentMetadata == null) { |
109 | if (other.contentMetadata != null) |
110 | return false; |
111 | } else if (!contentMetadata.equals(other.contentMetadata)) |
112 | return false; |
113 | return true; |
114 | } |
115 | |
116 | /** |
117 | * By default there are no resources to release. |
118 | */ |
119 | @Override |
120 | public void release() { |
121 | } |
122 | |
123 | /** |
124 | * Delegates to release() |
125 | */ |
126 | @Override |
127 | public void close() { |
128 | release(); |
129 | } |
130 | |
131 | /** |
132 | * {@inheritDoc} |
133 | */ |
134 | @Override |
135 | public MutableContentMetadata getContentMetadata() { |
136 | return contentMetadata; |
137 | } |
138 | |
139 | @Override |
140 | public void setContentMetadata(MutableContentMetadata in) { |
141 | contentMetadata = in; |
142 | } |
143 | } |