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 */
027package org.apache.http.client.entity;
028
029import java.io.IOException;
030import java.io.InputStream;
031
032/**
033 * Lazy init InputStream wrapper.
034 */
035class LazyDecompressingInputStream extends InputStream {
036
037    private final InputStream wrappedStream;
038    private final InputStreamFactory inputStreamFactory;
039
040    private InputStream wrapperStream;
041
042    public LazyDecompressingInputStream(
043            final InputStream wrappedStream,
044            final InputStreamFactory inputStreamFactory) {
045        this.wrappedStream = wrappedStream;
046        this.inputStreamFactory = inputStreamFactory;
047    }
048
049    private void initWrapper() throws IOException {
050        if (wrapperStream == null) {
051            wrapperStream = inputStreamFactory.create(wrappedStream);
052        }
053    }
054
055    @Override
056    public int read() throws IOException {
057        initWrapper();
058        return wrapperStream.read();
059    }
060
061    @Override
062    public int read(final byte[] b) throws IOException {
063        initWrapper();
064        return wrapperStream.read(b);
065    }
066
067    @Override
068    public int read(final byte[] b, final int off, final int len) throws IOException {
069        initWrapper();
070        return wrapperStream.read(b, off, len);
071    }
072
073    @Override
074    public long skip(final long n) throws IOException {
075        initWrapper();
076        return wrapperStream.skip(n);
077    }
078
079    @Override
080    public boolean markSupported() {
081        return false;
082    }
083
084    @Override
085    public int available() throws IOException {
086        initWrapper();
087        return wrapperStream.available();
088    }
089
090    @Override
091    public void close() throws IOException {
092        try {
093            if (wrapperStream != null) {
094                wrapperStream.close();
095            }
096        } finally {
097            wrappedStream.close();
098        }
099    }
100
101}