-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDicoComp16.java
More file actions
241 lines (209 loc) · 8.43 KB
/
DicoComp16.java
File metadata and controls
241 lines (209 loc) · 8.43 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.util.*;
import java.io.*;
public class DicoComp16 extends Compression{
private Map<List<Byte>,String> dictionnaire=null;
// une map qui faut correspondre rapidement un mot avec son index
private List<List<Byte>> listeMotsPresents=null;
// la liste ordonnée des mots de la map (uniques)
private List<List<Byte>> texteMotParMot=null;
// la listes des mots dans le fichier, ordonné par apparition (non uniques)
private int nbBit;
// la taille necessaire pour coder l'index
public DicoComp16(String adresseFichierDepart){
_depart=adresseFichierDepart;
}
// compresse le fichier vers adresseFichierFinal
public void compresser(String adresseFichierFinal, boolean verbeux){
long temps=-System.currentTimeMillis();
if(verbeux){
System.out.println("-------- COMPRESSION--------");
System.out.println("De : "+_depart);
System.out.println("Vers : "+adresseFichierFinal);
System.out.println("Methode : Dictionnaire");
}
try{
if(verbeux)System.out.print(">> Dictionnaire... ");
dictionnaire=new HashMap<List<Byte>,String>();
listeMotsPresents=new ArrayList<List<Byte>>();
texteMotParMot=new ArrayList<List<Byte>>();
DataInputStream fluxLecture=new DataInputStream(new BufferedInputStream(new FileInputStream(_depart)));
// le Set sert à obtenir rapidement tous les mots (plus rapide que le .put des maps) :
Set<List<Byte>> SetMotsPresents=new HashSet<List<Byte>>();
// puis on separe les mots en mettant à jour SetMotsPresents et texteMotParMot :
List<Byte> bytesDepuisEspace=new ArrayList<Byte>();
byte byteLu=0;
while((byteLu=(byte)fluxLecture.read())!=-1){
if(byteLu==' '){
SetMotsPresents.add(bytesDepuisEspace);
texteMotParMot.add(bytesDepuisEspace);
bytesDepuisEspace=new ArrayList<Byte>();
}
else{
bytesDepuisEspace.add(byteLu);
}
}
// pour le dernier mot :
SetMotsPresents.add(bytesDepuisEspace);
texteMotParMot.add(bytesDepuisEspace);
fluxLecture.close();
if(verbeux)System.out.println("OK ("+SetMotsPresents.size()+")");
// on calcule le nombre de bit pour coder les index :
if(verbeux)System.out.print(">> Poid des index... ");
nbBit=1;
while(SetMotsPresents.size()>Math.pow(2, nbBit))nbBit++;
if(verbeux)System.out.println("OK ("+nbBit+")");
// on ordonne les mots de Set avec listeMotsPresents et on met à jour le dictionnaire
if(verbeux)System.out.print(">> Indexation... ");
listeMotsPresents=new ArrayList<List<Byte>>(SetMotsPresents);
int i=0;
for(List<Byte> l:listeMotsPresents){
dictionnaire.put(l,int2bin(i,nbBit));// int2bin renvoie une String de '0' et de '1' correspondant au texte
i++;
}
if(verbeux)System.out.println("OK");
if(verbeux)System.out.print(">> Encodage... ");
DataOutputStream fluxEcriture=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(adresseFichierFinal)));
// on ajoute au debut du fichier :
fluxEcriture.writeInt(listeMotsPresents.size());// le nombre de mots du dictionnaire
fluxEcriture.writeInt(nbBit);// la taille des index
fluxEcriture.writeInt(texteMotParMot.size());// le nombre de mots du texte
// on ecrit la taille de chaque mot sur 16 bit
// (pas de séparateur entre les mots dans le fichier compressé)
for(List<Byte> mot:listeMotsPresents)
fluxEcriture.writeShort(mot.size());
// puis on écrit tous les mots du dictionnaire
byte[][] dictionnaireByteArray=new byte[listeMotsPresents.size()][];
for(int j=0;j<listeMotsPresents.size();j++){
dictionnaireByteArray[j]=new byte[listeMotsPresents.get(j).size()];
// dans certains cas, on obtient une erreur si on ecrit tous les bits à la fois :
for(int k=0;k<listeMotsPresents.get(j).size();k++){
dictionnaireByteArray[j][k]=listeMotsPresents.get(j).get(k);
fluxEcriture.writeByte(dictionnaireByteArray[j][k]);
}
}
//on ecrit les index dans une chaîne :
StringBuffer chaîneAEcrire=new StringBuffer();
for(List<Byte> l:texteMotParMot){
chaîneAEcrire.append(dictionnaire.get(l));
}
// on separe la chaîne en strings de '0' et de '1' de taille 8 et on ecrit le nombre correspondant
byte[] donneesI=new byte[(texteMotParMot.size()*nbBit)/8+1];
for(int j=0;j<donneesI.length-1;j++){
donneesI[j]=string2Byte(chaîneAEcrire.substring(j*8, j*8+8));
}
// le dernier caractère :
donneesI[donneesI.length-1]=string2Byte(chaîneAEcrire.substring(donneesI.length*8-8));
for(byte b:donneesI)
fluxEcriture.writeByte(b);
fluxEcriture.close();
System.out.println("OK");
temps+=System.currentTimeMillis();
System.out.println("Fin de la compression en "+temps+" ms");
CSP.tauxComp(_depart, adresseFichierFinal);
}
catch(IOException e){
System.out.println(e);
}
}
public void decompresser(String adresseFichierFinal, boolean verbeux){
long temps=-System.currentTimeMillis();
if(verbeux){
System.out.println("------- DECOMPRESSION-------");
System.out.println("De : "+_depart);
System.out.println("Vers : "+adresseFichierFinal);
System.out.println("Methode : Dictionnaire");
}
try{
dictionnaire=new HashMap<List<Byte>,String>();
listeMotsPresents=new ArrayList<List<Byte>>();
texteMotParMot=new ArrayList<List<Byte>>();
DataInputStream fluxLecture=new DataInputStream(new BufferedInputStream(new FileInputStream(_depart)));
// on lit la taille du dictionnaire, des index et du fichier :
if(verbeux)System.out.print(">> Taille dico... ");
int taille=fluxLecture.readInt();
if(verbeux)System.out.println("OK ("+taille+")");
if(verbeux)System.out.print(">> Poid des index... ");
nbBit=fluxLecture.readInt();
if(verbeux)System.out.println("OK ("+nbBit+")");
int nbMots=fluxLecture.readInt();
// on lit la taille des mots du dictionnaire :
if(verbeux)System.out.print(">> Lecture des tailles... ");
int[] tailles=new int[taille];
for(int i=0;i<taille;i++){
tailles[i]=fluxLecture.readShort();
}
if(verbeux)System.out.println("OK");
// on lit le dictionnaire
if(verbeux)System.out.print(">> Lecture du dico... ");
for(int i=0;i<taille;i++){
List<Byte> mot=new ArrayList<Byte>();
for(int j=0;j<tailles[i];j++){
mot.add(fluxLecture.readByte());
}
listeMotsPresents.add(mot);
}
if(verbeux)System.out.println("OK");
if(verbeux)System.out.print(">> Encodage... ");
// on lit tous les index, en les ajoutant sous forme de chaîne de '0' et de '1' à chaîneAEcrire :
StringBuffer chaîneAEcrire=new StringBuffer();
try{// attendre EOF avec try est plus rapide qu'une boucle
while(true){
byte by=fluxLecture.readByte();
chaîneAEcrire.append(int2bin(by,8).substring(int2bin(by,8).length()-8));
}
}
catch(EOFException e){
}
// on enlève les bits en trop :
int bitEnTrop=chaîneAEcrire.length()-nbMots*nbBit;
if(bitEnTrop!=0){
chaîneAEcrire.delete(chaîneAEcrire.length()-8+1, 1+chaîneAEcrire.length()-8+bitEnTrop);
}
fluxLecture.close();
if(verbeux)System.out.println("OK");
if(verbeux)System.out.print(">> Ecriture... ");
DataOutputStream fluxEcriture=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(adresseFichierFinal)));
// on ecrit chaque mot :
boolean premierPassage=true;
int i=0;
try{
while(true){
if(premierPassage){
// le premier mot ne commence pas par un espace si il est n'est pas "" :
premierPassage=false;
}
else{
fluxEcriture.writeByte(' ');
}
// on decode le numeros d'index en base 10 :
int index=Integer.parseInt(chaîneAEcrire.substring(i*nbBit,(i+1)*nbBit),2);
// on recupère le mot et on l'écrit :
List<Byte> mot=listeMotsPresents.get(index);
for(byte b:mot)
fluxEcriture.writeByte(b);
i++;
}
}
catch(StringIndexOutOfBoundsException e){
// fin de la boucle cassique
}
fluxEcriture.close();
if(verbeux)System.out.println("OK");
temps+=System.currentTimeMillis();
System.out.println("Fin de la décompression en "+temps+" ms");
}
catch(IOException e){
System.out.println(e);
}
}
public void raz(){
_depart=null;
dictionnaire=null;
listeMotsPresents=null;
texteMotParMot=null;
}
public static void main(String[] args){
DicoComp16 dc16=new DicoComp16("test/k.html");
dc16.testerToutTexte(true);
}
}