Skip to content

Commit 09649d5

Browse files
author
Gary Gregory
committed
Add and use RandomAccessFileMode.
1 parent 7247663 commit 09649d5

6 files changed

Lines changed: 123 additions & 17 deletions

File tree

src/changes/changes.xml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,23 @@ The <action> type attribute can be add,update,fix,remove.
191191
Add and use FileUtils.isNewer(File, FileTime) for more precision.
192192
</action>
193193
<action dev="ggregory" type="add" due-to="Gary Gregory">
194-
Add and use PathUtils.isOlder(Path, FileTime, LinkOption...)
195-
Add and use PathUtils.isOlder(Path, Instant, LinkOption...)
196-
Add and use PathUtils.isOlder(Path, long, LinkOption...)
197-
Add and use PathUtils.isOlder(Path, Path)
194+
Add and use PathUtils.isOlder(Path, FileTime, LinkOption...).
195+
Add and use PathUtils.isOlder(Path, Instant, LinkOption...).
196+
Add and use PathUtils.isOlder(Path, long, LinkOption...).
197+
Add and use PathUtils.isOlder(Path, Path).
198198
</action>
199199
<action dev="ggregory" type="add" due-to="Gary Gregory">
200-
Add and use PathUtils.sizeOf(Path)
201-
Add and use PathUtils.sizeOfAsBigInteger(Path)
202-
Add and use PathUtils.sizeOfDirectory(Path)
203-
Add and use PathUtils.sizeOfDirectoryAsBigInteger(Path)
200+
Add and use PathUtils.sizeOf(Path).
201+
Add and use PathUtils.sizeOfAsBigInteger(Path).
202+
Add and use PathUtils.sizeOfDirectory(Path).
203+
Add and use PathUtils.sizeOfDirectoryAsBigInteger(Path).
204204
</action>
205205
<action dev="jonfreedman" type="add" due-to="Jon Freedman, Gary Gregory">
206206
Add Tailer.Tailable interface to allow tailing of remote files for example using jCIFS.
207207
</action>
208+
<action dev="ggregory" type="add" due-to="Gary Gregory">
209+
Add and use RandomAccessFileMode.
210+
</action>
208211
<!-- UPDATE -->
209212
<action dev="ggregory" type="add" due-to="Gary Gregory">
210213
Update FileEntry to use FileTime instead of long for file time stamps.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
24+
/**
25+
* Modes and factory for {@link RandomAccessFile}.
26+
*
27+
* @since 2.12.0
28+
*/
29+
public enum RandomAccessFileMode {
30+
31+
/**
32+
* Mode "r" opens for reading only.
33+
*/
34+
READ_ONLY("r"),
35+
36+
/**
37+
* Mode "rw" opens for reading and writing.
38+
*/
39+
READ_WRITE("rw"),
40+
41+
/**
42+
* Mode "rws" opens for reading and writing, as with "rw", and also require that every update to the file's content or
43+
* metadata be written synchronously to the underlying storage device.
44+
*/
45+
READ_WRITE_SYNC_ALL("rws"),
46+
47+
/**
48+
* Mode "rwd" open for reading and writing, as with "rw", and also require that every update to the file's content be
49+
* written synchronously to the underlying storage device.
50+
*/
51+
READ_WRITE_SYNC_CONTENT("rwd");
52+
53+
private final String mode;
54+
55+
RandomAccessFileMode(final String mode) {
56+
this.mode = mode;
57+
}
58+
59+
/**
60+
* Creates a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
61+
* argument.
62+
*
63+
* @param file the file object
64+
* @return a random access file stream
65+
* @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
66+
*/
67+
public RandomAccessFile create(final File file) throws FileNotFoundException {
68+
return new RandomAccessFile(file, mode);
69+
}
70+
71+
/**
72+
* Creates a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
73+
* argument.
74+
*
75+
* @param file the file object
76+
* @return a random access file stream
77+
* @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
78+
*/
79+
public RandomAccessFile create(final Path file) throws FileNotFoundException {
80+
return create(file.toFile());
81+
}
82+
83+
/**
84+
* Creates a random access file stream to read from, and optionally to write to, the file specified by the {@link File}
85+
* argument.
86+
*
87+
* @param file the file object
88+
* @return a random access file stream
89+
* @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
90+
*/
91+
public RandomAccessFile create(final String file) throws FileNotFoundException {
92+
return new RandomAccessFile(file, mode);
93+
}
94+
95+
@Override
96+
public String toString() {
97+
return mode;
98+
}
99+
100+
}

