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 */
017package org.apache.commons.io;
018
019import java.io.File;
020import java.io.FileNotFoundException;
021import java.io.RandomAccessFile;
022import java.nio.file.Path;
023import java.util.Objects;
024
025/**
026 * Access modes and factory methods for {@link RandomAccessFile}.
027 *
028 * @since 2.12.0
029 */
030public enum RandomAccessFileMode {
031
032    /**
033     * Mode {@code "r"} opens for reading only.
034     */
035    READ_ONLY("r"),
036
037    /**
038     * Mode {@code "rw"} opens for reading and writing.
039     */
040    READ_WRITE("rw"),
041
042    /**
043     * Mode {@code "rws"} opens for reading and writing, as with {@code "rw"}, and also require that every update to the file's content or metadata be written
044     * synchronously to the underlying storage device.
045     */
046    READ_WRITE_SYNC_ALL("rws"),
047
048    /**
049     * Mode {@code "rwd"} open for reading and writing, as with {@code "rw"}, and also require that every update to the file's content be written synchronously
050     * to the underlying storage device.
051     */
052    READ_WRITE_SYNC_CONTENT("rwd");
053
054    private final String mode;
055
056    RandomAccessFileMode(final String mode) {
057        this.mode = mode;
058    }
059
060    /**
061     * Constructs a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
062     * argument.
063     *
064     * @param file the file object
065     * @return a random access file stream
066     * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
067     */
068    public RandomAccessFile create(final File file) throws FileNotFoundException {
069        return new RandomAccessFile(file, mode);
070    }
071
072    /**
073     * Constructs a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
074     * argument.
075     *
076     * @param file the file object
077     * @return a random access file stream
078     * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
079     */
080    public RandomAccessFile create(final Path file) throws FileNotFoundException {
081        return create(Objects.requireNonNull(file.toFile(), "file"));
082    }
083
084    /**
085     * Constructs a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
086     * argument.
087     *
088     * @param file the file object
089     * @return a random access file stream
090     * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
091     */
092    public RandomAccessFile create(final String file) throws FileNotFoundException {
093        return new RandomAccessFile(file, mode);
094    }
095
096    @Override
097    public String toString() {
098        return mode;
099    }
100
101}