mirror of
https://github.com/dmolony/DiskBrowser.git
synced 2024-11-25 16:34:00 +00:00
use original buffer
This commit is contained in:
parent
816548386e
commit
ee94f383ea
@ -6,28 +6,19 @@ import com.bytezone.diskbrowser.applefile.AbstractFile;
|
|||||||
|
|
||||||
public class Huffman extends AbstractFile
|
public class Huffman extends AbstractFile
|
||||||
{
|
{
|
||||||
private final byte[] tree;
|
private static final int LEFT = 256;
|
||||||
private final byte[] left;
|
private static final int RIGHT = 512;
|
||||||
private final byte[] right;
|
|
||||||
|
|
||||||
private int bitNo;
|
private int bitNo;
|
||||||
private int msgPtr;
|
private int msgPtr;
|
||||||
private int currentByte;
|
private int currentByte;
|
||||||
private byte[] message;
|
private byte[] message;
|
||||||
|
|
||||||
private String treeContents;
|
private String bufferContents;
|
||||||
|
|
||||||
public Huffman (String name, byte[] buffer)
|
public Huffman (String name, byte[] buffer)
|
||||||
{
|
{
|
||||||
super (name, buffer);
|
super (name, buffer);
|
||||||
|
|
||||||
tree = new byte[256];
|
|
||||||
left = new byte[256];
|
|
||||||
right = new byte[256];
|
|
||||||
|
|
||||||
System.arraycopy (buffer, 0, tree, 0, 256);
|
|
||||||
System.arraycopy (buffer, 256, left, 0, 256);
|
|
||||||
System.arraycopy (buffer, 512, right, 0, 256);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMessage (byte[] message)
|
public String getMessage (byte[] message)
|
||||||
@ -47,7 +38,7 @@ public class Huffman extends AbstractFile
|
|||||||
|
|
||||||
private byte getChar ()
|
private byte getChar ()
|
||||||
{
|
{
|
||||||
int treePtr = 0; // start at the root
|
int treePtr = 0; // start at the root
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -57,20 +48,20 @@ public class Huffman extends AbstractFile
|
|||||||
currentByte = message[msgPtr++] & 0xFF;
|
currentByte = message[msgPtr++] & 0xFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
int currentBit = currentByte % 2; // get the next bit to process
|
int currentBit = currentByte % 2; // get the next bit to process
|
||||||
currentByte /= 2;
|
currentByte /= 2;
|
||||||
|
|
||||||
if (currentBit == 0) // take right path
|
if (currentBit == 0) // take right path
|
||||||
{
|
{
|
||||||
if ((tree[treePtr] & 0x02) != 0) // if has right leaf...
|
if ((buffer[treePtr] & 0x02) != 0) // if has right leaf...
|
||||||
return right[treePtr]; // return that character
|
return buffer[treePtr + RIGHT]; // return that character
|
||||||
treePtr = right[treePtr] & 0xFF; // else go to right node
|
treePtr = buffer[treePtr + RIGHT] & 0xFF; // else traverse right node
|
||||||
}
|
}
|
||||||
else // take left path
|
else // take left path
|
||||||
{
|
{
|
||||||
if ((tree[treePtr] & 0x01) != 0) // if has left leaf...
|
if ((buffer[treePtr] & 0x01) != 0) // if has left leaf...
|
||||||
return left[treePtr]; // return that character
|
return buffer[treePtr + LEFT]; // return that character
|
||||||
treePtr = left[treePtr] & 0xFF; // else go to left node
|
treePtr = buffer[treePtr + LEFT] & 0xFF; // else traverse left node
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,26 +69,26 @@ public class Huffman extends AbstractFile
|
|||||||
@Override
|
@Override
|
||||||
public String getText ()
|
public String getText ()
|
||||||
{
|
{
|
||||||
if (treeContents == null)
|
if (bufferContents == null)
|
||||||
{
|
{
|
||||||
StringBuilder text = new StringBuilder ();
|
StringBuilder text = new StringBuilder ();
|
||||||
walk (0, "", text);
|
walk (0, "", text);
|
||||||
treeContents = text.toString ();
|
bufferContents = text.toString ();
|
||||||
}
|
}
|
||||||
return treeContents;
|
return bufferContents;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void walk (int treePtr, String path, StringBuilder text)
|
private void walk (int treePtr, String path, StringBuilder text)
|
||||||
{
|
{
|
||||||
if ((tree[treePtr] & 0x01) == 0)
|
if ((buffer[treePtr] & 0x01) == 0)
|
||||||
walk (left[treePtr] & 0xFF, path + "1", text);
|
walk (buffer[treePtr + LEFT] & 0xFF, path + "1", text);
|
||||||
else
|
else
|
||||||
print (path + "1", left[treePtr], text);
|
print (path + "1", buffer[treePtr + LEFT], text);
|
||||||
|
|
||||||
if ((tree[treePtr] & 0x02) == 0)
|
if ((buffer[treePtr] & 0x02) == 0)
|
||||||
walk (right[treePtr] & 0xFF, path + "0", text);
|
walk (buffer[treePtr + RIGHT] & 0xFF, path + "0", text);
|
||||||
else
|
else
|
||||||
print (path + "0", right[treePtr], text);
|
print (path + "0", buffer[treePtr + RIGHT], text);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void print (String path, byte value, StringBuilder text)
|
private void print (String path, byte value, StringBuilder text)
|
||||||
|
Loading…
Reference in New Issue
Block a user