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.rmi;
19  
20  import java.io.Closeable;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.ObjectInput;
24  
25  /**
26   * A specialized subclass of {@link RemoteFileDescriptorBase}, specifically for
27   * {@link FileInputStream}s.
28   *
29   * @author Christian Kohlschütter
30   */
31  public final class RemoteFileInput extends RemoteFileDescriptorBase<FileInputStream> implements
32      Closeable {
33    private static final long serialVersionUID = 1L;
34  
35    /**
36     * Creates an uninitialized instance; used for externalization.
37     *
38     * @see #readExternal(ObjectInput)
39     */
40    public RemoteFileInput() {
41      super();
42    }
43  
44    /**
45     * Creates a new {@link RemoteFileInput} instance, encapsulating a {@link FileInputStream} so that
46     * it can be shared with other processes via RMI.
47     *
48     * @param socketFactory The socket factory.
49     * @param fin The {@link FileInputStream}.
50     * @throws IOException if the operation fails.
51     */
52    public RemoteFileInput(AFUNIXRMISocketFactory socketFactory, FileInputStream fin)
53        throws IOException {
54      super(socketFactory, fin, fin.getFD(), RemoteFileDescriptorBase.MAGIC_VALUE_MASK
55          | RemoteFileDescriptorBase.BIT_READABLE);
56    }
57  
58    /**
59     * Returns a FileInputStream for the given instance. This either is the actual instance provided
60     * by the constructor or a new instance created from the file descriptor.
61     *
62     * @return The FileInputStream.
63     * @throws IOException if the operation fails.
64     */
65    public FileInputStream asFileInputStream() throws IOException {
66      if ((getMagicValue() & RemoteFileDescriptorBase.BIT_READABLE) == 0) {
67        throw new IOException("FileDescriptor is not readable");
68      }
69  
70      return resource.accumulateAndGet(null, (prev, x) -> {
71        if (prev != null) {
72          return prev;
73        }
74        return new FileInputStream(getFileDescriptor()) {
75          @Override
76          public void close() throws IOException {
77            super.close();
78  
79            synchronized (RemoteFileInput.this) {
80              RemoteFileInput.this.close();
81            }
82          }
83        };
84      });
85    }
86  }