-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLZunpack.java
More file actions
66 lines (52 loc) · 2.85 KB
/
Copy pathLZunpack.java
File metadata and controls
66 lines (52 loc) · 2.85 KB
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
// Stefenie Pickston 1506427
// Holly Smallwood 1505405
import java.io.*;
import java.util.*;
import java.lang.Math;
public class LZunpack {
public static DataInputStream dis = new DataInputStream(System.in); //reads bytes from the input river
public static List<Integer> phraseNumberArray = new ArrayList<Integer>(); // array of phrase numbers
public static List<Integer> mismatchArray = new ArrayList<Integer>(); // array of mismatch characters
public static void main(String[] args) {
try {
// dis = new DataInputStream(new FileInputStream("t.txt"));
int k = 1, i, leftover = 0;
// int inputbuff = 0, mask = 255, bigmask = 2147483647;
long inputbuff = 0L, mask = 255L, bigmask = 9223372036854775807L;
byte b = 0;
while (dis.available() > 0) { // while input not empty
byte logOfKO = (byte) Math.ceil(Math.log(k) / Math.log(2)); // calculates log2 of k and rounds up
k++; // increment line
int tempaaa = (int) Math.ceil((8 + (double) logOfKO - (double) leftover)/8);
for(i = 0; i < tempaaa; i++){// calculate how many bytes to read in : 8 + logofK bits -> ceiling
inputbuff = inputbuff << 8; //shift along for the new bytes
if(dis.available() > 0)
b = dis.readByte(); // read in a byte for each buff
long bb = b & mask;
inputbuff = inputbuff ^ bb;
}
int total = 8 * i + leftover; // total amount of bits used in the long
long outputp = (long) (inputbuff >> (total - logOfKO)); // shift over >> by (total - logk) bits
long tempmask1 = (long) bigmask >> (63 - logOfKO);
outputp = outputp & tempmask1;
phraseNumberArray.add((int) outputp);// mask and convert the first logofk amount of bits, and output to array
long outputm = (long) (inputbuff >> (total - logOfKO - 8)); // shift by >> (total - logk - 8) bits
outputm = outputm & mask; // mask and convert the next 8 bits, and output to array - and with 255 after shifting
mismatchArray.add((int) outputm);
// keep the old unused bits and discard used ones
leftover = total - 8 - logOfKO;
long tempmask = bigmask >> (63 - leftover);
inputbuff = inputbuff & tempmask;
}
dis.close();
print(); // print out everything we have collected
} catch (Exception e) {
e.printStackTrace();
}
}
public static void print(){
for(int i = 0; i < phraseNumberArray.size(); i++){
System.out.println(phraseNumberArray.get(i) + " " + mismatchArray.get(i)); // print i of each array
}
}
}