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 private final HttpURLConnection conn;
\r
24 private final OutputStream out;
\r
25 private long counter;
\r
26 private long nanoLap;
\r
31 * @param conn HTTPコネクション
\r
32 * @throws java.io.IOException 入出力エラー
\r
34 protected TallyOutputStream(HttpURLConnection conn)
\r
43 os = this.conn.getOutputStream();
\r
44 os = new BufferedOutputStream(os, BUFSIZE);
\r
52 * HTTPコネクションから出力ストリームを得る。
\r
53 * @param conn HTTPコネクション
\r
55 * @throws java.io.IOException 入出力エラー
\r
57 public static OutputStream getOutputStream(HttpURLConnection conn)
\r
59 return new TallyOutputStream(conn);
\r
66 protected long getCount(){
\r
67 return this.counter;
\r
72 * 今までに書き込んだバイト数のスループットをログ出力する。
\r
73 * @throws java.io.IOException {@inheritDoc}
\r
76 public void close() throws IOException{
\r
79 long size = getCount();
\r
80 long span = System.nanoTime() - this.nanoLap;
\r
82 String message = HttpUtils.formatHttpStat(this.conn, size, span);
\r
83 Jindolf.logger().info(message);
\r
90 * @throws java.io.IOException {@inheritDoc}
\r
93 public void flush() throws IOException{
\r
100 * @param b {@inheritDoc}
\r
101 * @throws java.io.IOException {@inheritDoc}
\r
104 public void write(byte[] b) throws IOException{
\r
105 if(this.counter <= 0) this.nanoLap = System.nanoTime();
\r
108 this.counter += b.length;
\r
115 * @param b {@inheritDoc}
\r
116 * @param off {@inheritDoc}
\r
117 * @param len {@inheritDoc}
\r
118 * @throws java.io.IOException {@inheritDoc}
\r
121 public void write(byte[] b, int off, int len) throws IOException{
\r
122 if(this.counter <= 0) this.nanoLap = System.nanoTime();
\r
124 this.out.write(b, off, len);
\r
125 this.counter += len;
\r
132 * @param b {@inheritDoc}
\r
133 * @throws java.io.IOException {@inheritDoc}
\r
136 public void write(int b) throws IOException{
\r
137 if(this.counter <= 0) this.nanoLap = System.nanoTime();
\r