1 //****************************************************************************** 2 // 3 // File: Stdio.java 4 // Package: edu.rit.io 5 // Unit: Class edu.rit.io.Stdio 6 // 7 // This Java source file is copyright (C) 2010 by Alan Kaminsky. All rights 8 // reserved. For further information, contact the author, Alan Kaminsky, at 9 // ark@cs.rit.edu. 10 // 11 // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free 12 // software; you can redistribute it and/or modify it under the terms of the GNU 13 // General Public License as published by the Free Software Foundation; either 14 // version 3 of the License, or (at your option) any later version. 15 // 16 // PJ is distributed in the hope that it will be useful, but WITHOUT ANY 17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 18 // A PARTICULAR PURPOSE. See the GNU General Public License for more details. 19 // 20 // Linking this library statically or dynamically with other modules is making a 21 // combined work based on this library. Thus, the terms and conditions of the GNU 22 // General Public License cover the whole combination. 23 // 24 // As a special exception, the copyright holders of this library give you 25 // permission to link this library with independent modules to produce an 26 // executable, regardless of the license terms of these independent modules, and 27 // to copy and distribute the resulting executable under terms of your choice, 28 // provided that you also meet, for each linked independent module, the terms 29 // and conditions of the license of that module. An independent module is a module 30 // which is not derived from or based on this library. If you modify this library, 31 // you may extend this exception to your version of the library, but you are not 32 // obligated to do so. If you do not wish to do so, delete this exception 33 // statement from your version. 34 // 35 // A copy of the GNU General Public License is provided in the file gpl.txt. You 36 // may also obtain a copy of the GNU General Public License on the World Wide 37 // Web at http://www.gnu.org/licenses/gpl.html. 38 // 39 //****************************************************************************** 40 package edu.rit.io; 41 42 import java.io.InputStream; 43 import java.io.PrintStream; 44 45 /** 46 * Class Stdio provides standard I/O streams that can be redirected on a 47 * per-thread basis. 48 * 49 * @author Alan Kaminsky 50 * @version 08-Oct-2010 51 */ 52 public class Stdio { 53 54 // Prevent construction. 55 private Stdio() { 56 } 57 58 // Hidden data members. 59 private static ThreadLocal<InputStream> in 60 = new ThreadLocal<InputStream>() { 61 protected InputStream initialValue() { 62 return System.in; 63 } 64 }; 65 66 private static ThreadLocal<PrintStream> out 67 = new ThreadLocal<PrintStream>() { 68 protected PrintStream initialValue() { 69 return System.out; 70 } 71 }; 72 73 private static ThreadLocal<PrintStream> err 74 = new ThreadLocal<PrintStream>() { 75 protected PrintStream initialValue() { 76 return System.err; 77 } 78 }; 79 80 // Exported operations. 81 /** 82 * Get the standard input stream for the calling thread. 83 * 84 * @return Standard input stream. 85 */ 86 public static InputStream in() { 87 return in.get(); 88 } 89 90 /** 91 * Get the standard output stream for the calling thread. 92 * 93 * @return Standard output stream. 94 */ 95 public static PrintStream out() { 96 return out.get(); 97 } 98 99 /** 100 * Get the standard error stream for the calling thread. 101 * 102 * @return Standard error stream. 103 */ 104 public static PrintStream err() { 105 return err.get(); 106 } 107 108 /** 109 * Set the standard input stream for the calling thread. If not set, the 110 * default is <code>System.in</code>. 111 * 112 * @param stream Standard input stream. 113 * @exception NullPointerException (unchecked exception) Thrown if 114 * <code>stream</code> is null. 115 */ 116 public static void in(InputStream stream) { 117 if (stream == null) { 118 throw new NullPointerException("Stdio.in(): stream is null"); 119 } 120 in.set(stream); 121 } 122 123 /** 124 * Set the standard output stream for the calling thread. If not set, the 125 * default is <code>System.out</code>. 126 * 127 * @param stream Standard output stream. 128 * @exception NullPointerException (unchecked exception) Thrown if 129 * <code>stream</code> is null. 130 */ 131 public static void out(PrintStream stream) { 132 if (stream == null) { 133 throw new NullPointerException("Stdio.out(): stream is null"); 134 } 135 out.set(stream); 136 } 137 138 /** 139 * Set the standard error stream for the calling thread. If not set, the 140 * default is <code>System.err</code>. 141 * 142 * @param stream Standard error stream. 143 * @exception NullPointerException (unchecked exception) Thrown if 144 * <code>stream</code> is null. 145 */ 146 public static void err(PrintStream stream) { 147 if (stream == null) { 148 throw new NullPointerException("Stdio.err(): stream is null"); 149 } 150 err.set(stream); 151 } 152 153 }