|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.entity; |
|
29 |
|
30 import java.io.IOException; |
|
31 import java.io.InputStream; |
|
32 import java.io.OutputStream; |
|
33 |
|
34 /** |
|
35 * A generic streamed, non-repeatable entity that obtains its content |
|
36 * from an {@link InputStream}. |
|
37 * |
|
38 * @since 4.0 |
|
39 */ |
|
40 public class BasicHttpEntity extends AbstractHttpEntity { |
|
41 |
|
42 private InputStream content; |
|
43 private long length; |
|
44 |
|
45 /** |
|
46 * Creates a new basic entity. |
|
47 * The content is initially missing, the content length |
|
48 * is set to a negative number. |
|
49 */ |
|
50 public BasicHttpEntity() { |
|
51 super(); |
|
52 this.length = -1; |
|
53 } |
|
54 |
|
55 public long getContentLength() { |
|
56 return this.length; |
|
57 } |
|
58 |
|
59 /** |
|
60 * Obtains the content, once only. |
|
61 * |
|
62 * @return the content, if this is the first call to this method |
|
63 * since {@link #setContent setContent} has been called |
|
64 * |
|
65 * @throws IllegalStateException |
|
66 * if the content has not been provided |
|
67 */ |
|
68 public InputStream getContent() throws IllegalStateException { |
|
69 if (this.content == null) { |
|
70 throw new IllegalStateException("Content has not been provided"); |
|
71 } |
|
72 return this.content; |
|
73 |
|
74 } |
|
75 |
|
76 /** |
|
77 * Tells that this entity is not repeatable. |
|
78 * |
|
79 * @return <code>false</code> |
|
80 */ |
|
81 public boolean isRepeatable() { |
|
82 return false; |
|
83 } |
|
84 |
|
85 /** |
|
86 * Specifies the length of the content. |
|
87 * |
|
88 * @param len the number of bytes in the content, or |
|
89 * a negative number to indicate an unknown length |
|
90 */ |
|
91 public void setContentLength(long len) { |
|
92 this.length = len; |
|
93 } |
|
94 |
|
95 /** |
|
96 * Specifies the content. |
|
97 * |
|
98 * @param instream the stream to return with the next call to |
|
99 * {@link #getContent getContent} |
|
100 */ |
|
101 public void setContent(final InputStream instream) { |
|
102 this.content = instream; |
|
103 } |
|
104 |
|
105 public void writeTo(final OutputStream outstream) throws IOException { |
|
106 if (outstream == null) { |
|
107 throw new IllegalArgumentException("Output stream may not be null"); |
|
108 } |
|
109 InputStream instream = getContent(); |
|
110 try { |
|
111 int l; |
|
112 byte[] tmp = new byte[2048]; |
|
113 while ((l = instream.read(tmp)) != -1) { |
|
114 outstream.write(tmp, 0, l); |
|
115 } |
|
116 } finally { |
|
117 instream.close(); |
|
118 } |
|
119 } |
|
120 |
|
121 public boolean isStreaming() { |
|
122 return this.content != null; |
|
123 } |
|
124 |
|
125 /** |
|
126 * Closes the content InputStream. |
|
127 * |
|
128 * @deprecated Either use {@link #getContent()} and call {@link java.io.InputStream#close()} on that; |
|
129 * otherwise call {@link #writeTo(OutputStream)} which is required to free the resources. |
|
130 */ |
|
131 public void consumeContent() throws IOException { |
|
132 if (content != null) { |
|
133 content.close(); // reads to the end of the entity |
|
134 } |
|
135 } |
|
136 |
|
137 } |