DebugInputStream.java Source code

Java tutorial

Introduction

Here is the source code for DebugInputStream.java

Source

/*
Copyright (C) 2004  Juho Vh-Herttua
    
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
02111-1307, USA.
*/

import java.io.*;

public class DebugInputStream extends InputStream {
    InputStream is;
    ByteArrayOutputStream buffer;
    boolean debug;

    public DebugInputStream(InputStream in, boolean usedebug) {
        is = in;
        buffer = new ByteArrayOutputStream();
        debug = usedebug;
    }

    public int read() throws IOException {
        int input;

        input = is.read();
        if (debug)
            buffer.write(input);

        return input;
    }

    public int read(byte b[], int off, int len) throws IOException {
        int readb;

        readb = is.read(b, off, len);
        if (debug)
            buffer.write(b, off, readb);
        return readb;
    }

    public int available() throws IOException {
        return is.available();
    }

    public void close() throws IOException {
        buffer.close();
        is.close();
    }

    public void mark(int readlimit) {
        is.mark(readlimit);
    }

    public void reset() throws IOException {
        is.reset();
    }

    public boolean markSupported() {
        return is.markSupported();
    }

    public byte[] toByteArray() {
        return buffer.toByteArray();
    }

    public int size() {
        return buffer.size();
    }
}