1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  package org.newsclub.net.unix.demo.rmi.fd;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.rmi.NotBoundException;
24  import java.rmi.registry.Registry;
25  
26  import org.newsclub.net.unix.demo.rmi.services.StreamService;
27  import org.newsclub.net.unix.rmi.AFNaming;
28  import org.newsclub.net.unix.rmi.AFUNIXNaming;
29  import org.newsclub.net.unix.rmi.RemoteCloseable;
30  import org.newsclub.net.unix.rmi.RemoteFileInput;
31  
32  
33  
34  
35  
36  
37  
38  public final class StreamClient {
39    private StreamClient() {
40      throw new IllegalStateException("No instances");
41    }
42  
43    
44  
45  
46  
47  
48  
49  
50    public static void main(String[] args) throws IOException, NotBoundException {
51      if (args.length != 1) {
52        System.err.println("Usage: StreamClient <path-to-file>");
53        System.exit(1); 
54        return;
55      } else if ("--help".equals(args[0]) || "-h".equals(args[0])) {
56        System.out.println("Usage: StreamClient <path-to-file>");
57        System.out.println();
58        System.out.println("Examples:");
59        System.out.println("    StreamClient /etc/hosts");
60        System.out.println("    StreamClient /etc/master.passwd");
61      }
62  
63      AFNaming naming = AFUNIXNaming.getInstance();
64  
65      final Registry registry = naming.getRegistry();
66      StreamService obj = (StreamService) registry.lookup("streamService");
67  
68      File file = new File(args[0]);
69      System.out.println("Trying to read " + file);
70  
71      try (RemoteCloseable<RemoteFileInput> rc = obj.openForReading(file)) {
72        try (RemoteFileInput rfin = rc.get(); FileInputStream fin = rfin.asFileInputStream()) {
73          byte[] data = new byte[4096];
74  
75          int len;
76          while ((len = fin.read(data)) != -1) {
77            System.out.write(data, 0, len);
78          }
79        }
80      }
81    }
82  }