Skip to content

Commit

Permalink
Packfile: Slightly improve error messages by including archive name
Browse files Browse the repository at this point in the history
Includes small API cleanup
  • Loading branch information
ShadelessFox committed Jun 20, 2024
1 parent 8334324 commit c4a2932
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,6 @@ public synchronized void reload(boolean purgeChanges) throws IOException {
validate();
}

@NotNull
public byte[] extract(@NotNull String path) throws IOException {
return newInputStream(path).readAllBytes();
}

@NotNull
public byte[] extract(long hash) throws IOException {
return newInputStream(hash).readAllBytes();
}

@NotNull
public InputStream newInputStream(@NotNull String path) throws IOException {
return newInputStream(getPathHash(getNormalizedPath(path)));
Expand All @@ -195,7 +185,7 @@ public InputStream newInputStream(long hash) throws IOException {

final FileEntry entry = getFileEntry(hash);
if (entry == null) {
throw new IllegalArgumentException("Can't find path 0x" + Long.toHexString(hash) + " in this archive");
throw new IOException("Can't find file %#018x in %s".formatted(hash, getPath()));
}

return new PackfileInputStream(entry);
Expand Down Expand Up @@ -738,16 +728,20 @@ private void fill() throws IOException {
final ByteBuffer src = ByteBuffer.allocate(chunk.compressed().size());
final ByteBuffer dst = ByteBuffer.wrap(decompressed, 0, chunk.decompressed().size());

synchronized (Packfile.this) {
channel.position(chunk.compressed().offset());
channel.read(src.slice());
}
try {
synchronized (Packfile.this) {
channel.position(chunk.compressed().offset());
channel.read(src.slice());
}

if (header.isEncrypted()) {
chunk.swizzle(src.slice());
}
if (header.isEncrypted()) {
chunk.swizzle(src.slice());
}

compressor.decompress(src, dst);
compressor.decompress(src, dst);
} catch (Exception e) {
throw new IOException("Error reading chunk %d of file %#18x in %s".formatted(index, file.hash(), getPath()), e);
}

if (index == 0) {
pos = (int) (file.span().offset() - chunk.decompressed().offset());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@
import com.shade.platform.model.runtime.VoidProgressMonitor;
import com.shade.util.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.SecureRandom;

import static java.nio.file.StandardOpenOption.*;

public class PackfileWriterTest {
private static final int FILES_COUNT = 3;

@TempDir
private Path directory;

@ParameterizedTest
@ValueSource(ints = {0x0, 0x1, 0x10, 0x100, 0x1000, 0x10000, 0x3ffff, 0x40000, 0x40001, 0x7ffff, 0x80000, 0x80001, 0x7fffff})
public void writePackfileTest(int length) throws IOException {
final var file = Files.createTempFile("decima", ".bin");
public void writeAndReadPackfileTest(int length) throws IOException {
final var file = directory.resolve("test.bin");
final var monitor = new VoidProgressMonitor();
final var files = new byte[FILES_COUNT][length];
final var random = new SecureRandom();
Expand All @@ -41,13 +47,14 @@ public void writePackfileTest(int length) throws IOException {
}
}

final var manager = new PackfileManager(NoOpCompressor.INSTANCE);

try (Packfile packfile = manager.openPackfile(file)) {
try (PackfileManager manager = new PackfileManager(NoOpCompressor.INSTANCE)) {
Packfile packfile = manager.openPackfile(file);
Assertions.assertEquals(FILES_COUNT, packfile.getFileEntries().size());

for (int i = 0; i < FILES_COUNT; i++) {
Assertions.assertArrayEquals(files[i], packfile.extract(i));
try (InputStream is = packfile.newInputStream(i)) {
Assertions.assertArrayEquals(files[i], is.readAllBytes());
}
}
}
}
Expand Down

0 comments on commit c4a2932

Please sign in to comment.