-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSourceBuffer.java
175 lines (160 loc) · 5.72 KB
/
SourceBuffer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// BXLDecoder.java - a utility for converting Huffman encoded files
// into text, ported to Java from the vala code by Geert Jordaens
//
// SourceBuffer.java - an object used to cache and convert
// a Huffman encoded file
//
// BXLDecoder.java v1.0
// SourceBuffer.java v1.0
// Copyright (C) 2016 Erich S. Heinzle, a1039181@gmail.com
// see LICENSE-gpl-v2.txt for software license
// see README.txt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// BXLDecoder Copyright (C) 2016 Erich S. Heinzle a1039181@gmail.com
import java.io.*;
import java.util.Scanner;
import java.lang.StringBuffer;
public class SourceBuffer {
private char[] source_buffer = null;
private int source_index = 4;
private int source_char = 0;
private int bit = 0;
public SourceBuffer(String filename) {
FileInputStream input = null;
char [] ret_buffer = null;
try {
input = new FileInputStream(filename);
int c;
char [] buffer = new char[1000];
int bufferIndex = 0;
// System.out.println("about to read bytes from file");
while ((c = input.read()) != -1) {
if (bufferIndex == buffer.length) {
char [] newBuffer = new char[buffer.length*2];
for (int index = 0; index < buffer.length; index++) {
newBuffer[index] = buffer[index];
}
buffer = newBuffer;
}
buffer[bufferIndex] = (char)c;
bufferIndex++;
}
ret_buffer = new char[bufferIndex];
for (int index = 0; index < bufferIndex; index++) {
ret_buffer[index] = buffer[index];
}
//System.out.println("finished reading bytes from file");
// is_filled = true; // hack, should check
input.close();
} catch (Exception e) {
System.out.println ("Exception: " + e);
}
source_buffer = ret_buffer;
}
public int read_next_bit() {
int result = 0;
if (bit < 0) {
// Fetch next byte from source_buffer
bit = 7;
// System.out.println("About to get byte number " +
// source_index + " from source buffer");
source_char = (int)source_buffer[source_index];
result = source_char & (1 << bit);
source_index++;
} else {
result = source_char & (1 << bit);
}
bit--;
// System.out.println("bit now: " + bit);
// System.out.println("source_index now: " + source_index);
return result;
}
public int uncompressed_size() {
/* Uncompressed size =
B0b7 * 1<<0 + B0b6 * 1<<1 + ... + B0b0 * 1<<7 +
B1b7 * 1<<0 + B1b6 * 1<<1 + ... + B2b0 * 1<<7 +
B2b7 * 1<<0 + B2b6 * 1<<1 + ... + B3b0 * 1<<7 +
B3b7 * 1<<0 + B3b6 * 1<<1 + ... + B4b0 * 1<<7
*/
int size = 0;
int mask = 0;
for (int i = 7 ; i >=0 ; i--) {
if ((source_buffer[0] & (1 << i)) != 0) {
size |= (1 << mask);
}
mask++;
}
for (int i = 7 ; i >=0 ; i--) {
if ((source_buffer[1] & (1 << i)) != 0) {
size |= (1<<mask);
}
mask++;
}
for (int i = 7 ; i >=0 ; i--) {
if ((source_buffer[2] & (1 << i)) != 0) {
size |= (1<<mask);
}
mask++;
}
for (int i = 7 ; i >=0 ; i--) {
if ((source_buffer[3] & (1 << i)) != 0) {
size |= (1<<mask);
}
mask++;
}
return size;
}
public String decode() {
NodeTree tree = new NodeTree();
int out_file_length = uncompressed_size();
//String sb = "";
// immutable Strings replaced with more efficient string handling, suggested by wlbaker:
StringBuffer sb = new StringBuffer(out_file_length);
// System.out.println("About to enter decoding while loop...");
while (source_index < source_buffer.length && sb.length() != out_file_length) {
//System.out.println("Have entered decoding while loop...");
Node node = tree.getRoot();
// System.out.println("About to enter leaf finding while loop...");
while (!node.is_leaf()) {
// find leaf node
// System.out.println("now searching for leaf node...");
if (read_next_bit() != 0) {
// if (read_next_bit(source_index, source_char, bit, source_buffer) != 0) {
node = node.left;
// System.out.println("Picking left node, source bit != 0.");
} else {
node = node.right;
// System.out.println("Picking right node, source bit == 0.");
}
}
// System.out.println("Node symbol: " + (char)(node.symbol));
// System.out.println("Node symbol as toString: " + node);
sb.append((char)node.symbol); // more efficient string building, thanks wlbaker
//sb = sb + (char)(node.symbol);
// sb = sb + node;
// sb = sb + ((char)(node.symbol & 0xff));
// node.weight += 1;
node.incrementWeight();
// System.out.println("decoded text so far is: " +
// sb + ", now to update tree...");
tree.update_tree(node);
}
//source_buffer = null; // not needed for standalone utility
//is_filled = false;
return sb.toString();
}
}