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
36 /**
37 * A generic streamed, non-repeatable entity that obtains its content
38 * from an {@link InputStream}.
39 *
40 * @since 4.0
41 */
42 @NotThreadSafe
43 public class BasicHttpEntity extends AbstractHttpEntity {
44
45 private InputStream content;
46 private long length;
47
48 /**
49 * Creates a new basic entity.
50 * The content is initially missing, the content length
51 * is set to a negative number.
52 */
53 public BasicHttpEntity() {
54 super();
55 this.length = -1;
56 }
57
58 public long getContentLength() {
59 return this.length;
60 }
61
62 /**
63 * Obtains the content, once only.
64 *
65 * @return the content, if this is the first call to this method
66 * since {@link #setContent setContent} has been called
67 *
68 * @throws IllegalStateException
69 * if the content has not been provided
70 */
71 public InputStream getContent() throws IllegalStateException {
72 if (this.content == null) {
73 throw new IllegalStateException("Content has not been provided");
74 }
75 return this.content;
76
77 }
78
79 /**
80 * Tells that this entity is not repeatable.
81 *
82 * @return <code>false</code>
83 */
84 public boolean isRepeatable() {
85 return false;
86 }
87
88 /**
89 * Specifies the length of the content.
90 *
91 * @param len the number of bytes in the content, or
92 * a negative number to indicate an unknown length
93 */
94 public void setContentLength(long len) {
95 this.length = len;
96 }
97
98 /**
99 * Specifies the content.
100 *
101 * @param instream the stream to return with the next call to
102 * {@link #getContent getContent}
103 */
104 public void setContent(final InputStream instream) {
105 this.content = instream;
106 }
107
108 public void writeTo(final OutputStream outstream) throws IOException {
109 if (outstream == null) {
110 throw new IllegalArgumentException("Output stream may not be null");
111 }
112 InputStream instream = getContent();
113 try {
114 int l;
115 byte[] tmp = new byte[2048];
116 while ((l = instream.read(tmp)) != -1) {
117 outstream.write(tmp, 0, l);
118 }
119 } finally {
120 instream.close();
121 }
122 }
123
124 public boolean isStreaming() {
125 return this.content != null;
126 }
127
128 /**
129 * Closes the content InputStream.
130 *
131 * @deprecated (4.1) Either use {@link #getContent()} and call {@link java.io.InputStream#close()} on that;
132 * otherwise call {@link #writeTo(OutputStream)} which is required to free the resources.
133 */
134 @Deprecated
135 @Override
136 public void consumeContent() throws IOException {
137 if (content != null) {
138 content.close(); // reads to the end of the entity
139 }
140 }
141
142 }