OSDN Git Service

dd2deeacd5f0e82b5f4a70d57d58c5f100c811a6
[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      * HTTPコネクションから出力ストリームを得る。\r
24      * @param conn HTTPコネクション\r
25      * @return 出力ストリーム\r
26      * @throws java.io.IOException 入出力エラー\r
27      */\r
28     public static OutputStream getOutputStream(HttpURLConnection conn)\r
29             throws IOException{\r
30         return new TallyOutputStream(conn);\r
31     }\r
32 \r
33     private final HttpURLConnection conn;\r
34     private final OutputStream out;\r
35     private long counter;\r
36     private long nanoLap;\r
37 \r
38     /**\r
39      * コンストラクタ。\r
40      * @param conn HTTPコネクション\r
41      * @throws java.io.IOException 入出力エラー\r
42      */\r
43     protected TallyOutputStream(HttpURLConnection conn)\r
44             throws IOException{\r
45         super();\r
46 \r
47         this.conn = conn;\r
48         this.counter = 0;\r
49         this.nanoLap = 0;\r
50 \r
51         OutputStream os;\r
52         os = this.conn.getOutputStream();\r
53         os = new BufferedOutputStream(os, BUFSIZE);\r
54         this.out = os;\r
55 \r
56         return;\r
57     }\r
58 \r
59     /**\r
60      * 書き込みバイト数を返す。\r
61      * @return 書き込みバイト数。\r
62      */\r
63     protected long getCount(){\r
64         return this.counter;\r
65     }\r
66 \r
67     /**\r
68      * {@inheritDoc}\r
69      * 今までに書き込んだバイト数のスループットをログ出力する。\r
70      * @throws java.io.IOException {@inheritDoc}\r
71      */\r
72     @Override\r
73     public void close() throws IOException{\r
74         this.out.close();\r
75 \r
76         long size = getCount();\r
77         long span = System.nanoTime() - this.nanoLap;\r
78 \r
79         String message = HttpUtils.formatHttpStat(this.conn, size, span);\r
80         Jindolf.logger().info(message);\r
81 \r
82         return;\r
83     }\r
84 \r
85     /**\r
86      * {@inheritDoc}\r
87      * @throws java.io.IOException {@inheritDoc}\r
88      */\r
89     @Override\r
90     public void flush() throws IOException{\r
91         this.out.flush();\r
92         return;\r
93     }\r
94 \r
95     /**\r
96      * {@inheritDoc}\r
97      * @param b {@inheritDoc}\r
98      * @throws java.io.IOException {@inheritDoc}\r
99      */\r
100     @Override\r
101     public void write(byte[] b) throws IOException{\r
102         if(this.counter <= 0) this.nanoLap = System.nanoTime();\r
103 \r
104         this.out.write(b);\r
105         this.counter += b.length;\r
106 \r
107         return;\r
108     }\r
109 \r
110     /**\r
111      * {@inheritDoc}\r
112      * @param b {@inheritDoc}\r
113      * @param off {@inheritDoc}\r
114      * @param len {@inheritDoc}\r
115      * @throws java.io.IOException {@inheritDoc}\r
116      */\r
117     @Override\r
118     public void write(byte[] b, int off, int len) throws IOException{\r
119         if(this.counter <= 0) this.nanoLap = System.nanoTime();\r
120 \r
121         this.out.write(b, off, len);\r
122         this.counter += len;\r
123 \r
124         return;\r
125     }\r
126 \r
127     /**\r
128      * {@inheritDoc}\r
129      * @param b {@inheritDoc}\r
130      * @throws java.io.IOException {@inheritDoc}\r
131      */\r
132     public void write(int b) throws IOException{\r
133         if(this.counter <= 0) this.nanoLap = System.nanoTime();\r
134 \r
135         this.out.write(b);\r
136         this.counter++;\r
137 \r
138         return;\r
139     }\r
140 \r
141 }\r