Skip to content

Commit 152e4f1

Browse files
committed
Add Add RandomAccessFileMode.accept(Path, IOConsumer<RandomAccessFile>)
Add Add RandomAccessFileMode.apply(Path, IOFunction<RandomAccessFile, T>)
1 parent d8bdae7 commit 152e4f1

File tree

8 files changed

+184
-146
lines changed

8 files changed

+184
-146
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ The <action> type attribute can be add,update,fix,remove.
6262
<action dev="ggregory" type="add" due-to="Gary Gregory">Add FileAlterationObserver.Builder() and deprecate most constructors.</action>
6363
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOUtils.readLines(CharSequence).</action>
6464
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ValidatingObjectInputStream.ObjectStreamClassPredicate to allow configuration reuse.</action>
65+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileMode.accept(Path, IOConsumer&lt;RandomAccessFile&gt;).</action>
66+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileMode.apply(Path, IOFunction&lt;RandomAccessFile&gt;, T).</action>
6567
<!-- UPDATE -->
6668
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 74 to 78 #670, #676, #679, #688.</action>
6769
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump commons.bytebuddy.version from 1.15.1 to 1.15.8 #672, #673, #685, #686, #694, #696.</action>

src/main/java/org/apache/commons/io/RandomAccessFileMode.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919

2020
import java.io.File;
2121
import java.io.FileNotFoundException;
22+
import java.io.IOException;
2223
import java.io.RandomAccessFile;
2324
import java.nio.file.OpenOption;
2425
import java.nio.file.Path;
2526
import java.nio.file.StandardOpenOption;
2627
import java.util.Objects;
2728

29+
import org.apache.commons.io.function.IOConsumer;
30+
import org.apache.commons.io.function.IOFunction;
31+
2832
/**
2933
* Enumerates access modes for {@link RandomAccessFile} with factory methods.
3034
*
@@ -147,8 +151,49 @@ public static RandomAccessFileMode valueOfMode(final String mode) {
147151
this.level = level;
148152
}
149153

154+
/**
155+
* Performs an operation on the {@link RandomAccessFile} specified at the given {@link Path}.
156+
* <p>
157+
* This method allocates and releases the {@link RandomAccessFile} given to the consumer.
158+
* </p>
159+
*
160+
* @param file the file specifying the {@link RandomAccessFile} to open.
161+
* @param consumer the function to apply.
162+
* @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
163+
* @throws IOException Thrown by the given function.
164+
* @since 2.18.0
165+
*/
166+
public void accept(final Path file, final IOConsumer<RandomAccessFile> consumer) throws IOException {
167+
try (RandomAccessFile raf = create(file)) {
168+
consumer.accept(raf);
169+
}
170+
}
171+
172+
/**
173+
* Applies the given function for a {@link RandomAccessFile} specified at the given {@link Path}.
174+
* <p>
175+
* This method allocates and releases the {@link RandomAccessFile} given to the function.
176+
* </p>
177+
*
178+
* @param <T> the return type of the function.
179+
* @param file the file specifying the {@link RandomAccessFile} to open.
180+
* @param function the function to apply.
181+
* @return the function's result value.
182+
* @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
183+
* @throws IOException Thrown by the given function.
184+
* @since 2.18.0
185+
*/
186+
public <T> T apply(final Path file, final IOFunction<RandomAccessFile, T> function) throws IOException {
187+
try (RandomAccessFile raf = create(file)) {
188+
return function.apply(raf);
189+
}
190+
}
191+
150192
/**
151193
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
194+
* <p>
195+
* Prefer {@link #create(Path)} over this.
196+
* </p>
152197
*
153198
* @param file the file object
154199
* @return a random access file
@@ -171,6 +216,9 @@ public RandomAccessFile create(final Path file) throws FileNotFoundException {
171216

172217
/**
173218
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
219+
* <p>
220+
* Prefer {@link #create(Path)} over this.
221+
* </p>
174222
*
175223
* @param name the file object
176224
* @return a random access file

src/main/java/org/apache/commons/io/build/AbstractOrigin.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,7 @@ public PathOrigin(final Path origin) {
413413

414414
@Override
415415
public byte[] getByteArray(final long position, final int length) throws IOException {
416-
try (RandomAccessFile raf = RandomAccessFileMode.READ_ONLY.create(origin)) {
417-
return RandomAccessFiles.read(raf, position, length);
418-
}
416+
return RandomAccessFileMode.READ_ONLY.apply(origin, raf -> RandomAccessFiles.read(raf, position, length));
419417
}
420418

421419
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21-
import java.io.RandomAccessFile;
2221
import java.io.Serializable;
2322
import java.nio.ByteBuffer;
2423
import java.nio.channels.FileChannel;
@@ -262,8 +261,9 @@ public MagicNumberFileFilter(final String magicNumber, final long offset) {
262261
@Override
263262
public boolean accept(final File file) {
264263
if (file != null && file.isFile() && file.canRead()) {
265-
try (RandomAccessFile randomAccessFile = RandomAccessFileMode.READ_ONLY.create(file)) {
266-
return Arrays.equals(magicNumbers, RandomAccessFiles.read(randomAccessFile, byteOffset, magicNumbers.length));
264+
try {
265+
return RandomAccessFileMode.READ_ONLY.apply(file.toPath(),
266+
raf -> Arrays.equals(magicNumbers, RandomAccessFiles.read(raf, byteOffset, magicNumbers.length)));
267267
} catch (final IOException ignored) {
268268
// Do nothing, fall through and do not accept file
269269
}

0 commit comments

Comments
 (0)