2 * OutputStream associated with HttpURLConnection with counter
\r
4 * License : The MIT License
\r
5 * Copyright(c) 2009 olyutorskii
\r
8 package jp.sourceforge.jindolf;
\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
16 * 書き込みバイト数をログ出力するHTTPコネクション由来のOutputStream。
\r
18 public class TallyOutputStream extends OutputStream{
\r
20 private static final int BUFSIZE = 512;
\r
23 * HTTPコネクションから出力ストリームを得る。
\r
24 * @param conn HTTPコネクション
\r
26 * @throws java.io.IOException 入出力エラー
\r
28 public static OutputStream getOutputStream(HttpURLConnection conn)
\r
30 return new TallyOutputStream(conn);
\r
33 private final HttpURLConnection conn;
\r
34 private final OutputStream out;
\r
35 private long counter;
\r
36 private long nanoLap;
\r
40 * @param conn HTTPコネクション
\r
41 * @throws java.io.IOException 入出力エラー
\r
43 protected TallyOutputStream(HttpURLConnection conn)
\r
52 os = this.conn.getOutputStream();
\r
53 os = new BufferedOutputStream(os, BUFSIZE);
\r
63 protected long getCount(){
\r
64 return this.counter;
\r
69 * 今までに書き込んだバイト数のスループットをログ出力する。
\r
70 * @throws java.io.IOException {@inheritDoc}
\r
73 public void close() throws IOException{
\r
76 long size = getCount();
\r
77 long span = System.nanoTime() - this.nanoLap;
\r
79 String message = HttpUtils.formatHttpStat(this.conn, size, span);
\r
80 Jindolf.logger().info(message);
\r
87 * @throws java.io.IOException {@inheritDoc}
\r
90 public void flush() throws IOException{
\r
97 * @param b {@inheritDoc}
\r
98 * @throws java.io.IOException {@inheritDoc}
\r
101 public void write(byte[] b) throws IOException{
\r
102 if(this.counter <= 0) this.nanoLap = System.nanoTime();
\r
105 this.counter += b.length;
\r
112 * @param b {@inheritDoc}
\r
113 * @param off {@inheritDoc}
\r
114 * @param len {@inheritDoc}
\r
115 * @throws java.io.IOException {@inheritDoc}
\r
118 public void write(byte[] b, int off, int len) throws IOException{
\r
119 if(this.counter <= 0) this.nanoLap = System.nanoTime();
\r
121 this.out.write(b, off, len);
\r
122 this.counter += len;
\r
129 * @param b {@inheritDoc}
\r
130 * @throws java.io.IOException {@inheritDoc}
\r
132 public void write(int b) throws IOException{
\r
133 if(this.counter <= 0) this.nanoLap = System.nanoTime();
\r