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 * @deprecated Please use {@link java.net.URL#openConnection} instead. 48 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 49 * for further details. 50 */ 51 @Deprecated 52 public class BasicHttpEntity extends AbstractHttpEntity { 53 54 private InputStream content; 55 private boolean contentObtained; 56 private long length; 57 58 /** 59 * Creates a new basic entity. 60 * The content is initially missing, the content length 61 * is set to a negative number. 62 */ 63 public BasicHttpEntity() { 64 super(); 65 this.length = -1; 66 } 67 68 // non-javadoc, see interface HttpEntity 69 public long getContentLength() { 70 return this.length; 71 } 72 73 /** 74 * Obtains the content, once only. 75 * 76 * @return the content, if this is the first call to this method 77 * since {@link #setContent setContent} has been called 78 * 79 * @throws IllegalStateException 80 * if the content has been obtained before, or 81 * has not yet been provided 82 */ 83 public InputStream getContent() 84 throws IllegalStateException { 85 if (this.content == null) { 86 throw new IllegalStateException("Content has not been provided"); 87 } 88 if (this.contentObtained) { 89 throw new IllegalStateException("Content has been consumed"); 90 } 91 this.contentObtained = true; 92 return this.content; 93 94 } // getContent 95 96 /** 97 * Tells that this entity is not repeatable. 98 * 99 * @return <code>false</code> 100 */ 101 public boolean isRepeatable() { 102 return false; 103 } 104 105 /** 106 * Specifies the length of the content. 107 * 108 * @param len the number of bytes in the content, or 109 * a negative number to indicate an unknown length 110 */ 111 public void setContentLength(long len) { 112 this.length = len; 113 } 114 115 /** 116 * Specifies the content. 117 * 118 * @param instream the stream to return with the next call to 119 * {@link #getContent getContent} 120 */ 121 public void setContent(final InputStream instream) { 122 this.content = instream; 123 this.contentObtained = false; 124 } 125 126 // non-javadoc, see interface HttpEntity 127 public void writeTo(final OutputStream outstream) throws IOException { 128 if (outstream == null) { 129 throw new IllegalArgumentException("Output stream may not be null"); 130 } 131 InputStream instream = getContent(); 132 int l; 133 byte[] tmp = new byte[2048]; 134 while ((l = instream.read(tmp)) != -1) { 135 outstream.write(tmp, 0, l); 136 } 137 } 138 139 // non-javadoc, see interface HttpEntity 140 public boolean isStreaming() { 141 return !this.contentObtained && this.content != null; 142 } 143 144 // non-javadoc, see interface HttpEntity 145 public void consumeContent() throws IOException { 146 if (content != null) { 147 content.close(); // reads to the end of the entity 148 } 149 } 150 151 } // class BasicHttpEntity 152