/******************************************************************************* * Copyright (c) 2013 Ziver * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ package zutil.io; 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; import zutil.Dumpable; /** * @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 */ 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 it = ((Collection)o).iterator(); buffer.append( "{" ); while(it.hasNext()){ Object value = it.next(); buffer.append("\n"); buffer.append(nextHead); buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) ); if(it.hasNext()) buffer.append( "," ); } buffer.append( "\n" ); buffer.append(head); buffer.append( "}" ); } // Prints out a Map else if(o instanceof Map){ Iterator it = ((Map)o).keySet().iterator(); buffer.append( "{" ); while(it.hasNext()){ Object key = it.next(); Object value = ((Map)o).get(key); buffer.append("\n"); buffer.append(nextHead); buffer.append( key ); buffer.append( "=>" ); 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