OSDN Git Service

スタートアップ処理の改善
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / net / TallyInputStream.java
1 /*
2  * InputStream 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.BufferedInputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.net.HttpURLConnection;
14 import java.util.logging.Logger;
15
16 /**
17  * 読み込みバイト数を記録するHTTPコネクション由来のInputStream。
18  * バッファリングも行う。
19  */
20 public class TallyInputStream extends InputStream{
21
22     private static final int BUFSIZE = 2 * 1024;
23
24     private static final Logger LOGGER = Logger.getAnonymousLogger();
25
26
27     private final HttpURLConnection conn;
28     private final InputStream in;
29     private long counter;
30     private long nanoLap;
31     private boolean hasClosed;
32
33
34     /**
35      * コンストラクタ。
36      * @param conn HTTPコネクション
37      * @throws java.io.IOException 入出力エラー
38      */
39     protected TallyInputStream(HttpURLConnection conn) throws IOException{
40         super();
41
42         this.conn = conn;
43         this.counter = 0;
44         this.nanoLap = 0;
45
46         InputStream is;
47         is = this.conn.getInputStream();
48         is = new BufferedInputStream(is, BUFSIZE);
49         this.in = is;
50
51         this.hasClosed = false;
52
53         return;
54     }
55
56
57     /**
58      * HTTPコネクションから入力ストリームを得る。
59      * @param conn HTTPコネクション
60      * @return 入力ストリーム
61      * @throws java.io.IOException 入出力エラー
62      */
63     public static InputStream getInputStream(HttpURLConnection conn)
64             throws IOException{
65         return new TallyInputStream(conn);
66     }
67
68     /**
69      * 読み込みバイト数を返す。
70      * @return 読み込みバイト数。
71      */
72     protected long getCount(){
73         return this.counter;
74     }
75
76     /**
77      * {@inheritDoc}
78      *
79      * @return {@inheritDoc}
80      * @throws java.io.IOException {@inheritDoc}
81      */
82     @Override
83     public int available() throws IOException{
84         int bytes = this.in.available();
85         return bytes;
86     }
87
88     /**
89      * {@inheritDoc}
90      * 今までに読み込んだバイト数のスループットをログ出力する。
91      * @throws java.io.IOException {@inheritDoc}
92      */
93     @Override
94     public void close() throws IOException{
95         if(this.hasClosed) return;
96
97         this.in.close();
98
99         long size = getCount();
100         long span = System.nanoTime() - this.nanoLap;
101
102         String message = HttpUtils.formatHttpStat(this.conn, size, span);
103         LOGGER.info(message);
104
105         this.hasClosed = true;
106
107         return;
108     }
109
110     /**
111      * {@inheritDoc}
112      *
113      * @return {@inheritDoc}
114      * @throws java.io.IOException {@inheritDoc}
115      */
116     @Override
117     public int read() throws IOException{
118         if(this.counter <= 0) this.nanoLap = System.nanoTime();
119
120         int byteData = this.in.read();
121         if(byteData >= 0) this.counter++;
122
123         return byteData;
124     }
125
126     /**
127      * {@inheritDoc}
128      *
129      * @param buf {@inheritDoc}
130      * @return {@inheritDoc}
131      * @throws java.io.IOException {@inheritDoc}
132      */
133     @Override
134     public int read(byte[] buf) throws IOException{
135         if(this.counter <= 0) this.nanoLap = System.nanoTime();
136
137         int count = this.in.read(buf);
138         if(count >= 0) this.counter += count;
139
140         return count;
141     }
142
143     /**
144      * {@inheritDoc}
145      *
146      * @param buf {@inheritDoc}
147      * @param off {@inheritDoc}
148      * @param len {@inheritDoc}
149      * @return {@inheritDoc}
150      * @throws java.io.IOException {@inheritDoc}
151      */
152     @Override
153     public int read(byte[] buf, int off, int len) throws IOException{
154         if(this.counter <= 0) this.nanoLap = System.nanoTime();
155
156         int count = this.in.read(buf, off, len);
157         if(count >= 0) this.counter += count;
158
159         return count;
160     }
161
162     /**
163      * {@inheritDoc}
164      *
165      * @param n {@inheritDoc}
166      * @return {@inheritDoc}
167      * @throws java.io.IOException {@inheritDoc}
168      */
169     @Override
170     public long skip(long n) throws IOException{
171         if(this.counter <= 0) this.nanoLap = System.nanoTime();
172
173         long skipped = this.in.skip(n);
174         this.counter += skipped;
175
176         return skipped;
177     }
178
179 }