Home | History | Annotate | Download | only in entity
      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 public class InputStreamEntity extends AbstractHttpEntity {
     48 
     49     private final static int BUFFER_SIZE = 2048;
     50 
     51     private final InputStream content;
     52     private final long length;
     53     private boolean consumed = false;
     54 
     55     public InputStreamEntity(final InputStream instream, long length) {
     56         super();
     57         if (instream == null) {
     58             throw new IllegalArgumentException("Source input stream may not be null");
     59         }
     60         this.content = instream;
     61         this.length = length;
     62     }
     63 
     64     public boolean isRepeatable() {
     65         return false;
     66     }
     67 
     68     public long getContentLength() {
     69         return this.length;
     70     }
     71 
     72     public InputStream getContent() throws IOException {
     73         return this.content;
     74     }
     75 
     76     public void writeTo(final OutputStream outstream) throws IOException {
     77         if (outstream == null) {
     78             throw new IllegalArgumentException("Output stream may not be null");
     79         }
     80         InputStream instream = this.content;
     81         byte[] buffer = new byte[BUFFER_SIZE];
     82         int l;
     83         if (this.length < 0) {
     84             // consume until EOF
     85             while ((l = instream.read(buffer)) != -1) {
     86                 outstream.write(buffer, 0, l);
     87             }
     88         } else {
     89             // consume no more than length
     90             long remaining = this.length;
     91             while (remaining > 0) {
     92                 l = instream.read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining));
     93                 if (l == -1) {
     94                     break;
     95                 }
     96                 outstream.write(buffer, 0, l);
     97                 remaining -= l;
     98             }
     99         }
    100         this.consumed = true;
    101     }
    102 
    103     // non-javadoc, see interface HttpEntity
    104     public boolean isStreaming() {
    105         return !this.consumed;
    106     }
    107 
    108     // non-javadoc, see interface HttpEntity
    109     public void consumeContent() throws IOException {
    110         this.consumed = true;
    111         // If the input stream is from a connection, closing it will read to
    112         // the end of the content. Otherwise, we don't care what it does.
    113         this.content.close();
    114     }
    115 
    116 } // class InputStreamEntity
    117