-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractor.java
More file actions
84 lines (72 loc) · 1.97 KB
/
Copy pathExtractor.java
File metadata and controls
84 lines (72 loc) · 1.97 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package compresseur;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
public class Extractor {
public static void main(String[] args) throws IOException {
if(args.length != 1) {
System.out.println("Usage: extractor <file to extract>");
}
Reader reader = new Reader(args[0]);
Vector<Integer> vector = reader.read();
if(vector.size() == 0) {
throw new IOException("Unexploitable file");
}
try
{
// Creation of the result file
StringBuffer buffer = new StringBuffer(args[0]);
buffer.replace(buffer.lastIndexOf("."),buffer.length(),".seg");
FileWriter fileWriter = new FileWriter (buffer.toString());
int nbOfPixels;
int sizeOfPixel;
int index = 0;
int character;;
while (index != vector.size())
{
nbOfPixels = 0;
sizeOfPixel = 0;
// Acquiring the number of pixels in the segment
try{
nbOfPixels = Reader.readAByte(vector, index);
index += 8;
}
catch(IOException exception){
throw new IOException("Uncomplete header readen");
}
// Acquiring the size of each pixel of the segment
for(int i = 0; i < 3; i++) {
if(index == vector.size())
{
throw new IOException("Uncomplete header readen");
}
character = vector.get(index);
sizeOfPixel = sizeOfPixel * 2 + Character.getNumericValue(character);
index++;
}
// Reading the segment
for(int i = 0; i < nbOfPixels; i++) {
// Writing missing bits of the segment
for(int j = 0; j < 8 - sizeOfPixel; j++) {
fileWriter.write("0");
}
// Copying current bit
for(int j=0; j < sizeOfPixel; j++) {
if(index == vector.size())
{
throw new IOException("Uncomplete data readen");
}
character = vector.get(index);
fileWriter.write(character);
index++;
}
}
}
fileWriter.close();
}
catch (IOException exception)
{
System.out.println ("Error while reading file : " + exception.getMessage());
}
}
}