From 816548386eba184821e8a48f812bca0055eaaf73 Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Wed, 17 Aug 2016 12:20:00 +1000 Subject: [PATCH] added walk() --- .../diskbrowser/wizardry/Huffman.java | 44 +++++++++++++++++-- .../wizardry/Wizardry4BootDisk.java | 6 +-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/com/bytezone/diskbrowser/wizardry/Huffman.java b/src/com/bytezone/diskbrowser/wizardry/Huffman.java index a05b589..6426a65 100644 --- a/src/com/bytezone/diskbrowser/wizardry/Huffman.java +++ b/src/com/bytezone/diskbrowser/wizardry/Huffman.java @@ -1,8 +1,10 @@ package com.bytezone.diskbrowser.wizardry; +import com.bytezone.diskbrowser.applefile.AbstractFile; + // Based on a pascal routine by Tom Ewers -public class Huffman +public class Huffman extends AbstractFile { private final byte[] tree; private final byte[] left; @@ -13,8 +15,12 @@ public class Huffman private int currentByte; private byte[] message; - public Huffman (byte[] buffer) + private String treeContents; + + public Huffman (String name, byte[] buffer) { + super (name, buffer); + tree = new byte[256]; left = new byte[256]; right = new byte[256]; @@ -47,7 +53,7 @@ public class Huffman { if (bitNo-- == 0) { - bitNo = 7; + bitNo += 8; currentByte = message[msgPtr++] & 0xFF; } @@ -68,4 +74,36 @@ public class Huffman } } } + + @Override + public String getText () + { + if (treeContents == null) + { + StringBuilder text = new StringBuilder (); + walk (0, "", text); + treeContents = text.toString (); + } + return treeContents; + } + + private void walk (int treePtr, String path, StringBuilder text) + { + if ((tree[treePtr] & 0x01) == 0) + walk (left[treePtr] & 0xFF, path + "1", text); + else + print (path + "1", left[treePtr], text); + + if ((tree[treePtr] & 0x02) == 0) + walk (right[treePtr] & 0xFF, path + "0", text); + else + print (path + "0", right[treePtr], text); + } + + private void print (String path, byte value, StringBuilder text) + { + int val = value & 0xFF; + char c = val < 32 || val >= 127 ? ' ' : (char) val; + text.append (String.format ("%3d %1.1s %s%n", val, c, path)); + } } \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/wizardry/Wizardry4BootDisk.java b/src/com/bytezone/diskbrowser/wizardry/Wizardry4BootDisk.java index 06b3ebb..8aab2fc 100644 --- a/src/com/bytezone/diskbrowser/wizardry/Wizardry4BootDisk.java +++ b/src/com/bytezone/diskbrowser/wizardry/Wizardry4BootDisk.java @@ -94,11 +94,9 @@ public class Wizardry4BootDisk extends PascalDisk byte[] buffer = fileEntry.getDataSource ().buffer; - Huffman huffman = new Huffman (buffer); + Huffman huffman = new Huffman ("Huffman tree", buffer); messageBlock.setHuffman (huffman); - - // for (int msg = 2043; msg <= 2045; msg++) - // System.out.println (huffman.getMessage (messageBlock.getMessage (msg))); + fileEntry.setFile (huffman); } }