aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/io/RandomAccessFileMode.java
blob: f928e587236cb77da86fe25be13a09847d6c0eca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.file.Path;

/**
 * Access modes and factory methods for {@link RandomAccessFile}.
 *
 * @since 2.12.0
 */
public enum RandomAccessFileMode {

    /**
     * Mode "r" opens for reading only.
     */
    READ_ONLY("r"),

    /**
     * Mode "rw" opens for reading and writing.
     */
    READ_WRITE("rw"),

    /**
     * Mode "rws" opens for reading and writing, as with "rw", and also require that every update to the file's content or
     * metadata be written synchronously to the underlying storage device.
     */
    READ_WRITE_SYNC_ALL("rws"),

    /**
     * Mode "rwd" open for reading and writing, as with "rw", and also require that every update to the file's content be
     * written synchronously to the underlying storage device.
     */
    READ_WRITE_SYNC_CONTENT("rwd");

    private final String mode;

    RandomAccessFileMode(final String mode) {
        this.mode = mode;
    }

    /**
     * Creates a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
     * argument.
     *
     * @param file the file object
     * @return a random access file stream
     * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
     */
    public RandomAccessFile create(final File file) throws FileNotFoundException {
        return new RandomAccessFile(file, mode);
    }

    /**
     * Creates a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
     * argument.
     *
     * @param file the file object
     * @return a random access file stream
     * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
     */
    public RandomAccessFile create(final Path file) throws FileNotFoundException {
        return create(file.toFile());
    }

    /**
     * Creates a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
     * argument.
     *
     * @param file the file object
     * @return a random access file stream
     * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
     */
    public RandomAccessFile create(final String file) throws FileNotFoundException {
        return new RandomAccessFile(file, mode);
    }

    @Override
    public String toString() {
        return mode;
    }

}