From 5c3f6a07530cc0b80089fe6d89765c2603199aab Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Wed, 17 Aug 2016 21:33:27 +1000 Subject: [PATCH] tidying --- .../diskbrowser/wizardry/Huffman.java | 31 +++++++++++-------- .../wizardry/MessageDataBlock.java | 2 +- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/com/bytezone/diskbrowser/wizardry/Huffman.java b/src/com/bytezone/diskbrowser/wizardry/Huffman.java index f051f3e..0694c3a 100644 --- a/src/com/bytezone/diskbrowser/wizardry/Huffman.java +++ b/src/com/bytezone/diskbrowser/wizardry/Huffman.java @@ -4,11 +4,17 @@ import com.bytezone.diskbrowser.applefile.AbstractFile; // Based on a pascal routine by Tom Ewers +// link for possible display algorithm: +// http://stackoverflow.com/questions/14184655/set-position-for-drawing-binary-tree + public class Huffman extends AbstractFile { private static final int LEFT = 256; private static final int RIGHT = 512; + private static final byte[] mask = { 2, 1 }; + private static final int[] offset = { RIGHT, LEFT }; + private int bitNo; private int msgPtr; private int currentByte; @@ -21,7 +27,7 @@ public class Huffman extends AbstractFile super (name, buffer); } - public String getMessage (byte[] message) + public String decodeMessage (byte[] message) { this.message = message; bitNo = 0; @@ -51,18 +57,15 @@ public class Huffman extends AbstractFile int currentBit = currentByte % 2; // get the next bit to process currentByte /= 2; - if (currentBit == 0) // take right path - { - if ((buffer[treePtr] & 0x02) != 0) // if has right leaf... - return buffer[treePtr + RIGHT]; // return that character - treePtr = buffer[treePtr + RIGHT] & 0xFF; // else traverse right node - } - else // take left path - { - if ((buffer[treePtr] & 0x01) != 0) // if has left leaf... - return buffer[treePtr + LEFT]; // return that character - treePtr = buffer[treePtr + LEFT] & 0xFF; // else traverse left node - } + // use currentBit to determine whether to use the left or right node + byte nodeValue = buffer[treePtr + offset[currentBit]]; + + // if the node is a leaf, return its contents + if ((buffer[treePtr] & mask[currentBit]) != 0) + return nodeValue; + + // else continue traversal + treePtr = nodeValue & 0xFF; } } @@ -80,11 +83,13 @@ public class Huffman extends AbstractFile private void walk (int treePtr, String path, StringBuilder text) { + // check left node if ((buffer[treePtr] & 0x01) == 0) walk (buffer[treePtr + LEFT] & 0xFF, path + "1", text); else print (path + "1", buffer[treePtr + LEFT], text); + // check right node if ((buffer[treePtr] & 0x02) == 0) walk (buffer[treePtr + RIGHT] & 0xFF, path + "0", text); else diff --git a/src/com/bytezone/diskbrowser/wizardry/MessageDataBlock.java b/src/com/bytezone/diskbrowser/wizardry/MessageDataBlock.java index 49b332b..4f024b6 100644 --- a/src/com/bytezone/diskbrowser/wizardry/MessageDataBlock.java +++ b/src/com/bytezone/diskbrowser/wizardry/MessageDataBlock.java @@ -99,7 +99,7 @@ public class MessageDataBlock extends AbstractFile byte[] returnMessage = new byte[message.length]; System.arraycopy (buffer, message.offset, returnMessage, 0, message.length); text.append ( - String.format ("%5d %s%n", message.msgNo, huffman.getMessage (returnMessage))); + String.format ("%5d %s%n", message.msgNo, huffman.decodeMessage (returnMessage))); } if (text.length () > 0)