Commit de8d9527 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Some tweaks to the API

parent 0a127a04
Loading
Loading
Loading
Loading
+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;
@@ -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 {

+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;
@@ -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.
@@ -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) {
@@ -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();
    }
+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;
+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;
}
+3 −0
Original line number Diff line number Diff line
@@ -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);