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 9e6b55e commit ff02615
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,31 @@
package ru.olegcherednik.zip4jvm.io.out.data;

import ru.olegcherednik.zip4jvm.crypto.Encoder;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

import lombok.RequiredArgsConstructor;

import java.io.IOException;

/**
* This interface describes ability to write an encoded data items to the
* given {@link DataOutput}. I.e. this is a decorator, that can encrypt
* incoming data and write it to the given {@link DataOutput}.
* This interface describes ability to write an encrypted data items to the
* given {@link DataOutput} using given {@link Encoder}. I.e. this is a
* decorator, that can encrypt incoming data and write it to the given
* {@link DataOutput}.
*
* @author Oleg Cherednik
* @since 11.02.2020
*/
@RequiredArgsConstructor
public class EncoderDataOutput extends BaseDataOutput {
public class EncryptedDataOutput extends BaseDataOutput {

private final Encoder encoder;
private final DataOutput out;

public static EncryptedDataOutput create(ZipEntry zipEntry, DataOutput out) {
return new EncryptedDataOutput(zipEntry.createEncoder(), out);
}

public void writeEncryptionHeader() throws IOException {
encoder.writeEncryptionHeader(out);
}
Expand All @@ -64,7 +70,6 @@ protected void writeInternal(byte[] buf, int offs, int len) throws IOException {

@Override
public void close() throws IOException {
System.out.println(EncoderDataOutput.class.getSimpleName() + ".close()");
out.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void write(byte[] buf, int offs, int len) throws IOException {

@Override
public void close() throws IOException {
System.out.println(Bzip2EntryOutputStream.class.getSimpleName() + ".close()");
bzip2.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@

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

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

/**
* This class represents a compressed stream using given {@link CompressionMethod}.
* It extends from the {@link OutputStream} to be able to use standard output
* optimizations (e.g. from {@link IOUtils}).
*
* @author Oleg Cherednik
* @since 12.02.2020
*/
Expand Down Expand Up @@ -66,7 +71,7 @@ public final void write(int b) throws IOException {

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
throw new NotImplementedException("EncryptedEntryOutputStream.write(byte[], int, int)");
throw new NotImplementedException("CompressedEntryOutputStream.write(byte[], int, int)");
}

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

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.EncoderDataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.EncryptedDataOutput;
import ru.olegcherednik.zip4jvm.io.out.entry.PayloadCalculationOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.encrypted.CompressedEntryOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.DataDescriptorOut;
Expand Down Expand Up @@ -50,8 +50,9 @@ public final class ZipEntryWriter implements Writer {
public void write(DataOutput out) throws IOException {
// 1. compression
// 2. encryption
EncoderDataOutput encoderDataOutput = new EncoderDataOutput(zipEntry.createEncoder(), out);
CompressedEntryOutputStream czeos = CompressedEntryOutputStream.create(zipEntry, encoderDataOutput);
EncryptedDataOutput encryptedDataOutput = EncryptedDataOutput.create(zipEntry, out);
CompressedEntryOutputStream compressedOutputStream =
CompressedEntryOutputStream.create(zipEntry, encryptedDataOutput);

zipEntry.setDiskNo(out.getDiskNo());

Expand All @@ -62,14 +63,14 @@ public void write(DataOutput out) throws IOException {

new LocalFileHeaderOut().write(zipEntry, out);
out.mark(COMPRESSED_DATA);
encoderDataOutput.writeEncryptionHeader();
encryptedDataOutput.writeEncryptionHeader();

try (InputStream in = zipEntry.getInputStream();
PayloadCalculationOutputStream os = new PayloadCalculationOutputStream(zipEntry, czeos)) {
PayloadCalculationOutputStream os = new PayloadCalculationOutputStream(zipEntry, compressedOutputStream)) {
IOUtils.copyLarge(in, os);
}

encoderDataOutput.encodingAccomplished();
encryptedDataOutput.encodingAccomplished();
zipEntry.setCompressedSize(out.getWrittenBytesAmount(COMPRESSED_DATA));
new UpdateZip64().update(zipEntry);

Expand Down

0 comments on commit ff02615

Please sign in to comment.