-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMTF.java
More file actions
105 lines (94 loc) · 3.03 KB
/
MTF.java
File metadata and controls
105 lines (94 loc) · 3.03 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.*;
import java.io.*;
public class MTF extends Compression{
// la table mesure la distance par rapport au caractère actuel
private List<Byte> tableDistance;
public MTF(String ad){
_depart=ad;
tableDistance=new ArrayList<Byte>();
for(int b=Byte.MIN_VALUE;b<=Byte.MAX_VALUE;b++)
tableDistance.add((byte)b);
}
public void compresser(String fin, boolean v){
if(v){
System.out.println("-------- COMPRESSION--------");
System.out.println("De : "+_depart);
System.out.println("Vers : "+fin);
System.out.println("Methode : MTF");
}
long tps=-System.currentTimeMillis();
try{
if(v)System.out.print(">> Compression... ");
BufferedInputStream fluxLecture =new BufferedInputStream(new FileInputStream(_depart));
DataOutputStream fluxEcriture=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fin)));
// on code :
int intLu=0;
while((intLu=fluxLecture.read())!=-1){
byte byteLu=(byte)intLu;
// on récupére l'index de l'octet dans la table
int index=tableDistance.indexOf(byteLu);
// on met à jour le tableau des index
tableDistance.remove((int)index);
tableDistance.add(0, byteLu);
fluxEcriture.writeByte(index+Byte.MIN_VALUE);
}
fluxEcriture.close();
fluxLecture.close();
if(v)System.out.println("OK");
tps+=System.currentTimeMillis();
System.out.println("Fin de la compression en "+tps+" ms");
CSP.tauxComp(_depart, fin);
}
catch(IOException e){
System.out.println(e);
}
}
public void decompresser(String fin, boolean v){
if(v){
System.out.println("------- DECOMPRESSION-------");
System.out.println("De : "+_depart);
System.out.println("Vers : "+fin);
System.out.println("Methode : MTF");
}
long tps=-System.currentTimeMillis();
try{
// on fait l'exact opposé de la compression
if(v)System.out.print(">> Décompression... ");
DataInputStream fluxLecture=new DataInputStream(new BufferedInputStream(new FileInputStream(_depart)));
BufferedOutputStream fluxEcriture=new BufferedOutputStream(new FileOutputStream(fin));
try{
while(true){
int index=fluxLecture.readByte()-Byte.MIN_VALUE;
byte byteLu=tableDistance.get(index);
tableDistance.remove(index);
tableDistance.add(0, byteLu);
fluxEcriture.write(byteLu);
}
}
catch(EOFException e){
}
fluxEcriture.close();
fluxLecture.close();
if(v)System.out.println("OK");
tps+=System.currentTimeMillis();
System.out.println("Fin de la décompression en "+tps+" ms");
}
catch(IOException e){
System.out.println(e);
}
}
public double estimationComp(){
System.out.println("Pas de changement de taille notable");
return 1.;
}
public void raz(){
_depart=null;
tableDistance=new ArrayList<Byte>();
for(int b=Byte.MIN_VALUE;b<=Byte.MAX_VALUE;b++)
tableDistance.add((byte)b);
}
public static void main(String[] args){
MTF b=new MTF("");
b.testerTout(true);
}
}