001    /*
002     * LAPIS lightweight structured text processing system
003     *
004     * Copyright (C) 1998-2002 Carnegie Mellon University,
005     * Copyright (C) 2003 Massachusetts Institute of Technology.
006     * All rights reserved.
007     *
008     * This library is free software; you can redistribute it
009     * and/or modify it under the terms of the GNU General
010     * Public License as published by the Free Software
011     * Foundation, version 2.
012     *
013     * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014     */
015    package lapisx.io;
016    
017    import java.io.*;
018    
019    /**
020     * TeeStream is used to simultaneously write to an OutputStream
021     * (passed as an argument to the constructor) as bytes are read from
022     * the underlying InputStream.
023     */
024    public class TeeStream extends FilterInputStream {
025    
026        protected OutputStream out = null;
027    
028        public TeeStream(InputStream in, OutputStream out) {
029            super(in);
030            this.out = out;
031        }
032    
033        public int read() throws IOException {
034            int j = in.read();
035            if (j != -1) {
036                out.write(j);
037            }
038            return j;
039        }
040    
041        public int read(byte[] b) throws IOException{
042            int j = in.read(b);
043            if (j != -1) {
044                out.write(b, 0, j);
045            }
046            return j;
047        }
048    
049        public int read(byte[] b, int off, int len) throws IOException {
050            int j = in.read(b,off,len);
051            if (j != -1) {
052                out.write(b,off,j);
053            }
054            return j;
055        }
056    
057    }