Skip to content

Commit

Permalink
Reduce code smells, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Feb 1, 2024
1 parent bd9f54b commit cf2eccd
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 81 deletions.
6 changes: 3 additions & 3 deletions src/main/java/mpo/dayon/common/squeeze/Compressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ public final class Compressor {
/**
* NONE. (testing only)
*/
private static final Compressor NULL_COMPRESSOR = new Compressor(CompressionMethod.NONE, new NullRunLengthEncoder(), new NULL_Zipper());
private static final Compressor NULL_COMPRESSOR = new Compressor(CompressionMethod.NONE, new NullRunLengthEncoder(), new NullZipper());

/**
* ZIP (with regular run-length-encoding).
*/
private static final Compressor ZIP_COMPRESSOR = new Compressor(CompressionMethod.ZIP, new RegularRunLengthEncoder(), new ZIP_Zipper());
private static final Compressor ZIP_COMPRESSOR = new Compressor(CompressionMethod.ZIP, new RegularRunLengthEncoder(), new ZipZipper());

/**
* XZ.
*/
private static final Compressor XZ_COMPRESSOR = new Compressor(CompressionMethod.XZ, new NullRunLengthEncoder(), new XZ_Zipper());
private static final Compressor XZ_COMPRESSOR = new Compressor(CompressionMethod.XZ, new NullRunLengthEncoder(), new XzZipper());

private final CompressionMethod method;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package mpo.dayon.common.squeeze;

import mpo.dayon.common.buffer.MemByteBuffer;

public class NULL_Zipper implements Zipper {
@Override
public MemByteBuffer zip(MemByteBuffer unzipped) {
final MemByteBuffer zipped = new MemByteBuffer();
zipped.write(unzipped.getInternal(), 0, unzipped.size());
return zipped;
}

@Override
public MemByteBuffer unzip(MemByteBuffer zipped) {
final MemByteBuffer unzipped = new MemByteBuffer();
unzipped.write(zipped.getInternal(), 0, zipped.size());
return unzipped;
}
}
package mpo.dayon.common.squeeze;

import mpo.dayon.common.buffer.MemByteBuffer;

public class NullZipper implements Zipper {
@Override
public MemByteBuffer zip(MemByteBuffer unzipped) {
final MemByteBuffer zipped = new MemByteBuffer();
zipped.write(unzipped.getInternal(), 0, unzipped.size());
return zipped;
}

@Override
public MemByteBuffer unzip(MemByteBuffer zipped) {
final MemByteBuffer unzipped = new MemByteBuffer();
unzipped.write(zipped.getInternal(), 0, zipped.size());
return unzipped;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.io.*;

public class XZ_Zipper implements Zipper {
public class XzZipper implements Zipper {
@Override
public MemByteBuffer zip(MemByteBuffer unCompressed) throws IOException {
try (MemByteBuffer compressed = new MemByteBuffer()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,50 @@
package mpo.dayon.common.squeeze;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import mpo.dayon.common.buffer.MemByteBuffer;

public class ZIP_Zipper implements Zipper {

@Override
public MemByteBuffer zip(MemByteBuffer unzipped) throws IOException {
final MemByteBuffer zipped = new MemByteBuffer();

final OutputStream zip = createZipOutputStream(zipped);

zip.write(unzipped.getInternal(), 0, unzipped.size());
zip.flush();

zip.close();

return zipped;
}

private static OutputStream createZipOutputStream(MemByteBuffer zipped) throws IOException {
final ZipOutputStream zip = new ZipOutputStream(zipped);
zip.putNextEntry(new ZipEntry("dirty-tiles"));
return zip;
}

@Override
public MemByteBuffer unzip(MemByteBuffer zipped) throws IOException {
try (final MemByteBuffer unzipped = new MemByteBuffer()) {
final InputStream unzip = createZipInputStream(zipped);

final byte[] buffer = new byte[4096];

int count;
while ((count = unzip.read(buffer)) > 0) {
unzipped.write(buffer, 0, count);
}

unzip.close();

return unzipped;
}
}

private static InputStream createZipInputStream(MemByteBuffer zipped) throws IOException {
final ZipInputStream unzip = new ZipInputStream(new ByteArrayInputStream(zipped.getInternal(), 0, zipped.size()));
unzip.getNextEntry();
return unzip;
}

package mpo.dayon.common.squeeze;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import mpo.dayon.common.buffer.MemByteBuffer;

public class ZipZipper implements Zipper {

@Override
public MemByteBuffer zip(MemByteBuffer unzipped) throws IOException {
final MemByteBuffer zipped = new MemByteBuffer();
final OutputStream zip = createZipOutputStream(zipped);
zip.write(unzipped.getInternal(), 0, unzipped.size());
zip.flush();
zip.close();
return zipped;
}

private static OutputStream createZipOutputStream(MemByteBuffer zipped) throws IOException {
final ZipOutputStream zip = new ZipOutputStream(zipped);
zip.putNextEntry(new ZipEntry("dirty-tiles"));
return zip;
}

@Override
public MemByteBuffer unzip(MemByteBuffer zipped) throws IOException {
try (final MemByteBuffer unzipped = new MemByteBuffer()) {
final InputStream unzip = createZipInputStream(zipped);
final byte[] buffer = new byte[4096];
int count;
while ((count = unzip.read(buffer)) > 0) {
unzipped.write(buffer, 0, count);
}
unzip.close();
return unzipped;
}
}

private static InputStream createZipInputStream(MemByteBuffer zipped) throws IOException {
final ZipInputStream unzip = new ZipInputStream(new ByteArrayInputStream(zipped.getInternal(), 0, zipped.size()));
unzip.getNextEntry();
return unzip;
}
}
25 changes: 25 additions & 0 deletions src/test/java/mpo/dayon/common/squeeze/XzZipperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mpo.dayon.common.squeeze;

import mpo.dayon.common.buffer.MemByteBuffer;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.*;

class XzZipperTest {

@Test
void zipAndUnzip() throws IOException {
// given
int star = 42;
MemByteBuffer origin = new MemByteBuffer();
origin.write(star);
XzZipper zipper = new XzZipper();
// when
final MemByteBuffer unzipped = zipper.unzip(zipper.zip(origin));
// then
assertEquals(new String(origin.getInternal(), UTF_8), new String(unzipped.getInternal(), UTF_8));
}
}
25 changes: 25 additions & 0 deletions src/test/java/mpo/dayon/common/squeeze/ZipZipperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mpo.dayon.common.squeeze;

import mpo.dayon.common.buffer.MemByteBuffer;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.*;

class ZipZipperTest {

@Test
void zipAndUnzip() throws IOException {
// given
int star = 42;
MemByteBuffer origin = new MemByteBuffer();
origin.write(star);
ZipZipper zipper = new ZipZipper();
// when
final MemByteBuffer unzipped = zipper.unzip(zipper.zip(origin));
// then
assertEquals(new String(origin.getInternal(), UTF_8), new String(unzipped.getInternal(), UTF_8));
}
}

0 comments on commit cf2eccd

Please sign in to comment.