1 import jcifs.smb.SmbNamedPipe; 2 import java.io.OutputStream ; 3 import java.io.InputStream ; 4 import java.io.IOException ; 5 6 public class PeekNamedPipe { 7 8 static class ReceiverThread extends Thread { 9 InputStream in; 10 byte[] buf = new byte[20]; 11 int n; 12 13 ReceiverThread( InputStream in ) { 14 this.in = in; 15 } 16 public void run() { 17 try { 18 while( true ) { 19 while(( n = in.available() ) == 0 ) { 20 System.out.println( "0 available" ); 21 sleep( 3000 ); 22 } 23 System.out.println( n + " available" ); 24 25 if(( n = in.read( buf )) == -1 ) { 26 break; 27 } 28 System.out.println( new String ( buf, 0, n )); 29 } 30 } catch( Exception e ) { 31 e.printStackTrace(); 32 } 33 System.out.println( "run finished" ); 34 } 35 } 36 37 public static void main( String [] argv ) throws Exception { 38 SmbNamedPipe pipe = new SmbNamedPipe( argv[0], SmbNamedPipe.PIPE_TYPE_RDWR ); 39 InputStream in = pipe.getNamedPipeInputStream(); 40 OutputStream out = pipe.getNamedPipeOutputStream(); 41 42 ReceiverThread rt = new ReceiverThread( in ); 43 rt.start(); 44 45 StringBuffer sb = new StringBuffer (); 46 String msg; 47 int c; 48 while(( c = System.in.read() ) != -1 ) { 49 if( c == '\n' ) { 50 msg = sb.toString(); 51 if( msg.startsWith( "exi" )) { 52 break; 53 } 54 out.write( msg.getBytes() ); 55 sb.setLength( 0 ); 56 } else { 57 sb.append( (char)c ); 58 } 59 } 60 in.close(); 61 out.close(); 62 } 63 } 64 65 | Popular Tags |