OSDN Git Service

mainエントリのパッケージを変更。
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / net / TallyOutputStream.java
1 /*
2  * OutputStream associated with HttpURLConnection with counter
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.net;
9
10 import java.io.BufferedOutputStream;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.net.HttpURLConnection;
14 import jp.sfjp.jindolf.log.LogWrapper;
15
16 /**
17  * 書き込みバイト数をログ出力するHTTPコネクション由来のOutputStream。
18  */
19 public class TallyOutputStream extends OutputStream{
20
21     private static final int BUFSIZE = 512;
22
23     private static final LogWrapper LOGGER = new LogWrapper();
24
25
26     private final HttpURLConnection conn;
27     private final OutputStream out;
28     private long counter;
29     private long nanoLap;
30
31
32     /**
33      * コンストラクタ。
34      * @param conn HTTPコネクション
35      * @throws java.io.IOException 入出力エラー
36      */
37     protected TallyOutputStream(HttpURLConnection conn)
38             throws IOException{
39         super();
40
41         this.conn = conn;
42         this.counter = 0;
43         this.nanoLap = 0;
44
45         OutputStream os;
46         os = this.conn.getOutputStream();
47         os = new BufferedOutputStream(os, BUFSIZE);
48         this.out = os;
49
50         return;
51     }
52
53
54     /**
55      * HTTPコネクションから出力ストリームを得る。
56      * @param conn HTTPコネクション
57      * @return 出力ストリーム
58      * @throws java.io.IOException 入出力エラー
59      */
60     public static OutputStream getOutputStream(HttpURLConnection conn)
61             throws IOException{
62         return new TallyOutputStream(conn);
63     }
64
65     /**
66      * 書き込みバイト数を返す。
67      * @return 書き込みバイト数。
68      */
69     protected long getCount(){
70         return this.counter;
71     }
72
73     /**
74      * {@inheritDoc}
75      * 今までに書き込んだバイト数のスループットをログ出力する。
76      * @throws java.io.IOException {@inheritDoc}
77      */
78     @Override
79     public void close() throws IOException{
80         this.out.close();
81
82         long size = getCount();
83         long span = System.nanoTime() - this.nanoLap;
84
85         String message = HttpUtils.formatHttpStat(this.conn, size, span);
86         LOGGER.info(message);
87
88         return;
89     }
90
91     /**
92      * {@inheritDoc}
93      * @throws java.io.IOException {@inheritDoc}
94      */
95     @Override
96     public void flush() throws IOException{
97         this.out.flush();
98         return;
99     }
100
101     /**
102      * {@inheritDoc}
103      * @param b {@inheritDoc}
104      * @throws java.io.IOException {@inheritDoc}
105      */
106     @Override
107     public void write(byte[] b) throws IOException{
108         if(this.counter <= 0) this.nanoLap = System.nanoTime();
109
110         this.out.write(b);
111         this.counter += b.length;
112
113         return;
114     }
115
116     /**
117      * {@inheritDoc}
118      * @param b {@inheritDoc}
119      * @param off {@inheritDoc}
120      * @param len {@inheritDoc}
121      * @throws java.io.IOException {@inheritDoc}
122      */
123     @Override
124     public void write(byte[] b, int off, int len) throws IOException{
125         if(this.counter <= 0) this.nanoLap = System.nanoTime();
126
127         this.out.write(b, off, len);
128         this.counter += len;
129
130         return;
131     }
132
133     /**
134      * {@inheritDoc}
135      * @param b {@inheritDoc}
136      * @throws java.io.IOException {@inheritDoc}
137      */
138     @Override
139     public void write(int b) throws IOException{
140         if(this.counter <= 0) this.nanoLap = System.nanoTime();
141
142         this.out.write(b);
143         this.counter++;
144
145         return;
146     }
147
148 }