Skip to content

Commit

Permalink
Compress refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Nov 5, 2024
1 parent 09907b5 commit f29d221
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ru.olegcherednik.zip4jvm.io.bzip2.Bzip2OutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.decorators.BaseDataOutput;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;

Expand All @@ -29,11 +30,12 @@
* @author Oleg Cherednik
* @since 12.04.2020
*/
final class Bzip2EntryOutputStream extends CompressedEntryOutputStream {
final class Bzip2EntryOutputStream extends BaseDataOutput {

private final Bzip2OutputStream bzip2;

Bzip2EntryOutputStream(DataOutput out, CompressionLevel compressionLevel) {
super(out);
bzip2 = Quietly.doQuietly(() -> new Bzip2OutputStream(out, compressionLevel));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

import ru.olegcherednik.zip4jvm.exception.CompressionNotSupportedException;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.decorators.BaseDataOutput;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;
import ru.olegcherednik.zip4jvm.model.CompressionMethod;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

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

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

/**
Expand All @@ -41,8 +41,7 @@
* @author Oleg Cherednik
* @since 12.02.2020
*/
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public abstract class CompressedEntryOutputStream extends OutputStream {
public abstract class CompressedEntryOutputStream extends BaseDataOutput {

public static OutputStream create(ZipEntry entry, DataOutput out) {
CompressionMethod compressionMethod = entry.getCompressionMethod();
Expand All @@ -65,4 +64,15 @@ public static OutputStream create(ZipEntry entry, DataOutput out) {
throw new CompressionNotSupportedException(compressionMethod);
}

protected CompressedEntryOutputStream(DataOutput out) {
super(out);
}

// ---------- Closeable ----------

@Override
public void close() throws IOException {
/* nothing to close */
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package ru.olegcherednik.zip4jvm.io.out.entry.compressed;

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.decorators.BaseDataOutput;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;

import java.io.IOException;
Expand All @@ -28,18 +29,17 @@
* @author Oleg Cherednik
* @since 26.07.2019
*/
final class DeflateEntryOutputStream extends CompressedEntryOutputStream {
final class DeflateEntryOutputStream extends BaseDataOutput {

private static final int FOUR = 4;

private final DataOutput out;
private final byte[] buf = new byte[1024 * 4];
private final Deflater deflater = new Deflater();

public boolean firstBytesRead;

DeflateEntryOutputStream(DataOutput out, CompressionLevel compressionLevel) {
this.out = out;
super(out);
deflater.setLevel(compressionLevel.getCode());
}

Expand Down Expand Up @@ -67,9 +67,9 @@ private void deflate() throws IOException {
}

if (firstBytesRead)
out.write(buf, 0, len);
delegate.write(buf, 0, len);
else {
out.write(buf, 2, len - 2);
delegate.write(buf, 2, len - 2);
firstBytesRead = true;
}
}
Expand All @@ -90,9 +90,4 @@ public void close() throws IOException {
finish();
}

@Override
public String toString() {
return out.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ru.olegcherednik.zip4jvm.io.lzma.LzmaInputStream;
import ru.olegcherednik.zip4jvm.io.lzma.LzmaOutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.decorators.BaseDataOutput;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;

Expand All @@ -30,14 +31,13 @@
* @author Oleg Cherednik
* @since 09.02.2020
*/
final class LzmaEntryOutputStream extends CompressedEntryOutputStream {
final class LzmaEntryOutputStream extends BaseDataOutput {

private final DataOutput out;
private final LzmaOutputStream lzma;
private boolean writeHeader = true;

LzmaEntryOutputStream(DataOutput out, CompressionLevel compressionLevel, boolean eosMarker, long uncompressedSize) {
this.out = out;
super(out);
lzma = createOutputStream(out, compressionLevel, eosMarker, uncompressedSize);
}

Expand All @@ -54,9 +54,9 @@ private static LzmaOutputStream createOutputStream(DataOutput out,
@Override
public void write(int b) throws IOException {
if (writeHeader) {
out.writeByte((byte) 19); // major version
out.writeByte((byte) 0); // minor version
out.writeWord(5); // header size
delegate.writeByte((byte) 19); // major version
delegate.writeByte((byte) 0); // minor version
delegate.writeWord(5); // header size
lzma.writeHeader();
writeHeader = false;
}
Expand All @@ -69,9 +69,4 @@ public void close() throws IOException {
lzma.close();
}

@Override
public String toString() {
return out.toString();
}

}

0 comments on commit f29d221

Please sign in to comment.