org/objectweb/proactive/ext/benchsocket/BenchOutputStream.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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         //we register a hook to be run
00049         //when the JVM is killed
00050         try {
00051             shThread = new ShutdownThread(this);
00052             Runtime.getRuntime().addShutdownHook(shThread);
00053         } catch (Exception e) {
00054             //e.printStackTrace();
00055         }
00056 
00057         //  ShutdownThread.addStream(this);
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         //      if (ShutdownThread.removeStream(this)){
00108         if (this.realOutputStream != null) {
00109             this.realOutputStream.close();
00110         }
00111 
00112         //System.out.println("BenchOutputStream.close() on " + this.number);
00113         this.displayTotal();
00114 
00115         //      }
00116         //no only we remove the thread, but we also fire it
00117         //because of java bug #4533
00118         try {
00119             Runtime.getRuntime().removeShutdownHook(shThread);
00120         } catch (Exception e) {
00121             //  e.printStackTrace();
00122         }
00123         if (shThread != null) {
00124             shThread.fakeRun();
00125         }
00126         shThread = null;
00127         this.parent = null;
00128     }
00129 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1