View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 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  /**
33   * Demonstrates how to read files via FileDescriptors that are exchanged via RMI.
34   *
35   * @author Christian Kohlschütter
36   * @see StreamServer
37   */
38  public final class StreamClient {
39    private StreamClient() {
40      throw new IllegalStateException("No instances");
41    }
42  
43    /**
44     * {@link StreamClient} command-line tool.
45     *
46     * @param args Command-line arguments.
47     * @throws IOException on error.
48     * @throws NotBoundException if the server cannot be reached.
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); // NOPMD
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  }