src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Arrays;
3131

3232
import org.apache.commons.io.IOUtils;
33+
import org.apache.commons.io.RandomAccessFileMode;
3334

3435
/**
3536
* <p>
@@ -259,7 +260,7 @@ public MagicNumberFileFilter(final String magicNumber, final long offset) {
259260
public boolean accept(final File file) {
260261
if (file != null && file.isFile() && file.canRead()) {
261262
try {
262-
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
263+
try (RandomAccessFile randomAccessFile = RandomAccessFileMode.READ_ONLY.create(file)) {
263264
final byte[] fileBytes = IOUtils.byteArray(this.magicNumbers.length);
264265
randomAccessFile.seek(byteOffset);
265266
final int read = randomAccessFile.read(fileBytes);

src/test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class FileCleaningTrackerTestCase {
5353
private FileCleaningTracker theInstance;
5454

5555
RandomAccessFile createRandomAccessFile() throws FileNotFoundException {
56-
return new RandomAccessFile(testFile, "rw");
56+
return RandomAccessFileMode.READ_WRITE.create(testFile);
5757
}
5858

5959
protected FileCleaningTracker newInstance() {

src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.RandomAccessFile;
2929
import java.nio.charset.StandardCharsets;
3030

31+
import org.apache.commons.io.RandomAccessFileMode;
3132
import org.junit.jupiter.api.Test;
3233

3334
public class RandomAccessFileInputStreamTest {
@@ -36,7 +37,7 @@ public class RandomAccessFileInputStreamTest {
3637
private static final int DATA_FILE_LEN = 1430;
3738

3839
private RandomAccessFile createRandomAccessFile() throws FileNotFoundException {
39-
return new RandomAccessFile(DATA_FILE, "r");
40+
return RandomAccessFileMode.READ_ONLY.create(DATA_FILE);
4041
}
4142

4243
@Test

src/test/java/org/apache/commons/io/input/TailerTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
import org.apache.commons.io.FileUtils;
4949
import org.apache.commons.io.IOUtils;
50+
import org.apache.commons.io.RandomAccessFileMode;
5051
import org.apache.commons.io.TestResources;
5152
import org.apache.commons.io.test.TestUtils;
5253
import org.junit.jupiter.api.AfterEach;
@@ -72,26 +73,26 @@ public NonStandardTailable(final File file) {
7273
public Tailer.RandomAccessResourceBridge getRandomAccess(final String mode) throws FileNotFoundException {
7374
return new Tailer.RandomAccessResourceBridge() {
7475

75-
private final RandomAccessFile reader = new RandomAccessFile(file, mode);
76+
private final RandomAccessFile randomAccessFile = new RandomAccessFile(file, mode);
7677

7778
@Override
7879
public void close() throws IOException {
79-
reader.close();
80+
randomAccessFile.close();
8081
}
8182

8283
@Override
8384
public long getPointer() throws IOException {
84-
return reader.getFilePointer();
85+
return randomAccessFile.getFilePointer();
8586
}
8687

8788
@Override
8889
public int read(final byte[] b) throws IOException {
89-
return reader.read(b);
90+
return randomAccessFile.read(b);
9091
}
9192

9293
@Override
9394
public void seek(final long position) throws IOException {
94-
reader.seek(position);
95+
randomAccessFile.seek(position);
9596
}
9697
};
9798
}
@@ -203,7 +204,7 @@ protected void createFile(final File file, final long size) throws IOException {
203204
try {
204205
while (reader == null) {
205206
try {
206-
reader = new RandomAccessFile(file.getPath(), "r");
207+
reader = RandomAccessFileMode.READ_ONLY.create(file);
207208
} catch (final FileNotFoundException ignore) {
208209
// ignore
209210
}

0 commit comments

Comments
 (0)