Skip to content

Commit

Permalink
#3 refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Oct 29, 2024
1 parent 4970be6 commit 1b7b9e7
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public void write(final int b) throws IOException {
throw new IOException("Closed");
}

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

/**
* Writes the current byte to the buffer, run-length encoding it
* if it has been repeated at least four times (the first step
Expand Down Expand Up @@ -443,7 +448,8 @@ public void write(final byte[] buf, int offs, final int len) throws IOException
if (len < 0)
throw new IndexOutOfBoundsException("len(" + len + ") < 0.");
if (offs + len > buf.length)
throw new IndexOutOfBoundsException("offs(" + offs + ") + len(" + len + ") > buf.length(" + buf.length + ").");
throw new IndexOutOfBoundsException(
"offs(" + offs + ") + len(" + len + ") > buf.length(" + buf.length + ").");
if (closed)
throw new IOException("Stream closed");

Expand Down Expand Up @@ -541,7 +547,8 @@ private void sendMTFValues() throws IOException {
/* Decide how many coding tables to use */
// assert (this.nMTF > 0) : this.nMTF;
final int nGroups = (this.nMTF < 200) ? 2 : (this.nMTF < 600) ? 3
: (this.nMTF < 1200) ? 4 : (this.nMTF < 2400) ? 5 : 6;
:
(this.nMTF < 1200) ? 4 : (this.nMTF < 2400) ? 5 : 6;

