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 org.apache.http.entity;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33
34 import org.apache.http.annotation.NotThreadSafe;
35 import org.apache.http.util.Args;
36 import org.apache.http.util.Asserts;
37
38 /**
39 * A generic streamed, non-repeatable entity that obtains its content
40 * from an {@link InputStream}.
41 *
42 * @since 4.0
43 */
44 @NotThreadSafe
45 public class BasicHttpEntity extends AbstractHttpEntity {
46
47 private InputStream content;
48 private long length;
49
50 /**
51 * Creates a new basic entity.
52 * The content is initially missing, the content length
53 * is set to a negative number.
54 */
55 public BasicHttpEntity() {
56 super();
57 this.length = -1;
58 }
59
60 public long getContentLength() {
61 return this.length;
62 }
63
64 /**
65 * Obtains the content, once only.
66 *
67 * @return the content, if this is the first call to this method
68 * since {@link #setContent setContent} has been called
69 *
70 * @throws IllegalStateException
71 * if the content has not been provided
72 */
73 public InputStream getContent() throws IllegalStateException {
74 Asserts.check(this.content != null, "Content has not been provided");
75 return this.content;
76 }
77
78 /**
79 * Tells that this entity is not repeatable.
80 *
81 * @return <code>false</code>
82 */
83 public boolean isRepeatable() {
84 return false;
85 }
86
87 /**
88 * Specifies the length of the content.
89 *
90 * @param len the number of bytes in the content, or
91 * a negative number to indicate an unknown length
92 */
93 public void setContentLength(final long len) {
94 this.length = len;
95 }
96
97 /**
98 * Specifies the content.
99 *
100 * @param instream the stream to return with the next call to
101 * {@link #getContent getContent}
102 */
103 public void setContent(final InputStream instream) {
104 this.content = instream;
105 }
106
107 public void writeTo(final OutputStream outstream) throws IOException {
108 Args.notNull(outstream, "Output stream");
109 final InputStream instream = getContent();
110 try {
111 int l;
112 final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE];
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 (4.1) 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 @Deprecated
132 @Override
133 public void consumeContent() throws IOException {
134 if (content != null) {
135 content.close(); // reads to the end of the entity
136 }
137 }
138
139 }