Løsningsforslag - oppgaver i Avsnitt 7.2.5


Oppgave 1

public static void main(String... args) throws IOException
{
  byte[] b = "ABBABABAC".getBytes();                      // byte-tabell
  InputStream fra = new ByteArrayInputStream(b);          // InputStream

  ByteArrayOutputStream ut = new ByteArrayOutputStream(); // OutputStream
  BitOutputStream til = new BitOutputStream(ut);          // BitOutputStream

  LZW.komprimer(fra, til);                                // komprimerer

  fra.close(); til.close();                               // lukker

  byte[] c = ut.toByteArray();                            // byte-tabell
  System.out.println(Arrays.toString(c));                 // komprimert melding
  // Utskift: [32, -112, -120, 80, 24, 33, 12]

  InputStream tallkoder = new ByteArrayInputStream(c);    // InputStream
  BitInputStream les = new BitInputStream(tallkoder);     // BitInputStream
  LZW.dekomprimer(les, System.out);                       // ABBABABAC	
}

Oppgave 2

  String fraUrl = "https://www.cs.hioa.no/~ulfu/appolonius/kap7/2/mystisk.txt";
  BitInputStream fra = new BitInputStream((new URL(fraUrl)).openStream());
  LZW.dekomprimer(fra, System.out);