This commit is contained in:
Denis Molony 2016-08-17 21:33:27 +10:00
parent ee94f383ea
commit 5c3f6a0753
2 changed files with 19 additions and 14 deletions

View File

@ -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

View File

@ -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)