// Template provided by Paul Amer for use in Project 3 // of CISC451/561: Data Compression for Multimedia // This applet opens a text window and echos the contents // of an ASCII file in that window. Students must complete // this template with their code for decoding amerRLE encoded // files. // Modified by Ezra Kissel and Dhaval Mehta // Date: 4/20/03 // Project 3 - CISC451/651 // Included modified DecompressFile() method to display // a decompressed file pointed to by FileToBeEchoed // in an applet window. import java.net.*; import java.io.*; import java.applet.Applet; import java.awt.*; // Abstract Windowing Toolkit package is imported. public class decode_applet extends Applet { // declare global applet variables private String filename; private TextArea outputArea; private DataInputStream inFile; // set length of the compressed file header static final int HEADER_LENGTH = 7; // make version equal to 1 static final char VERSION = '1'; // define alphabet size static final int ALPH_SIZE = 128; // define amerRLE header static final char[] amerRLE = {'a', 'm', 'e', 'r', 'R', 'L', 'E'}; // init invoked automatically by browser. public void init () { outputArea = new TextArea(15, 65); // rows, columns outputArea.setEditable(false); // make applet read-only outputArea.setFont(new Font("Courier", Font.PLAIN, 12)); // fixed-width font add(outputArea); // show empty text area in applet try { // file to be decoded should be a param in the html file // if param doesn't exist the html file, give error message filename = getParameter("FileToBeEchoed"); if (filename == null) { outputArea.append("ERROR: parameter FileToBeEchoed missing in html file"); return; } // create http connection to later get compressed file URL ourURLConnection = new URL(filename); // open the stream to be used for downloading the compressed file inFile = new DataInputStream(ourURLConnection.openStream()); DecompressFile(); inFile.close(); } catch (Exception e) { e.printStackTrace(); } } // Method DecompressFile() // decompresses the file, outputs decompressed characters to output TextArea // return values : 1 if successful, 0 if an error was encountered // arguments : none private int DecompressFile() { try { // store a character of the input char charr; // counters and temp holders int cur, prev = -1, count = 0, aLen = 0, replicator; // store the header signature byte[] signature = new byte[HEADER_LENGTH]; // store the alphabet size byte[] alphaLen = new byte[3]; // store the alphabet byte[] alphabet = new byte[128]; // read in signature, validate inFile.read(signature); for(int i = 0; i < HEADER_LENGTH; ++i) if(signature[i] != amerRLE[i]) { outputArea.append("\nERROR: Wrong signature."); return 0; } // check for correct version if(inFile.read() != VERSION) outputArea.append("\nERROR: Wrong version."); // find out the length of the alphabet inFile.read(alphaLen); aLen = Integer.parseInt(new String(alphaLen)); // read the alphabet in a byte array for(int i = 0; i < aLen; ++i) { byte temp = (byte)inFile.read(); alphabet[(int)temp] = temp; } // do the decompression while((replicator = inFile.read()) != -1) { // check if the replicator is in [1, 9] if((replicator-48) < 1 || (replicator-48) > 9) { outputArea.append("\nERROR:Replicator not in 1-9."); return 0; } // check if the character is in the alphabet charr = (char)inFile.read(); if(charr != alphabet[(int)charr]) { outputArea.append("\nERROR: '" + charr + "' not in the alphabet."); return 0; } // if all is well, write to file for(int i = 0; i < (replicator-48); ++i) outputArea.append(String.valueOf(charr)); } } catch (IOException e) { System.out.println("Exception raised: " + e); System.exit(-1); } return 1; } }