OSDN Git Service

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