1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/BasicHttpEntity.java $ 3 * $Revision: 496070 $ 4 * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ 5 * 6 * ==================================================================== 7 * Licensed to the Apache Software Foundation (ASF) under one 8 * or more contributor license agreements. See the NOTICE file 9 * distributed with this work for additional information 10 * regarding copyright ownership. The ASF licenses this file 11 * to you under the Apache License, Version 2.0 (the 12 * "License"); you may not use this file except in compliance 13 * with the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, 18 * software distributed under the License is distributed on an 19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20 * KIND, either express or implied. See the License for the 21 * specific language governing permissions and limitations 22 * under the License. 23 * ==================================================================== 24 * 25 * This software consists of voluntary contributions made by many 26 * individuals on behalf of the Apache Software Foundation. For more 27 * information on the Apache Software Foundation, please see 28 * <http://www.apache.org/>. 29 * 30 */ 31 32 package org.apache.http.entity; 33 34 import java.io.IOException; 35 import java.io.InputStream; 36 import java.io.OutputStream; 37 38 /** 39 * A generic streamed entity being received on a connection. 40 * 41 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 42 * 43 * @version $Revision: 496070 $ 44 * 45 * @since 4.0 46 */ 47 public class BasicHttpEntity extends AbstractHttpEntity { 48 49 private InputStream content; 50 private boolean contentObtained; 51 private long length; 52 53 /** 54 * Creates a new basic entity. 55 * The content is initially missing, the content length 56 * is set to a negative number. 57 */ BasicHttpEntity()58 public BasicHttpEntity() { 59 super(); 60 this.length = -1; 61 } 62 63 // non-javadoc, see interface HttpEntity getContentLength()64 public long getContentLength() { 65 return this.length; 66 } 67 68 /** 69 * Obtains the content, once only. 70 * 71 * @return the content, if this is the first call to this method 72 * since {@link #setContent setContent} has been called 73 * 74 * @throws IllegalStateException 75 * if the content has been obtained before, or 76 * has not yet been provided 77 */ getContent()78 public InputStream getContent() 79 throws IllegalStateException { 80 if (this.content == null) { 81 throw new IllegalStateException("Content has not been provided"); 82 } 83 if (this.contentObtained) { 84 throw new IllegalStateException("Content has been consumed"); 85 } 86 this.contentObtained = true; 87 return this.content; 88 89 } // getContent 90 91 /** 92 * Tells that this entity is not repeatable. 93 * 94 * @return <code>false</code> 95 */ isRepeatable()96 public boolean isRepeatable() { 97 return false; 98 } 99 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 */ setContentLength(long len)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 */ setContent(final InputStream instream)116 public void setContent(final InputStream instream) { 117 this.content = instream; 118 this.contentObtained = false; 119 } 120 121 // non-javadoc, see interface HttpEntity writeTo(final OutputStream outstream)122 public void writeTo(final OutputStream outstream) throws IOException { 123 if (outstream == null) { 124 throw new IllegalArgumentException("Output stream may not be null"); 125 } 126 InputStream instream = getContent(); 127 int l; 128 byte[] tmp = new byte[2048]; 129 while ((l = instream.read(tmp)) != -1) { 130 outstream.write(tmp, 0, l); 131 } 132 } 133 134 // non-javadoc, see interface HttpEntity isStreaming()135 public boolean isStreaming() { 136 return !this.contentObtained && this.content != null; 137 } 138 139 // non-javadoc, see interface HttpEntity consumeContent()140 public void consumeContent() throws IOException { 141 if (content != null) { 142 content.close(); // reads to the end of the entity 143 } 144 } 145 146 } // class BasicHttpEntity 147