1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/InputStreamEntity.java $ 3 * $Revision: 617591 $ 4 * $Date: 2008-02-01 10:21:17 -0800 (Fri, 01 Feb 2008) $ 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 streamed entity obtaining content from an {@link InputStream InputStream}. 40 * 41 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 42 * 43 * @version $Revision: 617591 $ 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 InputStreamEntity extends AbstractHttpEntity { 53 54 private final static int BUFFER_SIZE = 2048; 55 56 private final InputStream content; 57 private final long length; 58 private boolean consumed = false; 59 InputStreamEntity(final InputStream instream, long length)60 public InputStreamEntity(final InputStream instream, long length) { 61 super(); 62 if (instream == null) { 63 throw new IllegalArgumentException("Source input stream may not be null"); 64 } 65 this.content = instream; 66 this.length = length; 67 } 68 isRepeatable()69 public boolean isRepeatable() { 70 return false; 71 } 72 getContentLength()73 public long getContentLength() { 74 return this.length; 75 } 76 getContent()77 public InputStream getContent() throws IOException { 78 return this.content; 79 } 80 writeTo(final OutputStream outstream)81 public void writeTo(final OutputStream outstream) throws IOException { 82 if (outstream == null) { 83 throw new IllegalArgumentException("Output stream may not be null"); 84 } 85 InputStream instream = this.content; 86 byte[] buffer = new byte[BUFFER_SIZE]; 87 int l; 88 if (this.length < 0) { 89 // consume until EOF 90 while ((l = instream.read(buffer)) != -1) { 91 outstream.write(buffer, 0, l); 92 } 93 } else { 94 // consume no more than length 95 long remaining = this.length; 96 while (remaining > 0) { 97 l = instream.read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining)); 98 if (l == -1) { 99 break; 100 } 101 outstream.write(buffer, 0, l); 102 remaining -= l; 103 } 104 } 105 this.consumed = true; 106 } 107 108 // non-javadoc, see interface HttpEntity isStreaming()109 public boolean isStreaming() { 110 return !this.consumed; 111 } 112 113 // non-javadoc, see interface HttpEntity consumeContent()114 public void consumeContent() throws IOException { 115 this.consumed = true; 116 // If the input stream is from a connection, closing it will read to 117 // the end of the content. Otherwise, we don't care what it does. 118 this.content.close(); 119 } 120 121 } // class InputStreamEntity 122