001/*
002 * $HeadURL: file:///opt/dev/not-yet-commons-ssl-SVN-repo/tags/commons-ssl-0.3.17/src/java/org/apache/commons/ssl/ComboInputStream.java $
003 * $Revision: 121 $
004 * $Date: 2007-11-13 21:26:57 -0800 (Tue, 13 Nov 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
032package org.apache.commons.ssl;
033
034import java.io.IOException;
035import java.io.InputStream;
036
037/**
038 * @author Credit Union Central of British Columbia
039 * @author <a href="http://www.cucbc.com/">www.cucbc.com</a>
040 * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a>
041 * @since 22-Feb-2007
042 */
043public class ComboInputStream extends InputStream {
044    private boolean headDone;
045    private InputStream head;
046    private InputStream tail;
047
048    public ComboInputStream(InputStream head, InputStream tail) {
049        this.head = head != null ? head : tail;
050        this.tail = tail != null ? tail : head;
051    }
052
053    public int read() throws IOException {
054        int c;
055        if (headDone) {
056            c = tail.read();
057        } else {
058            c = head.read();
059            if (c == -1) {
060                headDone = true;
061                c = tail.read();
062            }
063        }
064        return c;
065    }
066
067    public int available() throws IOException {
068        return tail.available() + head.available();
069    }
070
071    public void close() throws IOException {
072        try {
073            head.close();
074        }
075        finally {
076            if (head != tail) {
077                tail.close();
078            }
079        }
080    }
081
082    public int read(byte b[], int off, int len) throws IOException {
083        int c;
084        if (headDone) {
085            c = tail.read(b, off, len);
086        } else {
087            c = head.read(b, off, len);
088            if (c == -1) {
089                headDone = true;
090                c = tail.read(b, off, len);
091            }
092        }
093        return c;
094    }
095
096}