KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PeekNamedPipe


1 import jcifs.smb.SmbNamedPipe;
2 import java.io.OutputStream JavaDoc;
3 import java.io.InputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 public class PeekNamedPipe {
7
8     static class ReceiverThread extends Thread JavaDoc {
9         InputStream JavaDoc in;
10         byte[] buf = new byte[20];
11         int n;
12
13         ReceiverThread( InputStream JavaDoc 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 JavaDoc( buf, 0, n ));
29                 }
30             } catch( Exception JavaDoc e ) {
31                 e.printStackTrace();
32             }
33             System.out.println( "run finished" );
34         }
35     }
36
37     public static void main( String JavaDoc[] argv ) throws Exception JavaDoc {
38         SmbNamedPipe pipe = new SmbNamedPipe( argv[0], SmbNamedPipe.PIPE_TYPE_RDWR );
39         InputStream JavaDoc in = pipe.getNamedPipeInputStream();
40         OutputStream JavaDoc out = pipe.getNamedPipeOutputStream();
41
42         ReceiverThread rt = new ReceiverThread( in );
43         rt.start();
44
45         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
46         String JavaDoc 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