001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.io;
029
030import java.io.IOException;
031import java.io.InputStream;
032
033import org.apache.http.io.BufferInfo;
034import org.apache.http.io.SessionInputBuffer;
035import org.apache.http.util.Args;
036
037/**
038 * Input stream that reads data without any transformation. The end of the
039 * content entity is demarcated by closing the underlying connection
040 * (EOF condition). Entities transferred using this input stream can be of
041 * unlimited length.
042 * <p>
043 * Note that this class NEVER closes the underlying stream, even when close
044 * gets called.  Instead, it will read until the end of the stream (until
045 * {@code -1} is returned).
046 *
047 * @since 4.0
048 */
049public class IdentityInputStream extends InputStream {
050
051    private final SessionInputBuffer in;
052
053    private boolean closed = false;
054
055    /**
056     * Wraps session input stream and reads input until the the end of stream.
057     *
058     * @param in The session input buffer
059     */
060    public IdentityInputStream(final SessionInputBuffer in) {
061        super();
062        this.in = Args.notNull(in, "Session input buffer");
063    }
064
065    @Override
066    public int available() throws IOException {
067        if (this.in instanceof BufferInfo) {
068            return ((BufferInfo) this.in).length();
069        } else {
070            return 0;
071        }
072    }
073
074    @Override
075    public void close() throws IOException {
076        this.closed = true;
077    }
078
079    @Override
080    public int read() throws IOException {
081        if (this.closed) {
082            return -1;
083        } else {
084            return this.in.read();
085        }
086    }
087
088    @Override
089    public int read(final byte[] b, final int off, final int len) throws IOException {
090        if (this.closed) {
091            return -1;
092        } else {
093            return this.in.read(b, off, len);
094        }
095    }
096
097}