Skip to content

Commit 5ff79e0

Browse files
committed
Tweak to pull in Apache Commons Codec.
1 parent 75d5d8d commit 5ff79e0

File tree

4 files changed

+114
-39
lines changed

4 files changed

+114
-39
lines changed

pom.xml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
3+
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>a2geek.apple2</groupId>
66
<artifactId>apple2-image-encoder</artifactId>
7-
<version>4.4.0-FINAL</version>
7+
<version>5.0.0-SNAPSHOT</version>
88
<name>Apple II Image Encoder</name>
99
<description>A utility that got out of hand and became an application to move modern images to the Apple II platform.</description>
10-
10+
1111
<properties>
1212
<maven.compiler.source>1.8</maven.compiler.source>
1313
<maven.compiler.target>1.8</maven.compiler.target>
14-
<maven.shade.version>2.4.3</maven.shade.version>
14+
<maven.shade.version>3.0.0</maven.shade.version>
1515
<junit.version>4.12</junit.version>
16+
<commons-compress.version>1.14</commons-compress.version>
1617
</properties>
1718

1819
<dependencies>
@@ -22,6 +23,23 @@
2223
<version>${junit.version}</version>
2324
<scope>test</scope>
2425
</dependency>
26+
<dependency>
27+
<groupId>org.apache.commons</groupId>
28+
<artifactId>commons-compress</artifactId>
29+
<version>${commons-compress.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.brotli</groupId>
33+
<artifactId>dec</artifactId>
34+
<version>0.1.2</version>
35+
<optional>true</optional>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.tukaani</groupId>
39+
<artifactId>xz</artifactId>
40+
<version>1.6</version>
41+
<optional>true</optional>
42+
</dependency>
2543
</dependencies>
2644

2745
<build>
@@ -52,4 +70,5 @@
5270
</executions>
5371
</plugin>
5472
</plugins>
55-
</build></project>
73+
</build>
74+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package a2geek.apple2.image.encoder.encode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.apache.commons.compress.compressors.CompressorStreamFactory;
7+
8+
public abstract class A2EncoderFactory {
9+
public static List<A2Encoder> getEncoders() {
10+
List<A2Encoder> list = new ArrayList<>();
11+
list.add(new RleEncoder());
12+
list.add(new VariableRleEncoder());
13+
list.add(new PackBitsEncoder());
14+
list.add(new BitPack1());
15+
list.add(new BitPack2());
16+
list.add(new BitPack3());
17+
list.add(new GZipEncoder());
18+
list.add(new ZipEncoder());
19+
// From Apache Commons
20+
for (String outputProvider : CompressorStreamFactory.findAvailableCompressorOutputStreamProviders().keySet()) {
21+
// PACK200 does nothing for some reason, so just ignoring it
22+
if (CompressorStreamFactory.PACK200.equals(outputProvider)) continue;
23+
list.add(new CommonsCodecEncoder(outputProvider));
24+
}
25+
return list;
26+
}
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package a2geek.apple2.image.encoder.encode;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
6+
import org.apache.commons.compress.compressors.CompressorException;
7+
import org.apache.commons.compress.compressors.CompressorOutputStream;
8+
import org.apache.commons.compress.compressors.CompressorStreamFactory;
9+
import org.apache.commons.compress.compressors.CompressorStreamProvider;
10+
11+
import a2geek.apple2.image.encoder.A2Image;
12+
import a2geek.apple2.image.encoder.ui.ImageEncoderApp;
13+
14+
public class CommonsCodecEncoder extends A2Encoder {
15+
private CompressorStreamProvider provider = new CompressorStreamFactory();
16+
private String name;
17+
private String title;
18+
19+
public CommonsCodecEncoder(String name) {
20+
this(name, name.toUpperCase());
21+
}
22+
public CommonsCodecEncoder(String name, String title) {
23+
this.name = name;
24+
this.title = title;
25+
}
26+
27+
@Override
28+
public void encode(A2Image a2image, int maxSize) {
29+
try {
30+
reset(maxSize);
31+
ByteArrayOutputStream ba = new ByteArrayOutputStream();
32+
CompressorOutputStream out = provider.createCompressorOutputStream(name, ba);
33+
out.write(a2image.getBytes());
34+
out.close();
35+
for (byte b : ba.toByteArray()) {
36+
addByte(b);
37+
}
38+
} catch (CompressorException | IOException ex) {
39+
ImageEncoderApp.showErrorDialog("Apache Commons '" + name + "' compress", ex);
40+
}
41+
}
42+
43+
@Override
44+
public String getTitle() {
45+
return title;
46+
}
47+
48+
}

src/main/java/a2geek/apple2/image/encoder/ui/EncoderTableModel.java

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@
77

88
import a2geek.apple2.image.encoder.A2Image;
99
import a2geek.apple2.image.encoder.encode.A2Encoder;
10-
import a2geek.apple2.image.encoder.encode.BitPack1;
11-
import a2geek.apple2.image.encoder.encode.BitPack2;
12-
import a2geek.apple2.image.encoder.encode.BitPack3;
13-
import a2geek.apple2.image.encoder.encode.GZipEncoder;
14-
import a2geek.apple2.image.encoder.encode.PackBitsEncoder;
15-
import a2geek.apple2.image.encoder.encode.RleEncoder;
16-
import a2geek.apple2.image.encoder.encode.VariableRleEncoder;
17-
import a2geek.apple2.image.encoder.encode.ZipEncoder;
10+
import a2geek.apple2.image.encoder.encode.A2EncoderFactory;
1811
import a2geek.apple2.image.encoder.util.ProgressListener;
1912

2013
/**
@@ -25,38 +18,26 @@
2518
*/
2619
@SuppressWarnings("serial")
2720
public class EncoderTableModel extends AbstractTableModel {
28-
private List<A2Encoder> encoders = new ArrayList<A2Encoder>();
21+
private final List<A2Encoder> results = new ArrayList<>();
2922
private byte[] data = null;
3023
private String[] headers = new String[] { "Type", "Original", "Compressed", "%" };
3124
/**
3225
* Construct the TableModel and setup all the A2Encoders.
3326
*/
3427
public EncoderTableModel(A2Image image, int maxSize, ProgressListener listener) {
3528
this.data = image.getBytes();
36-
37-
Class[] encoderClasses = new Class[] {
38-
RleEncoder.class,
39-
VariableRleEncoder.class,
40-
PackBitsEncoder.class,
41-
BitPack1.class,
42-
BitPack2.class,
43-
BitPack3.class,
44-
GZipEncoder.class,
45-
ZipEncoder.class
46-
};
29+
4730
int count = 0;
48-
for (Class encoderClass : encoderClasses) {
49-
if (listener != null && listener.isCancelled()) return;
50-
A2Encoder a2encoder = null;
31+
int size = A2EncoderFactory.getEncoders().size();
32+
for (A2Encoder encoder : A2EncoderFactory.getEncoders()) {
5133
try {
52-
a2encoder = (A2Encoder)encoderClass.newInstance();
34+
if (listener != null && listener.isCancelled()) return;
5335
count++;
54-
if (listener != null) listener.update(count, encoderClasses.length, a2encoder.getTitle());
55-
a2encoder.encode(image, maxSize);
56-
encoders.add(a2encoder);
36+
if (listener != null) listener.update(count, size, encoder.getTitle());
37+
encoder.encode(image, maxSize);
38+
results.add(encoder);
5739
} catch (Throwable t) {
58-
// FIXME ignore errors...
59-
//ImageEncoderApp.showErrorDialog(a2encoder.getTitle(), t);
40+
// Ignoring these as some are expected to throw errors if boundaries are missed
6041
}
6142
}
6243
}
@@ -70,7 +51,7 @@ public String getColumnName(int column) {
7051
* Answer with the number of rows.
7152
*/
7253
public int getRowCount() {
73-
return encoders.size();
54+
return results.size();
7455
}
7556
/**
7657
* Answer with the number of columns.
@@ -84,13 +65,13 @@ public int getColumnCount() {
8465
public Object getValueAt(int rowIndex, int columnIndex) {
8566
switch (columnIndex) {
8667
case 0:
87-
return encoders.get(rowIndex).getTitle();
68+
return results.get(rowIndex).getTitle();
8869
case 1:
8970
return data.length;
9071
case 2:
91-
return encoders.get(rowIndex).getSize();
72+
return results.get(rowIndex).getSize();
9273
case 3:
93-
return (encoders.get(rowIndex).getSize() * 100) / data.length;
74+
return (results.get(rowIndex).getSize() * 100) / data.length;
9475
default:
9576
return "Unknown";
9677
}
@@ -116,6 +97,6 @@ public Class<?> getColumnClass(int columnIndex) {
11697
* Answer with the specific A2Encoder.
11798
*/
11899
public A2Encoder getSelectedEncoder(int rowIndex) {
119-
return encoders.get(rowIndex);
100+
return results.get(rowIndex);
120101
}
121102
}

0 commit comments

Comments
 (0)