import CITS2200.*; import java.io.*; import java.util.*; /** *A very basic run length encoder, to rpovide a template for IO etc **/ public class RLE implements Compressor{ /** *default constructor **/ public RLE(){} /** *Reads a series of bytes from an inputstream and *executes a compression algorithm over those bytes, writing * the compressed data to the specified outputStream. *@param in the InputStream for the data *@param out the outputStream for writing the compressed data *@return a String reporting information to be logged. **/ public String compress(InputStream in, OutputStream out){ try{ //Uncomment the following line to use the write buffer //WriteBuffer b = new WriteBuffer(out); StringBuffer log = new StringBuffer(""); int bite = in.read(); while(bite!=-1){ int count = 1; int next = in.read(); while(next==bite && count<255){ count++; next = in.read(); } out.write(count); out.write(bite); //uncomment the following lines //to use the write buffer //b.write(8, count); //b.write(8, bite); log.append("Wrote "+count+" "+bite+"'s\n"); bite = next; } return log.toString(); } catch(IOException e){ e.printStackTrace(); return("Fail!"); } } /** *Reads a series of bytes from compressed data and *executes a decompression algorithm over those bytes, writing *the decompressed data to the specified outputStream. *@param in the InputStream for the compressed data *@param out the outputStream for writing the decompressed data *@return a String reporting information to be logged. **/ public String decompress(InputStream in, OutputStream out){ try{ StringBuffer log = new StringBuffer(""); int count = in.read(); while(count!=-1){ int bite = in.read(); for(int i =0; i=8){ out.write((int) (buffer>>(pos-8))); pos = pos-8; } } /** *pads the final output with 0's and writes to *the output stream **/ public void flush() throws IOException{ write((8-pos%8)%8,0); } } }