001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/entity/BasicHttpEntity.java $
003: * $Revision: 496070 $
004: * $Date: 2007-01-14 13:18:34 +0100 (Sun, 14 Jan 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.entity;
033:
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.OutputStream;
037:
038: /**
039: * A generic streamed entity being received on a connection.
040: *
041: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
042: *
043: * @version $Revision: 496070 $
044: *
045: * @since 4.0
046: */
047: public class BasicHttpEntity extends AbstractHttpEntity {
048:
049: private InputStream content;
050: private boolean contentObtained;
051: private long length;
052:
053: /**
054: * Creates a new basic entity.
055: * The content is initially missing, the content length
056: * is set to a negative number.
057: */
058: public BasicHttpEntity() {
059: super ();
060: this .length = -1;
061: }
062:
063: // non-javadoc, see interface HttpEntity
064: public long getContentLength() {
065: return this .length;
066: }
067:
068: /**
069: * Obtains the content, once only.
070: *
071: * @return the content, if this is the first call to this method
072: * since {@link #setContent setContent} has been called
073: *
074: * @throws IllegalStateException
075: * if the content has been obtained before, or
076: * has not yet been provided
077: */
078: public InputStream getContent() throws IllegalStateException {
079: if (this .content == null) {
080: throw new IllegalStateException(
081: "Content has not been provided");
082: }
083: if (this .contentObtained) {
084: throw new IllegalStateException("Content has been consumed");
085: }
086: this .contentObtained = true;
087: return this .content;
088:
089: } // getContent
090:
091: /**
092: * Tells that this entity is not repeatable.
093: *
094: * @return <code>false</code>
095: */
096: public boolean isRepeatable() {
097: return false;
098: }
099:
100: /**
101: * Specifies the length of the content.
102: *
103: * @param len the number of bytes in the content, or
104: * a negative number to indicate an unknown length
105: */
106: public void setContentLength(long len) {
107: this .length = len;
108: }
109:
110: /**
111: * Specifies the content.
112: *
113: * @param instream the stream to return with the next call to
114: * {@link #getContent getContent}
115: */
116: public void setContent(final InputStream instream) {
117: this .content = instream;
118: this .contentObtained = false;
119: }
120:
121: // non-javadoc, see interface HttpEntity
122: public void writeTo(final OutputStream outstream)
123: throws IOException {
124: if (outstream == null) {
125: throw new IllegalArgumentException(
126: "Output stream may not be null");
127: }
128: InputStream instream = getContent();
129: int l;
130: byte[] tmp = new byte[2048];
131: while ((l = instream.read(tmp)) != -1) {
132: outstream.write(tmp, 0, l);
133: }
134: }
135:
136: // non-javadoc, see interface HttpEntity
137: public boolean isStreaming() {
138: return !this .contentObtained && this .content != null;
139: }
140:
141: // non-javadoc, see interface HttpEntity
142: public void consumeContent() throws IOException {
143: if (content != null) {
144: content.close(); // reads to the end of the entity
145: }
146: }
147:
148: } // class BasicHttpEntity
|