1 package com.knowgate.dfs; 2 3 14 import java.io.InputStream ; 15 import java.io.FileInputStream ; 16 import java.io.BufferedInputStream ; 17 import java.io.OutputStream ; 18 import java.io.IOException ; 19 import java.io.FileNotFoundException ; 20 21 import com.knowgate.debug.DebugFile; 22 23 35 public class StreamPipe { 36 39 private static final int defaultBufferSize = 8000; 40 boolean bSynchronous; 41 46 public StreamPipe () { 47 bSynchronous = true; 48 } 49 50 55 public StreamPipe (boolean bSync) { 56 bSynchronous = bSync; 57 } 58 59 61 private static void pipe (InputStream in, OutputStream out, int bufferSize, boolean autoFlush) 62 throws IOException { 63 64 if (DebugFile.trace) { 65 DebugFile.writeln("Begin StreamPipe.pipe()"); 66 } 67 68 int total = 0; 69 byte[] buffer = new byte[bufferSize]; 70 71 int length; 72 while ((length = in.read(buffer)) > 0) { 73 if (DebugFile.trace) DebugFile.writeln("OutputStream.write(byte[], 0, " + String.valueOf(length)+")"); 74 out.write(buffer, 0, length); 75 if (autoFlush) out.flush(); 76 total += length; 77 } 78 79 if (DebugFile.trace) { 80 DebugFile.writeln("End StreamPipe.pipe() : " + String.valueOf(total)); 81 } 82 } 84 86 92 public void between(InputStream in,OutputStream out, int bufferSize, boolean autoFlush) 93 throws IOException 94 { 95 if (bSynchronous) 96 pipe(in, out, bufferSize, autoFlush); 97 else 98 (new PipeThread(in, out, bufferSize, autoFlush)).start(); 99 } 100 101 105 public void between(InputStream in, OutputStream out, int bufferSize) 106 throws IOException 107 { 108 if (bSynchronous) 109 pipe(in, out, bufferSize, false); 110 else 111 (new PipeThread(in, out, bufferSize, false)).start(); 112 } 113 114 118 public void between(InputStream in, OutputStream out) 119 throws IOException { 120 121 if (bSynchronous) 122 pipe(in, out, defaultBufferSize, false); 123 else 124 (new PipeThread(in, out, defaultBufferSize, false)).start(); 125 } 126 127 129 136 public static void between(String sFilePath, OutputStream oOutStrm) 137 throws IOException ,FileNotFoundException { 138 139 FileInputStream oFileIoStrm = new FileInputStream (sFilePath); 140 BufferedInputStream oBfIoStrm = new BufferedInputStream (oFileIoStrm, defaultBufferSize); 141 pipe(oBfIoStrm, oOutStrm, 8000, false); 142 oBfIoStrm.close(); 143 oFileIoStrm.close(); 144 } } 146 147 149 final class PipeThread extends Thread { 150 InputStream in; 151 OutputStream out; 152 byte[] buffer; 153 boolean flush; 154 155 PipeThread(InputStream in, OutputStream out, int bufferSize, boolean autoFlush) { 156 this.in = in; 157 this.out = out; 158 buffer = new byte[bufferSize]; 159 flush = autoFlush; 160 161 } 162 163 public void run() { 164 if (DebugFile.trace) { 165 DebugFile.writeln("Begin StreamPipe.PipeThread.run()"); 166 } 167 168 int total = 0; 169 170 try { 171 int length; 172 while ((length = in.read(buffer)) > 0) { 173 if (DebugFile.trace) DebugFile.writeln("OutputStream.write(byte[], 0, " + String.valueOf(length)+")"); 174 out.write(buffer, 0, length); 175 if (flush) out.flush(); 176 total += length; 177 } 178 179 } catch(IOException ex) { } 180 181 if (DebugFile.trace) { 182 DebugFile.writeln("End StreamPipe.PipeThread.run() : " + String.valueOf(total)); 183 } 184 } 185 } 186 | Popular Tags |