package q02b.exe.utils;



import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.FilterInputStream;

import java.io.IOException;

import java.io.InputStream;



public class Q02bCopyInputStream extends FilterInputStream {



	private static final long serialVersionUID = 394941082745728326L;



	private ByteArrayOutputStream _copy = new ByteArrayOutputStream();

	private InputStream			  _is   = null;

	

	public Q02bCopyInputStream(InputStream arg0) {

		super(arg0);

		_is = arg0;

	}



	public int read() throws IOException {

		int data=0;

		//Se escriben los datos mientras existan

		while((data = _is.read())>-1){

			_copy.write(data);

		}

		_copy.flush();

		_copy.close(); // MUY IMPORTANTE!!!

		_is.close();

		return _copy.toByteArray().length;

	}

	

	public InputStream getCopy() {

		return new ByteArrayInputStream(_copy.toByteArray());

	}



}

