IORandomAccessFile.java

  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. package org.apache.commons.io;

  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.RandomAccessFile;
  21. import java.util.Objects;

  22. /**
  23.  * Extends {@link RandomAccessFile} to provide access to the {@link File} and {@code mode} passed on construction.
  24.  *
  25.  * @since 2.18.0
  26.  * @see RandomAccessFile
  27.  * @see RandomAccessFileMode
  28.  */
  29. public final class IORandomAccessFile extends RandomAccessFile {

  30.     private final File file;
  31.     private final String mode;

  32.     /**
  33.      * Constructs a new instance by calling {@link RandomAccessFile#RandomAccessFile(File, String)}.
  34.      *
  35.      * @param file the file object
  36.      * @param mode the access mode, as described in {@link RandomAccessFile#RandomAccessFile(File, String)}.
  37.      * @throws FileNotFoundException Thrown by {@link RandomAccessFile#RandomAccessFile(File, String)}.
  38.      * @see RandomAccessFile#RandomAccessFile(File, String)
  39.      */
  40.     public IORandomAccessFile(final File file, final String mode) throws FileNotFoundException {
  41.         super(file, mode);
  42.         this.file = file;
  43.         this.mode = mode;
  44.     }

  45.     /**
  46.      * Constructs a new instance by calling {@link RandomAccessFile#RandomAccessFile(String, String)}.
  47.      *
  48.      * @param name the file object
  49.      * @param mode the access mode, as described in {@link RandomAccessFile#RandomAccessFile(String, String)}.
  50.      * @throws FileNotFoundException Thrown by {@link RandomAccessFile#RandomAccessFile(String, String)}.
  51.      * @see RandomAccessFile#RandomAccessFile(String, String)
  52.      */
  53.     public IORandomAccessFile(final String name, final String mode) throws FileNotFoundException {
  54.         super(name, mode);
  55.         this.file = name != null ? new File(name) : null;
  56.         this.mode = mode;
  57.     }

  58.     /**
  59.      * Gets the file passed to {@link #IORandomAccessFile(File, String)}.
  60.      *
  61.      * @return the file passed to {@link #IORandomAccessFile(File, String)}.
  62.      */
  63.     public File getFile() {
  64.         return file;
  65.     }

  66.     /**
  67.      * Gets the mode passed to {@link #IORandomAccessFile(File, String)}.
  68.      *
  69.      * @return the mode passed to {@link #IORandomAccessFile(File, String)}.
  70.      */
  71.     public String getMode() {
  72.         return mode;
  73.     }

  74.     /**
  75.      * Returns the pathname string of this abstract pathname. This is just the string returned by the {@link File#toString()} method.
  76.      *
  77.      * @return The string form of the File's abstract pathname.
  78.      * @see File#toString()
  79.      */
  80.     @Override
  81.     public String toString() {
  82.         return Objects.toString(file);
  83.     }

  84. }