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 import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
33
34
35
36
37
38
39
40 public final class StreamClient {
41 private StreamClient() {
42 throw new IllegalStateException("No instances");
43 }
44
45
46
47
48
49
50
51
52 @SuppressFBWarnings("PATH_TRAVERSAL_IN")
53 public static void main(String[] args) throws IOException, NotBoundException {
54 if (args.length != 1) {
55 System.err.println("Usage: StreamClient <path-to-file>");
56 System.exit(1);
57 return;
58 } else if ("--help".equals(args[0]) || "-h".equals(args[0])) {
59 System.out.println("Usage: StreamClient <path-to-file>");
60 System.out.println();
61 System.out.println("Examples:");
62 System.out.println(" StreamClient /etc/hosts");
63 System.out.println(" StreamClient /etc/master.passwd");
64 }
65
66 AFNaming naming = AFUNIXNaming.getInstance();
67
68 final Registry registry = naming.getRegistry();
69 StreamService obj = (StreamService) registry.lookup("streamService");
70
71 File file = new File(args[0]);
72 System.out.println("Trying to read " + file);
73
74 try (RemoteCloseable<RemoteFileInput> rc = obj.openForReading(file)) {
75 try (RemoteFileInput rfin = rc.get(); FileInputStream fin = rfin.asFileInputStream()) {
76 byte[] data = new byte[4096];
77
78 int len;
79 while ((len = fin.read(data)) != -1) {
80 System.out.write(data, 0, len);
81 }
82 }
83 }
84 }
85 }