mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/BufferedHttpEntity.java

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:2505352f20fd
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.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34
35 import ch.boye.httpclientandroidlib.HttpEntity;
36 import ch.boye.httpclientandroidlib.util.EntityUtils;
37
38 /**
39 * A wrapping entity that buffers it content if necessary.
40 * The buffered entity is always repeatable.
41 * If the wrapped entity is repeatable itself, calls are passed through.
42 * If the wrapped entity is not repeatable, the content is read into a
43 * buffer once and provided from there as often as required.
44 *
45 * @since 4.0
46 */
47 public class BufferedHttpEntity extends HttpEntityWrapper {
48
49 private final byte[] buffer;
50
51 /**
52 * Creates a new buffered entity wrapper.
53 *
54 * @param entity the entity to wrap, not null
55 * @throws IllegalArgumentException if wrapped is null
56 */
57 public BufferedHttpEntity(final HttpEntity entity) throws IOException {
58 super(entity);
59 if (!entity.isRepeatable() || entity.getContentLength() < 0) {
60 this.buffer = EntityUtils.toByteArray(entity);
61 } else {
62 this.buffer = null;
63 }
64 }
65
66 public long getContentLength() {
67 if (this.buffer != null) {
68 return this.buffer.length;
69 } else {
70 return wrappedEntity.getContentLength();
71 }
72 }
73
74 public InputStream getContent() throws IOException {
75 if (this.buffer != null) {
76 return new ByteArrayInputStream(this.buffer);
77 } else {
78 return wrappedEntity.getContent();
79 }
80 }
81
82 /**
83 * Tells that this entity does not have to be chunked.
84 *
85 * @return <code>false</code>
86 */
87 public boolean isChunked() {
88 return (buffer == null) && wrappedEntity.isChunked();
89 }
90
91 /**
92 * Tells that this entity is repeatable.
93 *
94 * @return <code>true</code>
95 */
96 public boolean isRepeatable() {
97 return true;
98 }
99
100
101 public void writeTo(final OutputStream outstream) throws IOException {
102 if (outstream == null) {
103 throw new IllegalArgumentException("Output stream may not be null");
104 }
105 if (this.buffer != null) {
106 outstream.write(this.buffer);
107 } else {
108 wrappedEntity.writeTo(outstream);
109 }
110 }
111
112
113 // non-javadoc, see interface HttpEntity
114 public boolean isStreaming() {
115 return (buffer == null) && wrappedEntity.isStreaming();
116 }
117
118 } // class BufferedHttpEntity

mercurial