package zutil; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.Reader; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Map; /** * @author Ziver * this class can print strings to multiple PrintStreams */ public class MultiPrintStream extends PrintStream { //the print streams that will print private ArrayList streams; //a instance of this class public static MultiPrintStream out = new MultiPrintStream(); public MultiPrintStream(){ super(new PrintStream(System.out)); streams = new ArrayList(); streams.add(new PrintStream(System.out)); } /** * This constructor makes a simple PrintStream that prints to the console and to a file * @param file is the file name to output to */ public MultiPrintStream(String file){ super(new PrintStream(System.out)); try { streams = new ArrayList(); streams.add(new PrintStream(System.out)); streams.add(new PrintStream(new File(file))); } catch (FileNotFoundException e) { System.out.println("Error when declaring PrintStream!!"); e.printStackTrace(); } } /** * This constructor takes a array of PrintStreams to be used * @param streams is a array of the streams that will be used */ public MultiPrintStream(PrintStream[] streams){ super(streams[0]); this.streams = new ArrayList(); for(int i=0; i- Array content *
- Map content (HashMap etc.) *
- List content (ArrayList, LinkedList etc.) *
- InputStream content (Prints out until the end of the stream) *
- Reader content (Prints out until the end of the reader) *
- Instance variables of a Object * * @param o is the Object to dump */ public void dump( Object o ){ println(dumpToString( o )); } /** * Dumps the content of: *
- Array content *
- Map content (HashMap etc.) *
- List content (ArrayList, LinkedList etc.) *
- InputStream content (Prints out until the end of the stream) *
- Reader content (Prints out until the end of the reader) *
- Instance variables of a Object * * @param o is the Object to dump * @return A String with all the printed data */ public String dumpToString( Object o) { return dumpToString(o, ""); } /** * Dumps the content of: *
- Array content *
- Map content (HashMap etc.) *
- List content (ArrayList, LinkedList etc.) *
- InputStream content (Prints out until the end of the stream) *
- Reader content (Prints out until the end of the reader) *
- Instance variables of a Object * * @param o is the Object to dump * @param head is the string that will be put in front of every line * @return A String with all the printed data */ @SuppressWarnings("unchecked") private String dumpToString( Object o , String head) { if(o == null) return "NULL"; StringBuffer buffer = new StringBuffer(); Class oClass = o.getClass(); buffer.append( oClass.getName() ); String nextHead = head + "\t"; // Prints out Arrays if ( oClass.isArray() ) { buffer.append( "[" ); for ( int i=0; i" ); buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) ); if(it.hasNext()) buffer.append( "," ); } buffer.append( "\n" ); buffer.append(head); buffer.append( "}" ); } // Prints out data from InputStream else if(o instanceof InputStream){ buffer.append( " =>{\n" ); try { InputStream in = (InputStream)o; int tmp; while((tmp = in.read()) != -1){ buffer.append(nextHead); buffer.append( (char)tmp ); } in.close(); } catch (IOException e) { e.printStackTrace(this); } buffer.append( "\n" ); buffer.append(head); buffer.append( "}" ); } // Prints out data from InputStream else if(o instanceof Reader){ buffer.append( " =>{\n" ); try { Reader in = (Reader)o; int tmp; while((tmp = in.read()) != -1){ buffer.append(nextHead); buffer.append( (char)tmp ); } in.close(); } catch (IOException e) { e.printStackTrace(this); } buffer.append( "\n" ); buffer.append(head); buffer.append( "}" ); } // Prints out Object properties else{ buffer.append( "{" ); while ( oClass != null ) { Field[] fields = oClass.getDeclaredFields(); for ( int i=0; i