• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.RandomAccessFile;
22 import java.nio.file.Path;
23 import java.util.Objects;
24 
25 /**
26  * Access modes and factory methods for {@link RandomAccessFile}.
27  *
28  * @since 2.12.0
29  */
30 public enum RandomAccessFileMode {
31 
32     /**
33      * Mode {@code "r"} opens for reading only.
34      */
35     READ_ONLY("r"),
36 
37     /**
38      * Mode {@code "rw"} opens for reading and writing.
39      */
40     READ_WRITE("rw"),
41 
42     /**
43      * 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
44      * synchronously to the underlying storage device.
45      */
46     READ_WRITE_SYNC_ALL("rws"),
47 
48     /**
49      * 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
50      * to the underlying storage device.
51      */
52     READ_WRITE_SYNC_CONTENT("rwd");
53 
54     private final String mode;
55 
RandomAccessFileMode(final String mode)56     RandomAccessFileMode(final String mode) {
57         this.mode = mode;
58     }
59 
60     /**
61      * Constructs a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
62      * argument.
63      *
64      * @param file the file object
65      * @return a random access file stream
66      * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
67      */
create(final File file)68     public RandomAccessFile create(final File file) throws FileNotFoundException {
69         return new RandomAccessFile(file, mode);
70     }
71 
72     /**
73      * Constructs a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
74      * argument.
75      *
76      * @param file the file object
77      * @return a random access file stream
78      * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
79      */
create(final Path file)80     public RandomAccessFile create(final Path file) throws FileNotFoundException {
81         return create(Objects.requireNonNull(file.toFile(), "file"));
82     }
83 
84     /**
85      * Constructs a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
86      * argument.
87      *
88      * @param file the file object
89      * @return a random access file stream
90      * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
91      */
create(final String file)92     public RandomAccessFile create(final String file) throws FileNotFoundException {
93         return new RandomAccessFile(file, mode);
94     }
95 
96     @Override
toString()97     public String toString() {
98         return mode;
99     }
100 
101 }
102