001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/entity/InputStreamEntity.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 streamed entity obtaining content from an {@link InputStream InputStream}.
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 InputStreamEntity extends AbstractHttpEntity {
048:
049: private final static int BUFFER_SIZE = 2048;
050:
051: private final InputStream content;
052: private final long length;
053: private boolean consumed = false;
054:
055: public InputStreamEntity(final InputStream instream, long length) {
056: super ();
057: if (instream == null) {
058: throw new IllegalArgumentException(
059: "Source input stream may not be null");
060: }
061: this .content = instream;
062: this .length = length;
063: }
064:
065: public boolean isRepeatable() {
066: return false;
067: }
068:
069: public long getContentLength() {
070: return this .length;
071: }
072:
073: public InputStream getContent() throws IOException {
074: return this .content;
075: }
076:
077: public void writeTo(final OutputStream outstream)
078: throws IOException {
079: if (outstream == null) {
080: throw new IllegalArgumentException(
081: "Output stream may not be null");
082: }
083: InputStream instream = this .content;
084: byte[] buffer = new byte[BUFFER_SIZE];
085: int l;
086: if (this .length < 0) {
087: // consume until EOF
088: while ((l = instream.read(buffer)) != -1) {
089: outstream.write(buffer, 0, l);
090: }
091: } else {
092: // consume no more than length
093: long remaining = this .length;
094: while (remaining > 0) {
095: l = instream.read(buffer, 0, (int) Math.min(
096: BUFFER_SIZE, remaining));
097: if (l == -1) {
098: break;
099: }
100: outstream.write(buffer, 0, l);
101: remaining -= l;
102: }
103: }
104: this .consumed = true;
105: }
106:
107: // non-javadoc, see interface HttpEntity
108: public boolean isStreaming() {
109: return !this .consumed;
110: }
111:
112: // non-javadoc, see interface HttpEntity
113: public void consumeContent() throws IOException {
114: this .consumed = true;
115: // If the input stream is from a connection, closing it will read to
116: // the end of the content. Otherwise, we don't care what it does.
117: this .content.close();
118: }
119:
120: } // class InputStreamEntity
|