Skip to content

Commit

Permalink
refactor: use UncheckedIOException
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Nov 19, 2024
1 parent beccc53 commit df38182
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
36 changes: 24 additions & 12 deletions src/main/java/space/iseki/pefile/PEFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ public final class PEFile implements AutoCloseable {
*
* @param file the file
* @return the PEFile
* @throws IOException if an I/O error occurs
* @throws PEFileException if the file is not a valid PE file
* @throws UncheckedIOException if an I/O error occurs
* @throws PEFileException if the file is not a valid PE file
*/
public static @NotNull PEFile open(@NotNull File file) throws IOException {
return open(file.toPath());
public static @NotNull PEFile open(@NotNull File file) {
return wrapUncheckIOException(() -> open(file.toPath()));
}

/**
* Open a PE file from a {@link Path}.
*
* @param path the path of the PE file
* @return the PEFile
* @throws IOException if an I/O error occurs
* @throws PEFileException if the file is not a valid PE file
* @throws UncheckedIOException if an I/O error occurs
* @throws PEFileException if the file is not a valid PE file
*/
public static @NotNull PEFile open(@NotNull Path path) throws IOException {
return safeOpen(DataAccessors.of(path));
public static @NotNull PEFile open(@NotNull Path path) {
return wrapUncheckIOException(() -> safeOpen(DataAccessors.of(path)));
}

/**
Expand All @@ -94,11 +94,11 @@ public final class PEFile implements AutoCloseable {
*
* @param channel the channel will be closed when the PEFile is closed.
* @return the PEFile
* @throws IOException if an I/O error occurs
* @throws PEFileException if the file is not a valid PE file
* @throws UncheckedIOException if an I/O error occurs
* @throws PEFileException if the file is not a valid PE file
*/
public static @NotNull PEFile open(@NotNull SeekableByteChannel channel) throws IOException {
return open(new SeekableByteChannelDataAccessor(channel));
public static @NotNull PEFile open(@NotNull SeekableByteChannel channel) {
return wrapUncheckIOException(() -> open(new SeekableByteChannelDataAccessor(channel)));
}

private static long seekToCoffHeader(DataAccessor accessor) throws IOException {
Expand Down Expand Up @@ -185,6 +185,14 @@ static PEFile open(DataAccessor accessor) throws IOException {
return new PEFile(accessor, sections, coffHeader, standardHeader, optionalHeader, dataDirectory);
}

private static <R> R wrapUncheckIOException(Wrap<R> callable) {
try {
return callable.call();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
* Returns an iterator over the import table.
*
Expand Down Expand Up @@ -309,5 +317,9 @@ private void resourceNameTooLong(int len, int entryOffset) {
" at offset: " +
Integer.toUnsignedLong(entryOffset));
}

private interface Wrap<R> {
R call() throws IOException;
}
}

2 changes: 1 addition & 1 deletion src/main/java/space/iseki/pefile/ResourceWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected void computeNext() {
};
}

public class Entry {
public static class Entry {
final ResourceNode[] path;

Entry(ResourceNode[] path) {
Expand Down

0 comments on commit df38182

Please sign in to comment.