/* Generate an initial set of coding tables */
sendMTFValues0(nGroups, alphaSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
*/
package ru.olegcherednik.zip4jvm.io.lzma;

import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -43,7 +44,8 @@ public class LzmaOutputStream extends OutputStream {

private long currentUncompressedSize;

public LzmaOutputStream(DataOutput out, LzmaInputStream.Properties properties, long uncompressedSize) throws IOException {
public LzmaOutputStream(DataOutput out, LzmaInputStream.Properties properties, long uncompressedSize)
throws IOException {
this.out = out;
this.properties = properties;
this.uncompressedSize = uncompressedSize;
Expand All @@ -56,14 +58,15 @@ public void writeHeader() throws IOException {

@Override
public void write(int val) throws IOException {
oneByteBuf[0] = (byte)val;
oneByteBuf[0] = (byte) val;
write(oneByteBuf, 0, 1);
}

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
if (uncompressedSize != -1 && uncompressedSize - currentUncompressedSize < len)
throw new IOException(String.format("Expected uncompressed input size (%s bytes) was exceeded", uncompressedSize));
throw new IOException(String.format("Expected uncompressed input size (%s bytes) was exceeded",
uncompressedSize));

currentUncompressedSize += len;

Expand All @@ -78,8 +81,10 @@ public void write(byte[] buf, int offs, int len) throws IOException {
@Override
public void close() throws IOException {
if (uncompressedSize != -1 && uncompressedSize != currentUncompressedSize)
throw new IOException(String.format("Expected uncompressed size (%s) doesn't equal the number of bytes written to the stream (%s)",
uncompressedSize, currentUncompressedSize));
throw new IOException(String.format(
"Expected uncompressed size (%s) doesn't equal the number of bytes written to the stream (%s)",
uncompressedSize,
currentUncompressedSize));

lzma.getLZEncoder().setFinishing();
lzma.encodeForLZMA1();
Expand All @@ -90,6 +95,11 @@ public void close() throws IOException {
lzma.close();
}

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

@Getter
@Builder
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
Expand All @@ -98,9 +108,11 @@ public static final class Properties {
/**
* The largest dictionary size supported by this implementation.
* <p>
* LZMA allows dictionaries up to one byte less than 4 GiB. This implementation supports only 16 bytes less than 2 GiB. This limitation is
* LZMA allows dictionaries up to one byte less than 4 GiB. This implementation supports only 16 bytes less than
* 2 GiB. This limitation is
* due
* to Java using signed 32-bit integers for array indexing. The limitation shouldn't matter much in practice since so huge dictionaries are
* to Java using signed 32-bit integers for array indexing. The limitation shouldn't matter much in practice
* since so huge dictionaries are
* not
* normally used.
*/
Expand All @@ -113,7 +125,7 @@ public static final class Properties {
private final int dictionarySize;

public int write(DataOutput out) throws IOException {
out.writeByte((byte)((pb * 5 + lp) * 9 + lc));
out.writeByte((byte) ((pb * 5 + lp) * 9 + lc));
out.writeDword(dictionarySize);
return 5;
}
Expand All @@ -123,7 +135,7 @@ public static Properties read(DataInput in) throws IOException {
int lc = v % 9;
int lp = (v / 9) % 5;
int pb = v / (9 * 5);
int dictionarySize = (int)in.readDword();
int dictionarySize = (int) in.readDword();

checkDictionarySize(dictionarySize);

Expand All @@ -134,7 +146,8 @@ private static void checkDictionarySize(int dictionarySize) {
if (dictionarySize < 0)
throw new IllegalArgumentException("Incorrect LZMA dictionary size: " + dictionarySize);
if (dictionarySize > DICTIONARY_SIZE_MAX)
throw new IllegalArgumentException("Incorrect LZMA dictionary size is too big for this implementation: " + dictionarySize);
throw new IllegalArgumentException(
"Incorrect LZMA dictionary size is too big for this implementation: " + dictionarySize);
}

private static int dictionarySizeInRange(int dictionarySize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package ru.olegcherednik.zip4jvm.io.out.entry.encrypted;

import ru.olegcherednik.zip4jvm.io.bzip2.Bzip2OutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.EncoderDataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;

import java.io.IOException;
Expand All @@ -30,12 +30,10 @@
*/
final class Bzip2EntryOutputStream extends EncryptedEntryOutputStream {

private final EncoderDataOutput encoderDataOutput;
private final Bzip2OutputStream bzip2;

Bzip2EntryOutputStream(CompressionLevel compressionLevel, EncoderDataOutput encoderDataOutput) throws IOException {
this.encoderDataOutput = encoderDataOutput;
bzip2 = new Bzip2OutputStream(encoderDataOutput, compressionLevel);
Bzip2EntryOutputStream(DataOutput out, CompressionLevel compressionLevel) throws IOException {
bzip2 = new Bzip2OutputStream(out, compressionLevel);
}

@Override
Expand All @@ -46,12 +44,11 @@ public void write(byte[] buf, int offs, int len) throws IOException {
@Override
public void close() throws IOException {
bzip2.close();
// encoderDataOutput.encodingAccomplished();
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package ru.olegcherednik.zip4jvm.io.out.entry.encrypted;

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

import java.io.IOException;
Expand All @@ -32,14 +32,14 @@ final class DeflateEntryOutputStream extends EncryptedEntryOutputStream {

private static final int FOUR = 4;

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

public boolean firstBytesRead;

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

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

if (firstBytesRead)
encoderDataOutput.write(buf, 0, len);
out.write(buf, 0, len);
else {
encoderDataOutput.write(buf, 2, len - 2);
out.write(buf, 2, len - 2);
firstBytesRead = true;
}
}
Expand All @@ -88,12 +88,11 @@ private void finish() throws IOException {
@Override
public void close() throws IOException {
finish();
// encoderDataOutput.encodingAccomplished();
}

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

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

import ru.olegcherednik.zip4jvm.exception.CompressionNotSupportedException;
import ru.olegcherednik.zip4jvm.io.out.data.EncoderDataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;
import ru.olegcherednik.zip4jvm.model.CompressionMethod;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;
Expand All @@ -38,25 +38,24 @@
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public abstract class EncryptedEntryOutputStream extends OutputStream {

public static EncryptedEntryOutputStream create(ZipEntry entry, EncoderDataOutput encoderDataOutput)
throws IOException {
public static EncryptedEntryOutputStream create(ZipEntry entry, DataOutput out) throws IOException {

CompressionMethod compressionMethod = entry.getCompressionMethod();
CompressionLevel compressionLevel = entry.getCompressionLevel();

if (compressionMethod == CompressionMethod.STORE)
return new StoreEntryOutputStream(encoderDataOutput);
return new StoreEntryOutputStream(out);
if (compressionMethod == CompressionMethod.DEFLATE)
return new DeflateEntryOutputStream(compressionLevel, encoderDataOutput);
return new DeflateEntryOutputStream(out, compressionLevel);
if (compressionMethod == CompressionMethod.BZIP2)
return new Bzip2EntryOutputStream(compressionLevel, encoderDataOutput);
return new Bzip2EntryOutputStream(out, compressionLevel);
if (compressionMethod == CompressionMethod.LZMA)
return new LzmaEntryOutputStream(compressionLevel,
return new LzmaEntryOutputStream(out,
compressionLevel,
entry.isLzmaEosMarker(),
entry.getUncompressedSize(),
encoderDataOutput);
entry.getUncompressedSize());
if (compressionMethod == CompressionMethod.ZSTD)
return new ZstdEntryOutputStream(compressionLevel, encoderDataOutput);
return new ZstdEntryOutputStream(out, compressionLevel);

throw new CompressionNotSupportedException(compressionMethod);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import ru.olegcherednik.zip4jvm.io.lzma.LzmaInputStream;
import ru.olegcherednik.zip4jvm.io.lzma.LzmaOutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.EncoderDataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;

import java.io.IOException;
Expand All @@ -31,32 +31,30 @@
*/
final class LzmaEntryOutputStream extends EncryptedEntryOutputStream {

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

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

private static LzmaOutputStream createOutputStream(CompressionLevel compressionLevel,
private static LzmaOutputStream createOutputStream(DataOutput out,
CompressionLevel compressionLevel,
boolean eosMarker,
long uncompressedSize,
EncoderDataOutput encoderDataOutput) throws IOException {
long uncompressedSize) throws IOException {
LzmaInputStream.Properties properties = new LzmaInputStream.Properties(compressionLevel);
return new LzmaOutputStream(encoderDataOutput, properties, eosMarker ? -1 : uncompressedSize);
return new LzmaOutputStream(out, properties, eosMarker ? -1 : uncompressedSize);
}

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
if (writeHeader) {
encoderDataOutput.writeByte((byte) 19); // major version
encoderDataOutput.writeByte((byte) 0); // minor version
encoderDataOutput.writeWord(5); // header size
out.writeByte((byte) 19); // major version
out.writeByte((byte) 0); // minor version
out.writeWord(5); // header size
lzma.writeHeader();
writeHeader = false;
}
Expand All @@ -67,12 +65,11 @@ public void write(byte[] buf, int offs, int len) throws IOException {
@Override
public void close() throws IOException {
lzma.close();
// encoderDataOutput.encodingAccomplished();
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package ru.olegcherednik.zip4jvm.io.out.entry.encrypted;

import ru.olegcherednik.zip4jvm.io.out.data.EncoderDataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
Expand All @@ -32,23 +32,16 @@
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
final class StoreEntryOutputStream extends EncryptedEntryOutputStream {

private final EncoderDataOutput encoderDataOutput;
private final DataOutput out;

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
encoderDataOutput.write(buf, offs, len);
}

@Override
public void close() throws IOException {
// encoderDataOutput.encodingAccomplished();
int a = 0;
a++;
out.write(buf, offs, len);
}

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

}
Loading

0 comments on commit 1b7b9e7

Please sign in to comment.