Skip to content

Instantly share code, notes, and snippets.

@sjyun
Created March 24, 2014 07:53
Show Gist options
  • Save sjyun/9735925 to your computer and use it in GitHub Desktop.
Save sjyun/9735925 to your computer and use it in GitHub Desktop.
multi Thread용 IO Wrapper
package thex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/*
* 동기화 처리를 위한 IO Wrapper
*
*
* 다음과 같이 사용가능함
* IOWrapper.out().println( "문자열" );
* IOWrapper.err().println( "문자열" );
* IOWrapper.printWriterWrapper().println( "문자열" );
* */
public final class IOWrapper {
private static BufferedReader input;
private static PrintWriter output = new PrintWriter(System.out, true);
private static PrintWriter error = new PrintWriter(System.err, true);
private static PrintWriter printWriterWrapper = new PrintWriterWrapper();
private static final Object input_lock = new Object();
private static final Object output_lock = new Object();
private static final Object error_lock = new Object();
private static final Object printWriterWrapper_lock = new Object();
//직접 생성하는 것을 막는다.
private IOWrapper(){}
//system.in wrapping
public static BufferedReader in(){
synchronized ( input_lock )
{
if( input == null) {
try{
input = new BufferedReader( new InputStreamReader(System.in));
}catch( Exception e){
throw new Error( e.getMessage());
}
}
}
return input;
}
//system.out wrapping
public static PrintWriter out(){
synchronized ( output_lock){
if( output == null){
output = new PrintWriter( System.out, true);
}
return output;
}
}
public static PrintWriter err(){
synchronized ( error_lock){
if( error == null){
error = new PrintWriter( System.err, true);
}
return error;
}
}
public static PrintWriter printWriterWrapper()
{
synchronized ( printWriterWrapper_lock){
if( printWriterWrapper == null){
printWriterWrapper = new PrintWriterWrapper();
}
}
return printWriterWrapper;
}
private final static class PrintWriterWrapper extends PrintWriter
{
public PrintWriterWrapper(){ super(System.err);}
public void close() {}
public void flush() {}
public void print(boolean b) {}
public void print(char c) {}
public void print(char[] s) {}
public void print(double d) {}
public void print(float f) {}
public void print(int i) {}
public void print(long l) {}
public void print(Object o) {}
public void print(String s) {}
public void println() {}
public void println(boolean b) {}
public void println(char c) {}
public void println(char[] s) {}
public void println(double d) {}
public void println(float f) {}
public void println(int i) {}
public void println(long l) {}
public void println(Object o) {}
public void write(char[] buf) {}
public void write(char[] buf, int off, int len) {}
public void write(int c) {}
public void write(String buf) {}
public void write(String buf, int off, int len) {}
}
/*
* 테스트를 위한 내부 클래스
* */
static public class Test{
static public void main(String ar[]) throws IOException{
String s;
while( (s = IOWrapper.in().readLine()) != null )
{
if( s.equals("quit") )
break;
IOWrapper.out().println( s);
IOWrapper.err().println( s );
IOWrapper.printWriterWrapper().println( s );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment