-
Notifications
You must be signed in to change notification settings - Fork 0
/
Read.java
119 lines (101 loc) · 2.56 KB
/
Read.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
//Kaushil Ruparelia CS610 9169 prp
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author kaushilruparelia
*
*/
public class Read {
private static BufferedInputStream inputStream;
private static int buffer;
private static int lengthOfBuffer;
public Read() {
inputStream = new BufferedInputStream(System.in);
}
public Read(String filename) {
try {
inputStream = new BufferedInputStream(new FileInputStream(filename));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public boolean readBit() throws Exception {
if (lengthOfBuffer == 0) {
readBuffer();
} else if (lengthOfBuffer == -1) {
throw new Exception("File read ended....");
}
lengthOfBuffer--;
return ((buffer >> lengthOfBuffer) & 1) == 1;
}
public void readBuffer() {
try {
buffer = inputStream.read();
if(buffer == -1) {
return;
}
lengthOfBuffer = 8;
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
lengthOfBuffer = -1;
buffer = -1;
}
}
public char readChar() throws Exception {
return (char)(readByte() & 0xFF);
}
public int readInt() throws Exception {
//Read Byte by Byte
int number = 0;
for (int i = 0; i < 4; i++) {
number<<=8;
number |= (readByte() & 0xFF);
}
return number;
}
public char readByte() throws Exception {
if (lengthOfBuffer == 0) {
readBuffer();
int myByte = (buffer & 0xFF);
readBuffer();
return (char)(myByte & 0xFF);
} else if (lengthOfBuffer == -1) {
throw new Exception("File read ended....");
} else if (lengthOfBuffer == 8) {
//Lucky to have the entire character
//Do a bitwise AND to make sure no adulteration remain
int myByte = buffer;
readBuffer();
return (char)(myByte & 0xFF);
}
else {
int oldLength = lengthOfBuffer;
int bits = buffer;
//Shift the bits to the right
bits <<= (8-oldLength);
readBuffer();
if (lengthOfBuffer == -1 || buffer == -1) {
throw new Exception("File read ended....");
}
//Append the new bits
lengthOfBuffer = oldLength;
bits |= buffer >>> lengthOfBuffer;
//Do a bitwise AND to make sure no adulteration remain
return (char)(bits & 0xFF);
}
}
public String readString() throws Exception {
StringBuilder sb = new StringBuilder();
do {
sb.append(readChar());
}while (buffer != -1);
return sb.toString();
}
public void close() throws IOException {
inputStream.close();
}
}