00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.ext.benchsocket;
00032 
00033 import java.io.IOException;
00034 import java.io.OutputStream;
00035 
00036 
00037 public class BenchOutputStream extends OutputStream implements BenchStream {
00038     private OutputStream realOutputStream;
00039     private int total;
00040     private int number;
00041     private BenchClientSocket parent;
00042     private ShutdownThread shThread;
00043 
00044     public BenchOutputStream(OutputStream o, int number) {
00045         this.realOutputStream = o;
00046         this.number = number;
00047 
00048         
00049         
00050         try {
00051             shThread = new ShutdownThread(this);
00052             Runtime.getRuntime().addShutdownHook(shThread);
00053         } catch (Exception e) {
00054             
00055         }
00056 
00057         
00058     }
00059 
00060     public BenchOutputStream(OutputStream o, int number,
00061         BenchClientSocket parent) {
00062         this(o, number);
00063         this.parent = parent;
00064     }
00065 
00066     public void write(int b) throws IOException {
00067         if (BenchSocketFactory.measure) {
00068             total++;
00069         }
00070         this.realOutputStream.write(b);
00071     }
00072 
00073     public void write(byte[] b, int off, int len) throws IOException {
00074         if (BenchSocketFactory.measure) {
00075             total += len;
00076         }
00077 
00078         this.realOutputStream.write(b, off, len);
00079     }
00080 
00081     public void write(byte[] b) throws IOException {
00082         if (BenchSocketFactory.measure) {
00083             total += b.length;
00084         }
00085         this.realOutputStream.write(b);
00086     }
00087 
00088     public synchronized void displayTotal() {
00089         display("=== Total Output for socket ");
00090         total = 0;
00091     }
00092 
00093     public synchronized void dumpIntermediateResults() {
00094         display("---- Intermediate output for socket ");
00095     }
00096 
00097     protected void display(String s) {
00098         if (parent != null) {
00099             System.out.println(s + "" + number + " = " + total + " real " +
00100                 parent);
00101         } else {
00102             System.out.println(s + "" + number + " = " + total);
00103         }
00104     }
00105 
00106     public void close() throws IOException {
00107         
00108         if (this.realOutputStream != null) {
00109             this.realOutputStream.close();
00110         }
00111 
00112         
00113         this.displayTotal();
00114 
00115         
00116         
00117         
00118         try {
00119             Runtime.getRuntime().removeShutdownHook(shThread);
00120         } catch (Exception e) {
00121             
00122         }
00123         if (shThread != null) {
00124             shThread.fakeRun();
00125         }
00126         shThread = null;
00127         this.parent = null;
00128     }
00129 }