View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.io;
19  
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.io.RandomAccessFile;
23  import java.util.Objects;
24  
25  /**
26   * Extends {@link RandomAccessFile} to provide access to the {@link File} and {@code mode} passed on construction.
27   *
28   * @since 2.18.0
29   * @see RandomAccessFile
30   * @see RandomAccessFileMode
31   */
32  public final class IORandomAccessFile extends RandomAccessFile {
33  
34      private final File file;
35      private final String mode;
36  
37      /**
38       * Constructs a new instance by calling {@link RandomAccessFile#RandomAccessFile(File, String)}.
39       *
40       * @param file the file object
41       * @param mode the access mode, as described in {@link RandomAccessFile#RandomAccessFile(File, String)}.
42       * @throws FileNotFoundException Thrown by {@link RandomAccessFile#RandomAccessFile(File, String)}.
43       * @see RandomAccessFile#RandomAccessFile(File, String)
44       */
45      public IORandomAccessFile(final File file, final String mode) throws FileNotFoundException {
46          super(file, mode);
47          this.file = file;
48          this.mode = mode;
49      }
50  
51      /**
52       * Constructs a new instance by calling {@link RandomAccessFile#RandomAccessFile(String, String)}.
53       *
54       * @param name the file object
55       * @param mode the access mode, as described in {@link RandomAccessFile#RandomAccessFile(String, String)}.
56       * @throws FileNotFoundException Thrown by {@link RandomAccessFile#RandomAccessFile(String, String)}.
57       * @see RandomAccessFile#RandomAccessFile(String, String)
58       */
59      public IORandomAccessFile(final String name, final String mode) throws FileNotFoundException {
60          super(name, mode);
61          this.file = name != null ? new File(name) : null;
62          this.mode = mode;
63      }
64  
65      /**
66       * Gets the file passed to {@link #IORandomAccessFile(File, String)}.
67       *
68       * @return the file passed to {@link #IORandomAccessFile(File, String)}.
69       */
70      public File getFile() {
71          return file;
72      }
73  
74      /**
75       * Gets the mode passed to {@link #IORandomAccessFile(File, String)}.
76       *
77       * @return the mode passed to {@link #IORandomAccessFile(File, String)}.
78       */
79      public String getMode() {
80          return mode;
81      }
82  
83      /**
84       * Returns the pathname string of this abstract pathname. This is just the string returned by the {@link File#toString()} method.
85       *
86       * @return The string form of the File's abstract pathname.
87       * @see File#toString()
88       */
89      @Override
90      public String toString() {
91          return Objects.toString(file);
92      }
93  
94  }