2 * OutputStream associated with HttpURLConnection with counter
4 * License : The MIT License
5 * Copyright(c) 2009 olyutorskii
8 package jp.sfjp.jindolf.net;
10 import java.io.BufferedOutputStream;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.net.HttpURLConnection;
14 import jp.sfjp.jindolf.log.LogWrapper;
17 * 書き込みバイト数をログ出力するHTTPコネクション由来のOutputStream。
19 public class TallyOutputStream extends OutputStream{
21 private static final int BUFSIZE = 512;
23 private static final LogWrapper LOGGER = new LogWrapper();
26 private final HttpURLConnection conn;
27 private final OutputStream out;
34 * @param conn HTTPコネクション
35 * @throws java.io.IOException 入出力エラー
37 protected TallyOutputStream(HttpURLConnection conn)
46 os = this.conn.getOutputStream();
47 os = new BufferedOutputStream(os, BUFSIZE);
55 * HTTPコネクションから出力ストリームを得る。
56 * @param conn HTTPコネクション
58 * @throws java.io.IOException 入出力エラー
60 public static OutputStream getOutputStream(HttpURLConnection conn)
62 return new TallyOutputStream(conn);
69 protected long getCount(){
75 * 今までに書き込んだバイト数のスループットをログ出力する。
76 * @throws java.io.IOException {@inheritDoc}
79 public void close() throws IOException{
82 long size = getCount();
83 long span = System.nanoTime() - this.nanoLap;
85 String message = HttpUtils.formatHttpStat(this.conn, size, span);
93 * @throws java.io.IOException {@inheritDoc}
96 public void flush() throws IOException{
103 * @param b {@inheritDoc}
104 * @throws java.io.IOException {@inheritDoc}
107 public void write(byte[] b) throws IOException{
108 if(this.counter <= 0) this.nanoLap = System.nanoTime();
111 this.counter += b.length;
118 * @param b {@inheritDoc}
119 * @param off {@inheritDoc}
120 * @param len {@inheritDoc}
121 * @throws java.io.IOException {@inheritDoc}
124 public void write(byte[] b, int off, int len) throws IOException{
125 if(this.counter <= 0) this.nanoLap = System.nanoTime();
127 this.out.write(b, off, len);
135 * @param b {@inheritDoc}
136 * @throws java.io.IOException {@inheritDoc}
139 public void write(int b) throws IOException{
140 if(this.counter <= 0) this.nanoLap = System.nanoTime();