OSDN Git Service

Merge branch 'Branch_release-'
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / net / TallyOutputStream.java
-/*\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
+/*
+ * OutputStream associated with HttpURLConnection with counter
+ *
+ * License : The MIT License
+ * Copyright(c) 2009 olyutorskii
+ */
+
+package jp.sfjp.jindolf.net;
+
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.util.logging.Logger;
+
+/**
+ * 書き込みバイト数をログ出力するHTTPコネクション由来のOutputStream。
+ */
+public class TallyOutputStream extends OutputStream{
+
+    private static final int BUFSIZE = 512;
+
+    private static final Logger LOGGER = Logger.getAnonymousLogger();
+
+
+    private final HttpURLConnection conn;
+    private final OutputStream out;
+    private long counter;
+    private long nanoLap;
+
+
+    /**
+     * コンストラクタ。
+     * @param conn HTTPコネクション
+     * @throws java.io.IOException 入出力エラー
+     */
+    protected TallyOutputStream(HttpURLConnection conn)
+            throws IOException{
+        super();
+
+        this.conn = conn;
+        this.counter = 0;
+        this.nanoLap = 0;
+
+        OutputStream os;
+        os = this.conn.getOutputStream();
+        os = new BufferedOutputStream(os, BUFSIZE);
+        this.out = os;
+
+        return;
+    }
+
+
+    /**
+     * HTTPコネクションから出力ストリームを得る。
+     * @param conn HTTPコネクション
+     * @return 出力ストリーム
+     * @throws java.io.IOException 入出力エラー
+     */
+    public static OutputStream getOutputStream(HttpURLConnection conn)
+            throws IOException{
+        return new TallyOutputStream(conn);
+    }
+
+    /**
+     * 書き込みバイト数を返す。
+     * @return 書き込みバイト数。
+     */
+    protected long getCount(){
+        return this.counter;
+    }
+
+    /**
+     * {@inheritDoc}
+     * 今までに書き込んだバイト数のスループットをログ出力する。
+     * @throws java.io.IOException {@inheritDoc}
+     */
+    @Override
+    public void close() throws IOException{
+        this.out.close();
+
+        long size = getCount();
+        long span = System.nanoTime() - this.nanoLap;
+
+        String message = HttpUtils.formatHttpStat(this.conn, size, span);
+        LOGGER.info(message);
+
+        return;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @throws java.io.IOException {@inheritDoc}
+     */
+    @Override
+    public void flush() throws IOException{
+        this.out.flush();
+        return;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @param b {@inheritDoc}
+     * @throws java.io.IOException {@inheritDoc}
+     */
+    @Override
+    public void write(byte[] b) throws IOException{
+        if(this.counter <= 0) this.nanoLap = System.nanoTime();
+
+        this.out.write(b);
+        this.counter += b.length;
+
+        return;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @param b {@inheritDoc}
+     * @param off {@inheritDoc}
+     * @param len {@inheritDoc}
+     * @throws java.io.IOException {@inheritDoc}
+     */
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException{
+        if(this.counter <= 0) this.nanoLap = System.nanoTime();
+
+        this.out.write(b, off, len);
+        this.counter += len;
+
+        return;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @param b {@inheritDoc}
+     * @throws java.io.IOException {@inheritDoc}
+     */
+    @Override
+    public void write(int b) throws IOException{
+        if(this.counter <= 0) this.nanoLap = System.nanoTime();
+
+        this.out.write(b);
+        this.counter++;
+
+        return;
+    }
+
+}