Loading src/edu/bu/ec504/hw1p3/compressors/CharByCharCompressor.java +3 −1 Original line number Diff line number Diff line package edu.bu.ec504.hw1p3.compressors; import edu.bu.ec504.hw1p3.util.Atom; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; Loading @@ -7,7 +9,7 @@ import java.util.ArrayList; /** * A simple implementation of the {@link Compressor} class. * This implementation ignores the dictionary entirely, and simply encodes each character of * the input as a trivial {@link Compressor.Atom}, indexed against the empty string. * the input as a trivial {@link Atom}, indexed against the empty string. */ public class CharByCharCompressor extends Compressor { Loading src/edu/bu/ec504/hw1p3/compressors/Compressor.java +54 −69 Original line number Diff line number Diff line package edu.bu.ec504.hw1p3.compressors; import edu.bu.ec504.hw1p3.util.Atom; import java.io.BufferedReader; import java.io.Serializable; import java.util.ArrayList; Loading @@ -8,40 +10,11 @@ import java.util.List; public abstract class Compressor implements Serializable { // nested classes /** * An immutable atom of compression that incorporates: * a. An integer representing an index into some dictionary. * b. A string that follows the dictionary word in a. */ static final class Atom implements Serializable { public Atom(int myIndex, String myNext) { index = myIndex; next = myNext; } public int getIndex() { return index; } public String getNext() { return next; } /** * Pretty print the atom. * @return A human-readable string representing this atom. * DEBUG==true prints debugging information during operation. * DEBUG==false does not print this debugging information */ @Override public String toString() { return "Atom{" + "index=" + index + ", next='" + next + '\'' + '}'; } private static final long serialVersionUID = 2L; // for serialization private final int index; private final String next; } public boolean DEBUG = true; /** * An encapsulation of a list of Atoms. Loading @@ -51,24 +24,32 @@ public abstract class Compressor implements Serializable { data = theData; } List<Atom> getList() { return data; } List<Atom> getList() { return data; } private final List<Atom> data; private static final long serialVersionUID = 3L; // for serialization } // METHODS // CONSTRUCTORS Compressor() { } /** * Process additional plaintext from <code>in</code> and incorporates it into this compressed object. * * @param in A BufferedReader linked to an input source. */ public abstract CompressedText processPlainText(BufferedReader in); /** * Decodes the meta-data stored within this object into an uncompressed text. * Reads the file, Atom by Atom, reconstructing the dictionary that was used * to produce it *and* the string that produced the dictionary. * * @return The uncompressed text corresponding to this object. */ final public String uncompress(CompressedText theCompressedText) { Loading @@ -76,9 +57,13 @@ public abstract class Compressor implements Serializable { StringBuilder result = new StringBuilder(); for (Atom anAtom : theCompressedText.getList()) { String nextStr=dict.get( anAtom.getIndex() ) + anAtom.getNext(); // the next String decoded from the theCompressedText String nextStr = dict.get(anAtom.getIndex()) + anAtom.getSuffix(); // the next String decoded from the theCompressedText result.append(nextStr); dict.add(nextStr); // add this string to the dictionary if (DEBUG) { System.out.println("Decoding Atom "+anAtom+ " to "+nextStr); } } return result.toString(); } Loading src/edu/bu/ec504/hw1p3/compressors/LineByLineCompressor.java +2 −0 Original line number Diff line number Diff line package edu.bu.ec504.hw1p3.compressors; import edu.bu.ec504.hw1p3.util.Atom; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; Loading src/edu/bu/ec504/hw1p3/util/Atom.java 0 → 100644 +41 −0 Original line number Diff line number Diff line package edu.bu.ec504.hw1p3.util; import java.io.Serializable; /** * An immutable atom that represents one string, and is built of two elements: * a. [prefix]: marked by an integer index into some compression dictionary. * The string at that index is a <i>prefix</i> of this atom's string. * b. [suffix]: a string that follows the prefix to complete the atom's string. * The string represented by this attom is this the concatenation of * [prefix] and [suffix] */ public final class Atom implements Serializable { public Atom(int myIndex, String mySuffix) { index = myIndex; suffix = mySuffix; } public int getIndex() { return index; } public String getSuffix() { return suffix; } /** * Pretty print the atom. * * @return A human-readable string representing this atom. */ @Override public String toString() { return "Atom{" + "index=" + index + ", next='" + suffix + '\'' + '}'; } private static final long serialVersionUID = 2L; // for serialization private final int index; private final String suffix; } src/edu/bu/ec504/hw1p3/util/ConvertToBase64.java +3 −0 Original line number Diff line number Diff line Loading @@ -29,6 +29,9 @@ public class ConvertToBase64 { Files.write(Paths.get(theOutputFile),decodedBytes); } /** * An example of how to convert the default input file into a base64 encoding. */ public static void main(String[] args) throws IOException { String base64 = toBase64(inputFileName); System.out.println("Base64:\n"+base64); Loading Loading
src/edu/bu/ec504/hw1p3/compressors/CharByCharCompressor.java +3 −1 Original line number Diff line number Diff line package edu.bu.ec504.hw1p3.compressors; import edu.bu.ec504.hw1p3.util.Atom; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; Loading @@ -7,7 +9,7 @@ import java.util.ArrayList; /** * A simple implementation of the {@link Compressor} class. * This implementation ignores the dictionary entirely, and simply encodes each character of * the input as a trivial {@link Compressor.Atom}, indexed against the empty string. * the input as a trivial {@link Atom}, indexed against the empty string. */ public class CharByCharCompressor extends Compressor { Loading
src/edu/bu/ec504/hw1p3/compressors/Compressor.java +54 −69 Original line number Diff line number Diff line package edu.bu.ec504.hw1p3.compressors; import edu.bu.ec504.hw1p3.util.Atom; import java.io.BufferedReader; import java.io.Serializable; import java.util.ArrayList; Loading @@ -8,40 +10,11 @@ import java.util.List; public abstract class Compressor implements Serializable { // nested classes /** * An immutable atom of compression that incorporates: * a. An integer representing an index into some dictionary. * b. A string that follows the dictionary word in a. */ static final class Atom implements Serializable { public Atom(int myIndex, String myNext) { index = myIndex; next = myNext; } public int getIndex() { return index; } public String getNext() { return next; } /** * Pretty print the atom. * @return A human-readable string representing this atom. * DEBUG==true prints debugging information during operation. * DEBUG==false does not print this debugging information */ @Override public String toString() { return "Atom{" + "index=" + index + ", next='" + next + '\'' + '}'; } private static final long serialVersionUID = 2L; // for serialization private final int index; private final String next; } public boolean DEBUG = true; /** * An encapsulation of a list of Atoms. Loading @@ -51,24 +24,32 @@ public abstract class Compressor implements Serializable { data = theData; } List<Atom> getList() { return data; } List<Atom> getList() { return data; } private final List<Atom> data; private static final long serialVersionUID = 3L; // for serialization } // METHODS // CONSTRUCTORS Compressor() { } /** * Process additional plaintext from <code>in</code> and incorporates it into this compressed object. * * @param in A BufferedReader linked to an input source. */ public abstract CompressedText processPlainText(BufferedReader in); /** * Decodes the meta-data stored within this object into an uncompressed text. * Reads the file, Atom by Atom, reconstructing the dictionary that was used * to produce it *and* the string that produced the dictionary. * * @return The uncompressed text corresponding to this object. */ final public String uncompress(CompressedText theCompressedText) { Loading @@ -76,9 +57,13 @@ public abstract class Compressor implements Serializable { StringBuilder result = new StringBuilder(); for (Atom anAtom : theCompressedText.getList()) { String nextStr=dict.get( anAtom.getIndex() ) + anAtom.getNext(); // the next String decoded from the theCompressedText String nextStr = dict.get(anAtom.getIndex()) + anAtom.getSuffix(); // the next String decoded from the theCompressedText result.append(nextStr); dict.add(nextStr); // add this string to the dictionary if (DEBUG) { System.out.println("Decoding Atom "+anAtom+ " to "+nextStr); } } return result.toString(); } Loading
src/edu/bu/ec504/hw1p3/compressors/LineByLineCompressor.java +2 −0 Original line number Diff line number Diff line package edu.bu.ec504.hw1p3.compressors; import edu.bu.ec504.hw1p3.util.Atom; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; Loading
src/edu/bu/ec504/hw1p3/util/Atom.java 0 → 100644 +41 −0 Original line number Diff line number Diff line package edu.bu.ec504.hw1p3.util; import java.io.Serializable; /** * An immutable atom that represents one string, and is built of two elements: * a. [prefix]: marked by an integer index into some compression dictionary. * The string at that index is a <i>prefix</i> of this atom's string. * b. [suffix]: a string that follows the prefix to complete the atom's string. * The string represented by this attom is this the concatenation of * [prefix] and [suffix] */ public final class Atom implements Serializable { public Atom(int myIndex, String mySuffix) { index = myIndex; suffix = mySuffix; } public int getIndex() { return index; } public String getSuffix() { return suffix; } /** * Pretty print the atom. * * @return A human-readable string representing this atom. */ @Override public String toString() { return "Atom{" + "index=" + index + ", next='" + suffix + '\'' + '}'; } private static final long serialVersionUID = 2L; // for serialization private final int index; private final String suffix; }
src/edu/bu/ec504/hw1p3/util/ConvertToBase64.java +3 −0 Original line number Diff line number Diff line Loading @@ -29,6 +29,9 @@ public class ConvertToBase64 { Files.write(Paths.get(theOutputFile),decodedBytes); } /** * An example of how to convert the default input file into a base64 encoding. */ public static void main(String[] args) throws IOException { String base64 = toBase64(inputFileName); System.out.println("Base64:\n"+base64); Loading