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.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         
00048         
00049         try {
00050             shThread = new ShutdownThread(this);
00051             Runtime.getRuntime().addShutdownHook(shThread);
00052         } catch (Exception e) {
00053             
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     
00082 
00083 
00084     public int available() throws IOException {
00085         return this.realInputStream.available();
00086     }
00087 
00088     
00089 
00090 
00091     public void close() throws IOException {
00092         
00093         
00094         
00095         
00096         
00097         
00098         if (this.realInputStream != null) {
00099             this.realInputStream.close();
00100         }
00101 
00102         
00103         this.displayTotal();
00104         
00105         
00106         
00107         try {
00108             Runtime.getRuntime().removeShutdownHook(shThread);
00109         } catch (Exception e) {
00110             
00111         }
00112         if (shThread != null) {
00113             shThread.fakeRun();
00114         }
00115         shThread = null;
00116         this.parent = null;
00117     }
00118 
00119     
00120 
00121 
00122     public synchronized void mark(int readlimit) {
00123         this.realInputStream.mark(readlimit);
00124     }
00125 
00126     
00127 
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         
00137         if (BenchSocketFactory.measure) {
00138             total += 1;
00139         }
00140 
00141         
00142         return tmp;
00143     }
00144 
00145     
00146 
00147 
00148     public int read(byte[] b, int off, int len) throws IOException {
00149         int tmp = this.realInputStream.read(b, off, len);
00150 
00151         
00152         if (BenchSocketFactory.measure) {
00153             total += tmp;
00154         }
00155         return tmp;
00156     }
00157 
00158     
00159 
00160 
00161     public int read(byte[] b) throws IOException {
00162         int tmp = this.realInputStream.read(b);
00163 
00164         
00165         if (BenchSocketFactory.measure) {
00166             total += tmp;
00167         }
00168         return tmp;
00169     }
00170 
00171     
00172 
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 }