File: PipeTalk.java

package info (click to toggle)
jcifs 1.3.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,860 kB
  • ctags: 3,997
  • sloc: java: 23,131; xml: 3,672; ansic: 386; sh: 171; makefile: 27
file content (56 lines) | stat: -rw-r--r-- 1,539 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import jcifs.smb.SmbNamedPipe;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;

public class PipeTalk {

    static class ReceiverThread extends Thread {
        InputStream in;
        byte[] buf = new byte[20];
        int n;

        ReceiverThread( InputStream in ) {
            this.in = in;
        }
        public void run() {
            try {
                while(( n = in.read( buf )) != -1 ) {
                    System.out.println( new String( buf, 0, n ));
                }
            } catch( IOException ioe ) {
                ioe.printStackTrace();
            }
        }
    }

    public static void main( String argv[] ) throws Exception {

        SmbNamedPipe pipe = new SmbNamedPipe( argv[0], SmbNamedPipe.PIPE_TYPE_RDWR );
        InputStream in = pipe.getNamedPipeInputStream();
        OutputStream out = pipe.getNamedPipeOutputStream();

        ReceiverThread rt = new ReceiverThread( in );
        rt.start();

        StringBuffer sb = new StringBuffer();
        String msg;
        int c;
        while(( c = System.in.read() ) != -1 ) {
            if( c == '\n' ) {
                msg = sb.toString();
                if( msg.startsWith( "exi" )) {
                    break;
                }
                System.out.println( sb.toString() );
                out.write( msg.getBytes() );
                sb.setLength( 0 );
            } else {
                sb.append( (char)c );
            }
        }
        in.close();
        out.close();
    }
}