001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io;
019
020import java.io.File;
021import java.io.FileNotFoundException;
022import java.io.RandomAccessFile;
023import java.util.Objects;
024
025/**
026 * Extends {@link RandomAccessFile} to provide access to the {@link File} and {@code mode} passed on construction.
027 *
028 * @since 2.18.0
029 * @see RandomAccessFile
030 * @see RandomAccessFileMode
031 */
032public final class IORandomAccessFile extends RandomAccessFile {
033
034    private final File file;
035    private final String mode;
036
037    /**
038     * Constructs a new instance by calling {@link RandomAccessFile#RandomAccessFile(File, String)}.
039     *
040     * @param file the file object
041     * @param mode the access mode, as described in {@link RandomAccessFile#RandomAccessFile(File, String)}.
042     * @throws FileNotFoundException Thrown by {@link RandomAccessFile#RandomAccessFile(File, String)}.
043     * @see RandomAccessFile#RandomAccessFile(File, String)
044     */
045    public IORandomAccessFile(final File file, final String mode) throws FileNotFoundException {
046        super(file, mode);
047        this.file = file;
048        this.mode = mode;
049    }
050
051    /**
052     * Constructs a new instance by calling {@link RandomAccessFile#RandomAccessFile(String, String)}.
053     *
054     * @param name the file object
055     * @param mode the access mode, as described in {@link RandomAccessFile#RandomAccessFile(String, String)}.
056     * @throws FileNotFoundException Thrown by {@link RandomAccessFile#RandomAccessFile(String, String)}.
057     * @see RandomAccessFile#RandomAccessFile(String, String)
058     */
059    public IORandomAccessFile(final String name, final String mode) throws FileNotFoundException {
060        super(name, mode);
061        this.file = name != null ? new File(name) : null;
062        this.mode = mode;
063    }
064
065    /**
066     * Gets the file passed to {@link #IORandomAccessFile(File, String)}.
067     *
068     * @return the file passed to {@link #IORandomAccessFile(File, String)}.
069     */
070    public File getFile() {
071        return file;
072    }
073
074    /**
075     * Gets the mode passed to {@link #IORandomAccessFile(File, String)}.
076     *
077     * @return the mode passed to {@link #IORandomAccessFile(File, String)}.
078     */
079    public String getMode() {
080        return mode;
081    }
082
083    /**
084     * Returns the pathname string of this abstract pathname. This is just the string returned by the {@link File#toString()} method.
085     *
086     * @return The string form of the File's abstract pathname.
087     * @see File#toString()
088     */
089    @Override
090    public String toString() {
091        return Objects.toString(file);
092    }
093
094}