org/objectweb/proactive/ext/benchsocket/BenchInputStream.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.InputStream;
00035 
00036 
00037 public class BenchInputStream extends InputStream implements BenchStream {
00038     private InputStream realInputStream;
00039     private int number;
00040     private int total;
00041     private BenchClientSocket parent;
00042     private ShutdownThread shThread;
00043 
00044     public BenchInputStream(InputStream real, int number) {
00045         this.realInputStream = real;
00046         this.number = number;
00047         // ShutdownThread.addStream(this);
00048         //when the JVM is killed
00049         try {
00050             shThread = new ShutdownThread(this);
00051             Runtime.getRuntime().addShutdownHook(shThread);
00052         } catch (Exception e) {
00053             //e.printStackTrace();
00054         }
00055     }
00056 
00057     public BenchInputStream(InputStream stream, int number,
00058         BenchClientSocket parent) {
00059         this(stream, number);
00060         this.parent = parent;
00061     }
00062 
00063     public synchronized void displayTotal() {
00064         display("=== Total Input for socket ");
00065         total = 0;
00066     }
00067 
00068     public synchronized void dumpIntermediateResults() {
00069         display("---- Intermediate input for socket ");
00070     }
00071 
00072     protected void display(String s) {
00073         if (parent != null) {
00074             System.out.println(s + "" + number + " = " + total + " real " +
00075                 parent);
00076         } else {
00077             System.out.println(s + "" + number + " = " + total);
00078         }
00079     }
00080 
00081     /* (non-Javadoc)
00082      * @see java.io.InputStream#available()
00083      */
00084     public int available() throws IOException {
00085         return this.realInputStream.available();
00086     }
00087 
00088     /* (non-Javadoc)
00089      * @see java.io.InputStream#close()
00090      */
00091     public void close() throws IOException {
00092         //              if (ShutdownThread.removeStream(this)){
00093         //                      this.realInputStream.close();
00094         //                      //System.out.println("BenchOutputStream.close() on " + this.number);
00095         //                      this.displayTotal();
00096         //              }
00097         //      if (ShutdownThread.removeStream(this)){
00098         if (this.realInputStream != null) {
00099             this.realInputStream.close();
00100         }
00101 
00102         //System.out.println("BenchOutputStream.close() on " + this.number);
00103         this.displayTotal();
00104         //      }
00105         //no only we remove the thread, but we also fire it
00106         //because of java bug #4533
00107         try {
00108             Runtime.getRuntime().removeShutdownHook(shThread);
00109         } catch (Exception e) {
00110             //e.printStackTrace();
00111         }
00112         if (shThread != null) {
00113             shThread.fakeRun();
00114         }
00115         shThread = null;
00116         this.parent = null;
00117     }
00118 
00119     /* (non-Javadoc)
00120      * @see java.io.InputStream#mark(int)
00121      */
00122     public synchronized void mark(int readlimit) {
00123         this.realInputStream.mark(readlimit);
00124     }
00125 
00126     /* (non-Javadoc)
00127      * @see java.io.InputStream#markSupported()
00128      */
00129     public boolean markSupported() {
00130         return this.realInputStream.markSupported();
00131     }
00132 
00133     public int read() throws IOException {
00134         int tmp = this.realInputStream.read();
00135 
00136         //  System.out.println("BenchInputStream.read() on " + this.number +" " + tmp);
00137         if (BenchSocketFactory.measure) {
00138             total += 1;
00139         }
00140 
00141         // total += tmp;
00142         return tmp;
00143     }
00144 
00145     /* (non-Javadoc)
00146      * @see java.io.InputStream#read(byte[], int, int)
00147      */
00148     public int read(byte[] b, int off, int len) throws IOException {
00149         int tmp = this.realInputStream.read(b, off, len);
00150 
00151         //   System.out.println("BenchInputStream.read(byte[] b, int off, int len) on " + this.number +" " + tmp);
00152         if (BenchSocketFactory.measure) {
00153             total += tmp;
00154         }
00155         return tmp;
00156     }
00157 
00158     /* (non-Javadoc)
00159      * @see java.io.InputStream#read(byte[])
00160      */
00161     public int read(byte[] b) throws IOException {
00162         int tmp = this.realInputStream.read(b);
00163 
00164         // System.out.println("BenchInputStream.read(byte[] b) on " + this.number +" " + tmp);
00165         if (BenchSocketFactory.measure) {
00166             total += tmp;
00167         }
00168         return tmp;
00169     }
00170 
00171     /* (non-Javadoc)
00172      * @see java.io.InputStream#reset()
00173      */
00174     public synchronized void reset() throws IOException {
00175         this.realInputStream.reset();
00176     }
00177 
00178     public long skip(long n) throws IOException {
00179         return this.realInputStream.skip(n);
00180     }
00181 }

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