--- /dev/null
+/*\r
+ * OutputStream associated with HttpURLConnection with counter\r
+ *\r
+ * Copyright(c) 2009 olyutorskii\r
+ * $Id: TallyOutputStream.java 953 2009-12-06 16:42:14Z olyutorskii $\r
+ */\r
+\r
+package jp.sourceforge.jindolf;\r
+\r
+import java.io.BufferedOutputStream;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.net.HttpURLConnection;\r
+\r
+/**\r
+ * 書き込みバイト数をログ出力するHTTPコネクション由来のOutputStream。\r
+ */\r
+public class TallyOutputStream extends OutputStream{\r
+\r
+ private static final int BUFSIZE = 512;\r
+\r
+ /**\r
+ * HTTPコネクションから出力ストリームを得る。\r
+ * @param conn HTTPコネクション\r
+ * @return 出力ストリーム\r
+ * @throws java.io.IOException 入出力エラー\r
+ */\r
+ public static OutputStream getOutputStream(HttpURLConnection conn)\r
+ throws IOException{\r
+ return new TallyOutputStream(conn);\r
+ }\r
+\r
+ private final HttpURLConnection conn;\r
+ private final OutputStream out;\r
+ private long counter;\r
+ private long nanoLap;\r
+\r
+ /**\r
+ * コンストラクタ。\r
+ * @param conn HTTPコネクション\r
+ * @throws java.io.IOException 入出力エラー\r
+ */\r
+ protected TallyOutputStream(HttpURLConnection conn)\r
+ throws IOException{\r
+ super();\r
+\r
+ this.conn = conn;\r
+ this.counter = 0;\r
+ this.nanoLap = 0;\r
+\r
+ OutputStream os;\r
+ os = this.conn.getOutputStream();\r
+ os = new BufferedOutputStream(os, BUFSIZE);\r
+ this.out = os;\r
+\r
+ return;\r
+ }\r
+\r
+ /**\r
+ * 書き込みバイト数を返す。\r
+ * @return 書き込みバイト数。\r
+ */\r
+ protected long getCount(){\r
+ return this.counter;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ * 今までに書き込んだバイト数のスループットをログ出力する。\r
+ * @throws java.io.IOException {@inheritDoc}\r
+ */\r
+ @Override\r
+ public void close() throws IOException{\r
+ this.out.close();\r
+\r
+ long size = getCount();\r
+ long span = System.nanoTime() - this.nanoLap;\r
+\r
+ String message = HttpUtils.formatHttpStat(this.conn, size, span);\r
+ Jindolf.logger().info(message);\r
+\r
+ return;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ * @throws java.io.IOException {@inheritDoc}\r
+ */\r
+ @Override\r
+ public void flush() throws IOException{\r
+ this.out.flush();\r
+ return;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ * @param b {@inheritDoc}\r
+ * @throws java.io.IOException {@inheritDoc}\r
+ */\r
+ @Override\r
+ public void write(byte[] b) throws IOException{\r
+ if(this.counter <= 0) this.nanoLap = System.nanoTime();\r
+\r
+ this.out.write(b);\r
+ this.counter += b.length;\r
+\r
+ return;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ * @param b {@inheritDoc}\r
+ * @param off {@inheritDoc}\r
+ * @param len {@inheritDoc}\r
+ * @throws java.io.IOException {@inheritDoc}\r
+ */\r
+ @Override\r
+ public void write(byte[] b, int off, int len) throws IOException{\r
+ if(this.counter <= 0) this.nanoLap = System.nanoTime();\r
+\r
+ this.out.write(b, off, len);\r
+ this.counter += len;\r
+\r
+ return;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ * @param b {@inheritDoc}\r
+ * @throws java.io.IOException {@inheritDoc}\r
+ */\r
+ public void write(int b) throws IOException{\r
+ if(this.counter <= 0) this.nanoLap = System.nanoTime();\r
+\r
+ this.out.write(b);\r
+ this.counter++;\r
+\r
+ return;\r
+ }\r
+\r
+}\r