Skip to content

Commit

Permalink
#103 Avoid array when ECD
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Nov 23, 2024
1 parent 90b82ef commit 3d47b88
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,28 @@ public class Bzip2InputStream extends InputStream {

private Bzip2InputStream.Data data;

public Bzip2InputStream(DataInput in) {
try {
String magic = in.readString(2, Charsets.UTF_8);
String version = in.readString(1, Charsets.UTF_8);
int blockSize = in.readByte();

if (!MAGIC.equals(magic))
throw new Zip4jvmException(String.format(
"BZIP2 magic number is not correct: actual is '%s' (expected is '%s')",
magic,
MAGIC));
if (!VERSION.equals(version))
throw new Zip4jvmException(String.format("BZIP2 version '%s' is not supported: only '%s' is supported",
version,
VERSION));
if (blockSize < '1' || blockSize > '9')
throw new Zip4jvmException(String.format(
"BZIP2 block size is invalid: actual is '%c' (expected between '1' and '9')",
blockSize));

this.in = new BitInputStream(in, ByteOrder.BIG_ENDIAN);
this.blockSize = blockSize * Constants.BASE_BLOCK_SIZE;
initBlock();
} catch (IOException e) {
throw new Zip4jvmException(e);
}
public Bzip2InputStream(DataInput in) throws IOException {
String magic = in.readString(2, Charsets.UTF_8);
String version = in.readString(1, Charsets.UTF_8);
int blockSize = in.readByte();

if (!MAGIC.equals(magic))
throw new Zip4jvmException(String.format(
"BZIP2 magic number is not correct: actual is '%s' (expected is '%s')",
magic,
MAGIC));
if (!VERSION.equals(version))
throw new Zip4jvmException(String.format("BZIP2 version '%s' is not supported: only '%s' is supported",
version,
VERSION));
if (blockSize < '1' || blockSize > '9')
throw new Zip4jvmException(String.format(
"BZIP2 block size is invalid: actual is '%c' (expected between '1' and '9')",
blockSize));

this.in = new BitInputStream(in, ByteOrder.BIG_ENDIAN);
this.blockSize = blockSize * Constants.BASE_BLOCK_SIZE;
initBlock();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public final class Bzip2DataInput extends CompressedDataInput {

private final Bzip2InputStream bzip;

public Bzip2DataInput(DataInput in) {
public static Bzip2DataInput create(DataInput in) throws IOException {
Bzip2InputStream bzip = new Bzip2InputStream(in);
return new Bzip2DataInput(bzip, in);
}

private Bzip2DataInput(Bzip2InputStream bzip, DataInput in) {
super(in);
bzip = new Bzip2InputStream(in);
this.bzip = bzip;
}

// ---------- ReadBuffer ----------
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public final class EnhancedDeflateDataInput extends CompressedDataInput {

private final EnhancedDeflateInputStream ed;

public EnhancedDeflateDataInput(DataInput in) {
public static EnhancedDeflateDataInput create(DataInput in) {
EnhancedDeflateInputStream ed = new EnhancedDeflateInputStream(in);
return new EnhancedDeflateDataInput(ed, in);
}

private EnhancedDeflateDataInput(EnhancedDeflateInputStream ed, DataInput in) {
super(in);
ed = new EnhancedDeflateInputStream(in);
this.ed = ed;
}

// ---------- ReadBuffer ----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public final class InflateDataInput extends CompressedDataInput {
private final byte[] buf1 = new byte[1024 * 4];
private final Inflater inflater = new Inflater(true);

public InflateDataInput(DataInput in) {
public static InflateDataInput create(DataInput in) {
return new InflateDataInput(in);
}

private InflateDataInput(DataInput in) {
super(in);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.io.lzma.LzmaInputStream;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;

import org.apache.commons.io.IOUtils;

Expand All @@ -39,25 +38,28 @@ public final class LzmaDataInput extends CompressedDataInput {

private final LzmaInputStream lzma;

public LzmaDataInput(ZipEntry zipEntry, DataInput in) {
public static LzmaDataInput create(ZipEntry zipEntry, DataInput in) throws IOException {
LzmaInputStream lzma = createInputStream(zipEntry, in);
return new LzmaDataInput(lzma, in);
}

private LzmaDataInput(LzmaInputStream lzma, DataInput in) {
super(in);
lzma = createInputStream(zipEntry, in);
this.lzma = lzma;
}

private static LzmaInputStream createInputStream(ZipEntry zipEntry, DataInput in) {
return Quietly.doQuietly(() -> {
in.mark(HEADER);
in.skip(1); // major version
in.skip(1); // minor version
int headerSize = in.readWord();
private static LzmaInputStream createInputStream(ZipEntry zipEntry, DataInput in) throws IOException {
in.mark(HEADER);
in.skip(1); // major version
in.skip(1); // minor version
int headerSize = in.readWord();

if (headerSize != HEADER_SIZE)
throw new Zip4jvmException(String.format("LZMA header size expected %d bytes: actual is %d bytes",
HEADER_SIZE, headerSize));
if (headerSize != HEADER_SIZE)
throw new Zip4jvmException(String.format("LZMA header size expected %d bytes: actual is %d bytes",
HEADER_SIZE, headerSize));

long uncompressedSize = zipEntry.isLzmaEosMarker() ? -1 : zipEntry.getUncompressedSize();
return new LzmaInputStream(in, uncompressedSize);
});
long uncompressedSize = zipEntry.isLzmaEosMarker() ? -1 : zipEntry.getUncompressedSize();
return new LzmaInputStream(in, uncompressedSize);
}

// ---------- ReadBuffer ----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
*/
public final class StoreDataInput extends CompressedDataInput {

public StoreDataInput(DataInput in) {
public static StoreDataInput create(DataInput in) {
return new StoreDataInput(in);
}

private StoreDataInput(DataInput in) {
super(in);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public final class ZstdDataInput extends CompressedDataInput {

private final ZstdInputStream zstd;

public ZstdDataInput(DataInput in) {
public static ZstdDataInput create(DataInput in) throws IOException {
ZstdInputStream zstd = new ZstdInputStream(in);
return new ZstdDataInput(zstd, in);
}

private ZstdDataInput(ZstdInputStream zstd, DataInput in) {
super(in);
zstd = new ZstdInputStream(in);
this.zstd = zstd;
}

// ---------- ReadBuffer ----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
*/
package ru.olegcherednik.zip4jvm.io.zstd;

import ru.olegcherednik.zip4jvm.exception.Zip4jvmException;
import ru.olegcherednik.zip4jvm.io.in.data.DataInput;

import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -38,12 +36,8 @@ public class ZstdInputStream extends InputStream {
private final com.github.luben.zstd.ZstdInputStream zstd;
private final byte[] buf = new byte[1];

public ZstdInputStream(DataInput in) {
try {
zstd = new com.github.luben.zstd.ZstdInputStream(new Decorator(in));
} catch (IOException e) {
throw new Zip4jvmException(e);
}
public ZstdInputStream(DataInput in) throws IOException {
zstd = new com.github.luben.zstd.ZstdInputStream(new Decorator(in));
}

@Override
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/ru/olegcherednik/zip4jvm/model/Compression.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.io.IOException;
import java.util.Optional;
import java.util.function.BiFunction;

/**
* This matches with {@link CompressionMethod}, but here we have only supported methods.
Expand All @@ -43,29 +43,29 @@
@RequiredArgsConstructor
public enum Compression {

STORE(CompressionMethod.STORE, (zipEntry, in) -> new StoreDataInput(in), "store"),
DEFLATE(CompressionMethod.DEFLATE, (zipEntry, in) -> new InflateDataInput(in), "deflate"),
STORE(CompressionMethod.STORE, (zipEntry, in) -> StoreDataInput.create(in), "store"),
DEFLATE(CompressionMethod.DEFLATE, (zipEntry, in) -> InflateDataInput.create(in), "deflate"),
ENHANCED_DEFLATE(CompressionMethod.ENHANCED_DEFLATE,
(zipEntry, in) -> new EnhancedDeflateDataInput(in),
(zipEntry, in) -> EnhancedDeflateDataInput.create(in),
"enhanced-deflate"),
BZIP2(CompressionMethod.BZIP2, (zipEntry, in) -> new Bzip2DataInput(in), "bzip2"),
LZMA(CompressionMethod.LZMA, LzmaDataInput::new, "lzma"),
ZSTD(CompressionMethod.ZSTD, (zipEntry, in) -> new ZstdDataInput(in), "zstd");
BZIP2(CompressionMethod.BZIP2, (zipEntry, in) -> Bzip2DataInput.create(in), "bzip2"),
LZMA(CompressionMethod.LZMA, LzmaDataInput::create, "lzma"),
ZSTD(CompressionMethod.ZSTD, (zipEntry, in) -> ZstdDataInput.create(in), "zstd");

@Getter
private final CompressionMethod method;
private final BiFunction<ZipEntry, DataInput, DataInput> decoratorDataInput;
private final DataInputFactory dataInputFactory;
@Getter
private final String title;

public DataInput addCompressionDecorator(DataInput in) {
public DataInput addCompressionDecorator(DataInput in) throws IOException {
return addCompressionDecorator(null, in);
}

public DataInput addCompressionDecorator(ZipEntry zipEntry, DataInput in) {
return Optional.ofNullable(decoratorDataInput)
public DataInput addCompressionDecorator(ZipEntry zipEntry, DataInput in) throws IOException {
return Optional.ofNullable(dataInputFactory)
.orElseThrow(() -> new CompressionNotSupportedException(this))
.apply(zipEntry, in);
.create(zipEntry, in);
}

public static Compression parseCompressionMethod(CompressionMethod compressionMethod) {
Expand All @@ -76,4 +76,10 @@ public static Compression parseCompressionMethod(CompressionMethod compressionMe
throw new CompressionNotSupportedException(compressionMethod);
}

private interface DataInputFactory {

DataInput create(ZipEntry zipEntry, DataInput in) throws IOException;

}

}

0 comments on commit 3d47b88

Please sign in to comment.