Skip to content

Commit

Permalink
Small improvements, QIP I/O abstraction layer
Browse files Browse the repository at this point in the history
  • Loading branch information
andrebrait committed Feb 23, 2022
1 parent 930ae15 commit 67c3c9d
Show file tree
Hide file tree
Showing 357 changed files with 1,449 additions and 39 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/io/github/datromtool/data/CrcKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;

@Value
@AllArgsConstructor(staticName = "from")
@AllArgsConstructor(staticName = "of")
@JsonInclude(NON_NULL)
public class CrcKey {

Expand All @@ -23,11 +23,11 @@ public class CrcKey {

@Nonnull
public static CrcKey from(@Nonnull Rom rom) {
return from(rom.getSize(), rom.getCrc());
return of(rom.getSize(), rom.getCrc());
}

@Nonnull
public static CrcKey from(@Nonnull FileScanner.Result result) {
return from(result.getUnheaderedSize(), result.getDigest().getCrc());
return of(result.getUnheaderedSize(), result.getDigest().getCrc());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.datromtool.display;

public abstract class CachingDisplayable implements Displayable {

private transient String $displayNameCache;

@Override
public final String getDisplayName() {
if ($displayNameCache != null) {
return $displayNameCache;
}
String displayName = getDisplayNameForCache();
$displayNameCache = displayName;
return displayName;
}

protected abstract String getDisplayNameForCache();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.datromtool.display;

import io.github.datromtool.io.Addressable;

public abstract class CachingDisplayableAddressable extends CachingDisplayable implements Addressable {

@Override
protected final String getDisplayNameForCache() {
return getPath().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.datromtool.display;

import io.github.datromtool.io.AddressableChild;

public abstract class CachingDisplayableAddressableChild extends CachingDisplayable implements AddressableChild {

@Override
protected final String getDisplayNameForCache() {
return getParent().getPath().resolve(getRelativeName()).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.datromtool.display;

public interface Displayable {

String getDisplayName();
}
8 changes: 8 additions & 0 deletions core/src/main/java/io/github/datromtool/io/Addressable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.datromtool.io;

import java.nio.file.Path;

public interface Addressable {

Path getPath();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.datromtool.io;

public interface AddressableChild {

Addressable getParent();

String getRelativeName();
}
24 changes: 8 additions & 16 deletions core/src/main/java/io/github/datromtool/io/FileCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,7 @@ private void extractTarEntries(ExtractionSpec spec, int index) throws IOExceptio
}

private void compressZipEntries(CompressionSpec spec, int index) throws IOException {
try (ZipArchiveOutputStream zipArchiveOutputStream =
new ZipArchiveOutputStream(spec.getTo().toFile())) {
try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(spec.getTo())) {
for (CompressionSpec.InternalSpec internal : spec.getInternalSpecs()) {
Path source = internal.getFrom();
try (InputStream inputStream = Files.newInputStream(source)) {
Expand Down Expand Up @@ -775,8 +774,7 @@ private void fromTarToArchive(ArchiveCopySpec spec, int index) throws IOExceptio
}

private void fromZipToZipRaw(ArchiveCopySpec spec, int index) throws IOException {
try (ZipArchiveOutputStream zipArchiveOutputStream =
new ZipArchiveOutputStream(spec.getTo().toFile())) {
try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(spec.getTo())) {
ArchiveUtils.readZip(spec.getFrom(), (zipFile, zipArchiveEntry) -> {
String name = zipArchiveEntry.getName();
ArchiveCopySpec.InternalSpec internal = findInternalSpec(spec, name);
Expand Down Expand Up @@ -811,8 +809,7 @@ private void fromZipToZipRaw(ArchiveCopySpec spec, int index) throws IOException
}

private void fromZipToZip(ArchiveCopySpec spec, int index) throws IOException {
try (ZipArchiveOutputStream zipArchiveOutputStream =
new ZipArchiveOutputStream(spec.getTo().toFile())) {
try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(spec.getTo())) {
ArchiveUtils.readZip(
spec.getFrom(),
(zipFile, zipArchiveEntry) -> {
Expand Down Expand Up @@ -910,8 +907,7 @@ private void fromZipToTar(ArchiveCopySpec spec, int index) throws IOException {
}

private void fromRarToZip(ArchiveCopySpec spec, int index) throws Exception {
try (ZipArchiveOutputStream zipArchiveOutputStream =
new ZipArchiveOutputStream(spec.getTo().toFile())) {
try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(spec.getTo())) {
ArchiveUtils.readRar(
spec.getFrom(),
(archive, fileHeader) -> {
Expand Down Expand Up @@ -952,8 +948,7 @@ private void fromRarToZip(ArchiveCopySpec spec, int index) throws Exception {
}

private void fromRarWithUnrarToZip(ArchiveCopySpec spec, int index) throws Exception {
try (ZipArchiveOutputStream zipArchiveOutputStream =
new ZipArchiveOutputStream(spec.getTo().toFile())) {
try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(spec.getTo())) {
ImmutableSet<String> desiredEntryNames = getInternalSources(spec);
ArchiveUtils.readRarWithUnrar(
spec.getFrom(),
Expand All @@ -968,8 +963,7 @@ private void fromRarWithUnrarToZip(ArchiveCopySpec spec, int index) throws Excep
}

private void fromRarWithSevenZipToZip(ArchiveCopySpec spec, int index) throws Exception {
try (ZipArchiveOutputStream zipArchiveOutputStream =
new ZipArchiveOutputStream(spec.getTo().toFile())) {
try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(spec.getTo())) {
ImmutableSet<String> desiredEntryNames = getInternalSources(spec);
ArchiveUtils.readRarWithSevenZip(
spec.getFrom(),
Expand Down Expand Up @@ -1218,8 +1212,7 @@ private void fromRarEntryToTar(
}

private void fromSevenZipToZip(ArchiveCopySpec spec, int index) throws IOException {
try (ZipArchiveOutputStream zipArchiveOutputStream =
new ZipArchiveOutputStream(spec.getTo().toFile())) {
try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(spec.getTo())) {
ArchiveUtils.readSevenZip(
spec.getFrom(),
(sevenZFile, sevenZArchiveEntry) -> toZip(
Expand Down Expand Up @@ -1303,8 +1296,7 @@ private void fromSevenZipToTar(ArchiveCopySpec spec, int index) throws IOExcepti

private void fromTarToZip(ArchiveCopySpec spec, int index)
throws IOException {
try (ZipArchiveOutputStream zipArchiveOutputStream =
new ZipArchiveOutputStream(spec.getTo().toFile())) {
try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(spec.getTo())) {
ArchiveUtils.readTar(
spec.getFromType(),
spec.getFrom(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.datromtool.io.spec;

import io.github.datromtool.display.CachingDisplayableAddressableChild;

public abstract class AbstractArchiveDestinationInternalSpec extends CachingDisplayableAddressableChild implements ArchiveDestinationInternalSpec {

@Override
public final String getRelativeName() {
return getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.datromtool.io.spec;

import io.github.datromtool.display.CachingDisplayableAddressableChild;

public abstract class AbstractArchiveSourceInternalSpec extends CachingDisplayableAddressableChild implements ArchiveSourceInternalSpec {

@Override
public final String getRelativeName() {
return getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.datromtool.io.spec;

import io.github.datromtool.io.AddressableChild;

public interface ArchiveDestinationInternalSpec extends AddressableChild, DestinationSpec {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.datromtool.io.spec;

import io.github.datromtool.io.Addressable;
import io.github.datromtool.io.ArchiveType;

import java.io.Closeable;
import java.io.IOException;

public interface ArchiveDestinationSpec extends Addressable, Closeable {

ArchiveType getType();

ArchiveDestinationInternalSpec createInternalDestinationSpecFor(String name, SourceSpec sourceSpec) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.datromtool.io.spec;

import io.github.datromtool.io.AddressableChild;

public interface ArchiveSourceInternalSpec extends AddressableChild, SourceSpec {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.datromtool.io.spec;

import io.github.datromtool.io.Addressable;
import io.github.datromtool.io.ArchiveType;

import javax.annotation.Nullable;
import java.io.Closeable;
import java.io.IOException;

public interface ArchiveSourceSpec extends Addressable, Closeable {

ArchiveType getType();

@Nullable
ArchiveSourceInternalSpec getNextInternalSpec() throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.datromtool.io.spec;

import io.github.datromtool.display.Displayable;

import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;

public interface DestinationSpec extends Displayable, Closeable {

String getName();

OutputStream getOutputStream() throws IOException;
}
78 changes: 78 additions & 0 deletions core/src/main/java/io/github/datromtool/io/spec/FileTimes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.github.datromtool.io.spec;

import lombok.Builder;
import lombok.Value;

import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Value
@Builder(toBuilder = true)
public class FileTimes {

@Nullable
FileTime lastModifiedTime;
@Nullable
FileTime lastAccessTime;
@Nullable
FileTime creationTime;

public static FileTimes from(@Nullable FileTime lastModifiedTime, @Nullable FileTime lastAccessTime, @Nullable FileTime creationTime) {
return new FileTimes(truncate(lastModifiedTime), truncate(lastAccessTime), truncate(creationTime));
}

@Nullable
private static FileTime truncate(@Nullable FileTime lastModifiedTime) {
return lastModifiedTime != null
? FileTime.from(lastModifiedTime.to(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS)
: null;
}

private FileTimes(@Nullable FileTime lastModifiedTime, @Nullable FileTime lastAccessTime, @Nullable FileTime creationTime) {
this.lastModifiedTime = lastModifiedTime;
this.lastAccessTime = lastAccessTime;
this.creationTime = creationTime;
}

@Nullable
public Date getLastModifiedTimeAsDate() {
return asDate(lastModifiedTime);
}

@Nullable
public Date getLastAccessTimeAsDate() {
return asDate(lastAccessTime);
}

@Nullable
public Date getCreationTimeAsDate() {
return asDate(creationTime);
}

private static Date asDate(@Nullable FileTime fileTime) {
return fileTime != null ? new Date(fileTime.toMillis()) : null;
}

public static FileTimes from(Path file) throws IOException {
return from(Files.readAttributes(file, BasicFileAttributes.class));
}

public static FileTimes from(BasicFileAttributes attributes) {
return from(attributes.lastModifiedTime(), attributes.lastAccessTime(), attributes.creationTime());
}

public void applyTo(Path file) throws IOException {
applyTo(Files.getFileAttributeView(file, BasicFileAttributeView.class));
}

public void applyTo(BasicFileAttributeView attributes) throws IOException {
attributes.setTimes(lastModifiedTime, lastAccessTime, creationTime);
}
}
18 changes: 18 additions & 0 deletions core/src/main/java/io/github/datromtool/io/spec/SourceSpec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.datromtool.io.spec;

import io.github.datromtool.display.Displayable;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

public interface SourceSpec extends Displayable, Closeable {

String getName();

long getSize();

FileTimes getFileTimes();

InputStream getInputStream() throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.datromtool.io.spec.compression;

import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;

import java.io.IOException;
import java.io.OutputStream;

final class BZip2Compressor implements Compressor {
@Override
public OutputStream compress(OutputStream backingOutputStream) throws IOException {
return new BZip2CompressorOutputStream(backingOutputStream);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.datromtool.io.spec.compression;

import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;

import java.io.IOException;
import java.io.InputStream;

final class BZip2Decompressor implements Decompressor {
@Override
public InputStream decompress(InputStream compressedInputStream) throws IOException {
return new BZip2CompressorInputStream(compressedInputStream);
}
}
Loading

0 comments on commit 67c3c9d

Please sign in to comment.