View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2026 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Demonstrates how to read files via FileDescriptors that are exchanged via RMI.
36   *
37   * @author Christian Kohlschütter
38   * @see StreamServer
39   */
40  public final class StreamClient {
41    private StreamClient() {
42      throw new IllegalStateException("No instances");
43    }
44  
45    /**
46     * {@link StreamClient} command-line tool.
47     *
48     * @param args Command-line arguments.
49     * @throws IOException on error.
50     * @throws NotBoundException if the server cannot be reached.
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); // NOPMD
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  }