OSDN Git Service

55214baccc767dbeafd5454ef664c7a929fe08e8
[jindolf/JinArchiver.git] / src / main / java / jp / sourceforge / jindolf / archiver / SnifWriter.java
1 /*
2  * Snif Writer
3  *
4  * License : The MIT License
5  * Copyright(c) 2016 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf.archiver;
9
10 import java.io.IOException;
11 import java.io.PipedReader;
12 import java.io.PipedWriter;
13 import java.io.Reader;
14 import java.io.Writer;
15
16 /**
17  * 別スレッドから盗聴可能なWriter。
18  */
19 public class SnifWriter extends Writer{
20
21     private final Writer fout;
22
23     private final Writer pout;
24     private final Reader reader;
25
26
27     /**
28      * コンストラクタ。
29      * @param writer 移譲先Writer
30      */
31     public SnifWriter(Writer writer){
32         super();
33
34         if(writer == null) throw new NullPointerException();
35         this.fout = writer;
36
37         PipedWriter pipeOut = new PipedWriter();
38         PipedReader pipeIn  = new PipedReader();
39
40         try{
41             pipeOut.connect(pipeIn);
42         }catch(IOException e){
43             // ありえない
44             assert false;
45         }
46
47         this.pout = pipeOut;
48         this.reader = pipeIn;
49
50         return;
51     }
52
53
54     /**
55      * 傍受用Readerを返す。
56      * @return Reader
57      */
58     public Reader getSnifReader(){
59         return this.reader;
60     }
61
62     /**
63      * {@inheritDoc}
64      * @throws IOException {@inheritDoc}
65      */
66     @Override
67     public void close() throws IOException{
68         this.fout.close();
69         this.pout.close();
70         return;
71     }
72
73     /**
74      * {@inheritDoc}
75      * @throws IOException {@inheritDoc}
76      */
77     @Override
78     public void flush() throws IOException{
79         this.fout.flush();
80         this.pout.flush();
81         return;
82     }
83
84     /**
85      * {@inheritDoc}
86      * @param cbuf {@inheritDoc}
87      * @param off {@inheritDoc}
88      * @param len {@inheritDoc}
89      * @throws IOException {@inheritDoc}
90      */
91     @Override
92     public void write(char[] cbuf, int off, int len) throws IOException{
93         this.fout.write(cbuf, off, len);
94         this.pout.write(cbuf, off, len);
95         return;
96     }
97
98     /**
99      * {@inheritDoc}
100      * @param str {@inheritDoc}
101      * @param off {@inheritDoc}
102      * @param len {@inheritDoc}
103      * @throws IOException {@inheritDoc}
104      */
105     @Override
106     public void write(String str, int off, int len) throws IOException {
107         this.fout.write(str, off, len);
108         this.pout.write(str, off, len);
109         return;
110     }
111
112     /**
113      * {@inheritDoc}
114      * @param c {@inheritDoc}
115      * @throws IOException {@inheritDoc}
116      */
117     @Override
118     public void write(int c) throws IOException {
119         this.fout.write(c);
120         this.pout.write(c);
121         return;
122     }
123
124 }