Skip to content

Commit

Permalink
Add Add RandomAccessFileMode.accept(Path, IOConsumer<RandomAccessFile>)
Browse files Browse the repository at this point in the history
Add Add RandomAccessFileMode.apply(Path, IOFunction<RandomAccessFile,
T>)
  • Loading branch information
garydgregory committed Nov 6, 2024
1 parent d8bdae7 commit 152e4f1
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 146 deletions.
2 changes: 2 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">Add FileAlterationObserver.Builder() and deprecate most constructors.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOUtils.readLines(CharSequence).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ValidatingObjectInputStream.ObjectStreamClassPredicate to allow configuration reuse.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileMode.accept(Path, IOConsumer&lt;RandomAccessFile&gt;).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileMode.apply(Path, IOFunction&lt;RandomAccessFile&gt;, T).</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 74 to 78 #670, #676, #679, #688.</action>
<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>
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/apache/commons/io/RandomAccessFileMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Objects;

import org.apache.commons.io.function.IOConsumer;
import org.apache.commons.io.function.IOFunction;

/**
* Enumerates access modes for {@link RandomAccessFile} with factory methods.
*
Expand Down Expand Up @@ -147,8 +151,49 @@ public static RandomAccessFileMode valueOfMode(final String mode) {
this.level = level;
}

/**
* Performs an operation on the {@link RandomAccessFile} specified at the given {@link Path}.
* <p>
* This method allocates and releases the {@link RandomAccessFile} given to the consumer.
* </p>
*
* @param file the file specifying the {@link RandomAccessFile} to open.
* @param consumer the function to apply.
* @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
* @throws IOException Thrown by the given function.
* @since 2.18.0
*/
public void accept(final Path file, final IOConsumer<RandomAccessFile> consumer) throws IOException {
try (RandomAccessFile raf = create(file)) {
consumer.accept(raf);
}
}

/**
* Applies the given function for a {@link RandomAccessFile} specified at the given {@link Path}.
* <p>
* This method allocates and releases the {@link RandomAccessFile} given to the function.
* </p>
*
* @param <T> the return type of the function.
* @param file the file specifying the {@link RandomAccessFile} to open.
* @param function the function to apply.
* @return the function's result value.
* @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
* @throws IOException Thrown by the given function.
* @since 2.18.0
*/
public <T> T apply(final Path file, final IOFunction<RandomAccessFile, T> function) throws IOException {
try (RandomAccessFile raf = create(file)) {
return function.apply(raf);
}
}

/**
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
* <p>
* Prefer {@link #create(Path)} over this.
* </p>
*
* @param file the file object
* @return a random access file
Expand All @@ -171,6 +216,9 @@ public RandomAccessFile create(final Path file) throws FileNotFoundException {

/**
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
* <p>
* Prefer {@link #create(Path)} over this.
* </p>
*
* @param name the file object
* @return a random access file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,7 @@ public PathOrigin(final Path origin) {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -262,8 +261,9 @@ public MagicNumberFileFilter(final String magicNumber, final long offset) {
@Override
public boolean accept(final File file) {
if (file != null && file.isFile() && file.canRead()) {
try (RandomAccessFile randomAccessFile = RandomAccessFileMode.READ_ONLY.create(file)) {
return Arrays.equals(magicNumbers, RandomAccessFiles.read(randomAccessFile, byteOffset, magicNumbers.length));
try {
return RandomAccessFileMode.READ_ONLY.apply(file.toPath(),
raf -> Arrays.equals(magicNumbers, RandomAccessFiles.read(raf, byteOffset, magicNumbers.length)));
} catch (final IOException ignored) {
// Do nothing, fall through and do not accept file
}
Expand Down
Loading

0 comments on commit 152e4f1

Please sign in to comment.