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 2ff10a3 commit 51521dc
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/main/java/ru/olegcherednik/zip4jvm/engine/ZipEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import ru.olegcherednik.zip4jvm.exception.EntryDuplicationException;
import ru.olegcherednik.zip4jvm.exception.EntryNotFoundException;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.SolidZipOutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.SplitZipOutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.SolidZipWriteFile;
import ru.olegcherednik.zip4jvm.io.out.data.SplitZipWriteFile;
import ru.olegcherednik.zip4jvm.io.writers.ExistedEntryWriter;
import ru.olegcherednik.zip4jvm.io.writers.ZipEntryWriter;
import ru.olegcherednik.zip4jvm.model.ZipModel;
Expand Down Expand Up @@ -261,7 +261,7 @@ private static Path createTempZip(Path zip) throws IOException {
}

private static DataOutput creatDataOutput(ZipModel zipModel) throws IOException {
return zipModel.isSplit() ? new SplitZipOutputStream(zipModel) : new SolidZipOutputStream(zipModel);
return zipModel.isSplit() ? new SplitZipWriteFile(zipModel) : new SolidZipWriteFile(zipModel);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import java.io.IOException;

/**
* This class contains general logic of {@link DataOutput}. Subclasses must
* implement {@link BaseDataOutput#writeInternal(byte[], int, int)} only. All
* other methods are not mandatory to override.
*
* @author Oleg Cherednik
* @since 03.08.2019
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
* @author Oleg Cherednik
* @since 08.03.2019
*/
public class SolidZipOutputStream extends BaseZipDataOutput {
public class SolidZipWriteFile extends WriteFileDataOutput {

public SolidZipOutputStream(ZipModel zipModel) throws IOException {
super(zipModel);
protected final ZipModel zipModel;

public SolidZipWriteFile(ZipModel zipModel) throws IOException {
this.zipModel = zipModel;
createFile(zipModel.getSrcZip().getPath());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@
* @since 08.03.2019
*/
@Getter
public class SplitZipOutputStream extends BaseZipDataOutput {
public class SplitZipWriteFile extends WriteFileDataOutput {

/** see 8.5.5 */
public static final int SPLIT_SIGNATURE = DataDescriptor.SIGNATURE;

protected final ZipModel zipModel;
private int diskNo;

@SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
public SplitZipOutputStream(ZipModel zipModel) throws IOException {
super(zipModel);
public SplitZipWriteFile(ZipModel zipModel) throws IOException {
this.zipModel = zipModel;
createFile(zipModel.getSrcZip().getPath());
ValidationUtils.requireZeroOrPositive(zipModel.getSplitSize(), "zipModel.splitSize");
writeDwordSignature(SPLIT_SIGNATURE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,58 @@
*/
package ru.olegcherednik.zip4jvm.io.out.data;

import ru.olegcherednik.zip4jvm.io.out.file.DataOutputFile;
import ru.olegcherednik.zip4jvm.io.out.file.LittleEndianWriteFile;
import ru.olegcherednik.zip4jvm.model.ZipModel;
import ru.olegcherednik.zip4jvm.io.out.file.WriteFile;

import java.io.IOException;
import java.nio.file.Path;

/**
* This is an adapter from {@link DataOutput} to {@link WriteFile}. Using this
* adapter it's possible to write data primitives to the given file.
* <p>
* Method {@link WriteFileDataOutput#createFile(Path)} should be invoked before
* writing anything to the output.
*
* @author Oleg Cherednik
* @since 11.02.2020
*/
abstract class BaseZipDataOutput extends BaseDataOutput {

protected final ZipModel zipModel;
private DataOutputFile delegate;
abstract class WriteFileDataOutput extends BaseDataOutput {

@SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
protected BaseZipDataOutput(ZipModel zipModel) throws IOException {
this.zipModel = zipModel;
createFile(zipModel.getSrcZip().getPath());
}
private WriteFile writeFile;

protected void createFile(Path zip) throws IOException {
delegate = new LittleEndianWriteFile(zip);
protected final void createFile(Path zip) throws IOException {
writeFile = LittleEndianWriteFile.create(zip);
}

@Override
public void fromLong(long val, byte[] buf, int offs, int len) {
delegate.fromLong(val, buf, offs, len);
writeFile.fromLong(val, buf, offs, len);
}

@Override
public final long getRelativeOffs() {
return delegate.getRelativeOffs();
return writeFile.getRelativeOffs();
}

@Override
protected void writeInternal(byte[] buf, int offs, int len) throws IOException {
delegate.write(buf, offs, len);
writeFile.write(buf, offs, len);
}

@Override
public void close() throws IOException {
delegate.close();
writeFile.close();
}

@Override
public void flush() throws IOException {
delegate.flush();
writeFile.flush();
}

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

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

import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -28,14 +33,18 @@
* @author Oleg Cherednik
* @since 02.08.2019
*/
public class LittleEndianWriteFile implements DataOutputFile {
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class LittleEndianWriteFile implements WriteFile {

private final OutputStream out;
private final OutputStream os;
private long relativeOffs;

public LittleEndianWriteFile(Path file) throws IOException {
Files.createDirectories(file.getParent());
out = new BufferedOutputStream(Files.newOutputStream(file));
public static LittleEndianWriteFile create(Path file) {
return Quietly.doQuietly(() -> {
Files.createDirectories(file.getParent());
OutputStream os = new BufferedOutputStream(Files.newOutputStream(file));
return new LittleEndianWriteFile(os);
});
}

@Override
Expand All @@ -48,7 +57,7 @@ public void fromLong(long val, byte[] buf, int offs, int len) {

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
out.write(buf, offs, len);
os.write(buf, offs, len);
relativeOffs += len;
}

Expand All @@ -59,12 +68,12 @@ public long getRelativeOffs() {

@Override
public void flush() throws IOException {
out.flush();
os.flush();
}

@Override
public void close() throws IOException {
out.close();
os.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author Oleg Cherednik
* @since 08.08.2019
*/
public interface DataOutputFile extends Closeable {
public interface WriteFile extends Closeable {

void write(byte[] buf, int offs, int len) throws IOException;

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

import ru.olegcherednik.zip4jvm.Zip4jvmSuite;
import ru.olegcherednik.zip4jvm.exception.Zip4jvmException;
import ru.olegcherednik.zip4jvm.io.out.data.SolidZipOutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.SolidZipWriteFile;
import ru.olegcherednik.zip4jvm.model.Charsets;
import ru.olegcherednik.zip4jvm.model.ZipModel;
import ru.olegcherednik.zip4jvm.model.src.SrcZip;
Expand All @@ -45,9 +45,9 @@
*/
@Test
@SuppressWarnings("FieldNamingConvention")
public class SolidZipOutputStreamTest {
public class SolidZipWriteFileTest {

private static final Path rootDir = Zip4jvmSuite.generateSubDirNameWithTime(SolidZipOutputStreamTest.class);
private static final Path rootDir = Zip4jvmSuite.generateSubDirNameWithTime(SolidZipWriteFileTest.class);

@BeforeClass
public static void createDir() throws IOException {
Expand All @@ -63,7 +63,7 @@ public void shouldWriteStreamWhenUsingDataOutput() throws IOException {
Path zip = Zip4jvmSuite.subDirNameAsMethodName(rootDir).resolve(fileNameDataSrc);
ZipModel zipModel = new ZipModel(SrcZip.of(zip));

try (SolidZipOutputStream out = new SolidZipOutputStream(zipModel)) {
try (SolidZipWriteFile out = new SolidZipWriteFile(zipModel)) {
assertThat(out.getRelativeOffs()).isEqualTo(0);

out.writeWord(0x0201);
Expand Down Expand Up @@ -101,7 +101,7 @@ public void shouldThrowExceptionWhenGetUnknownMark() throws IOException {
ZipModel zipModel = new ZipModel(SrcZip.of(zip));

assertThatThrownBy(() -> {
try (SolidZipOutputStream out = new SolidZipOutputStream(zipModel)) {
try (SolidZipWriteFile out = new SolidZipWriteFile(zipModel)) {
out.writeWord(0x0201);
out.writeDword(0x06050403);

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

import ru.olegcherednik.zip4jvm.Zip4jvmSuite;
import ru.olegcherednik.zip4jvm.io.out.data.SplitZipOutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.SplitZipWriteFile;
import ru.olegcherednik.zip4jvm.model.Charsets;
import ru.olegcherednik.zip4jvm.model.ZipModel;
import ru.olegcherednik.zip4jvm.model.src.SrcZip;
Expand All @@ -44,9 +44,9 @@
*/
@Test
@SuppressWarnings("FieldNamingConvention")
public class SplitZipOutputStreamTest {
public class SplitZipWriteFileTest {

private static final Path rootDir = Zip4jvmSuite.generateSubDirNameWithTime(SplitZipOutputStreamTest.class);
private static final Path rootDir = Zip4jvmSuite.generateSubDirNameWithTime(SplitZipWriteFileTest.class);

@BeforeClass
public static void createDir() throws IOException {
Expand All @@ -63,7 +63,7 @@ public void shouldWriteStreamWhenUsingDataOutput() throws IOException {
ZipModel zipModel = new ZipModel(SrcZip.of(zip));
zipModel.setSplitSize(10);

try (SplitZipOutputStream out = new SplitZipOutputStream(zipModel)) {
try (SplitZipWriteFile out = new SplitZipWriteFile(zipModel)) {
assertThat(out.getRelativeOffs()).isEqualTo(4);

out.writeWord(0x0201);
Expand Down Expand Up @@ -108,7 +108,7 @@ public void shouldMoveToNextDiskWhenNotEnoughSpaceToWriteSignature() throws IOEx
ZipModel zipModel = new ZipModel(SrcZip.of(zip));
zipModel.setSplitSize(10);

try (SplitZipOutputStream out = new SplitZipOutputStream(zipModel)) {
try (SplitZipWriteFile out = new SplitZipWriteFile(zipModel)) {
assertThat(out.getRelativeOffs()).isEqualTo(4);

out.writeDwordSignature(0x01020304);
Expand Down Expand Up @@ -139,7 +139,7 @@ public void shouldThrowExceptionWhenSplitFileExists() throws IOException {
zipModel.setTotalDisks(0);

assertThatThrownBy(() -> {
try (SplitZipOutputStream out = new SplitZipOutputStream(zipModel)) {
try (SplitZipWriteFile out = new SplitZipWriteFile(zipModel)) {
assertThat(out.getRelativeOffs()).isEqualTo(4);

out.writeDwordSignature(0x01020304);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -52,7 +53,7 @@ public static void removeDir() throws IOException {
public void shouldSupportFlush() throws IOException {
Path file = Zip4jvmSuite.subDirNameAsMethodName(rootDir).resolve("foo.txt");

try (LittleEndianWriteFile out = new LittleEndianWriteFile(file)) {
try (LittleEndianWriteFile out = new LittleEndianWriteFile(new BufferedOutputStream(Files.newOutputStream(file)))) {
assertThat(Files.readAllBytes(file)).isEmpty();

out.write(new byte[] { 0x0, 0x1, 0x2 }, 0, 3);
Expand Down

0 comments on commit 51521dc

Please sign in to comment.