Skip to content

Instantly share code, notes, and snippets.

@Dysta
Last active September 18, 2019 06:48
Show Gist options
  • Save Dysta/55980054cc18f8ec794b8b0bb01e198d to your computer and use it in GitHub Desktop.
Save Dysta/55980054cc18f8ec794b8b0bb01e198d to your computer and use it in GitHub Desktop.
TD6 Compression/Decompression RLE
package TD6;
public class Compression implements ICompression {
/* encode une chaine de caractère c de longueur L de la facon suivante :
* cFLAGL
* L max = 9
* Si flag est rencontré on encode ça de la facon suiante : FLAG0
*
*/
private char flag;
private char currentChar;
Compression(char flag) {
this.flag = flag;
}
// data, chaine à compresser de type aaaaaaaa ou #aaaa
@Override
public String compress(String data) throws Exception {
StringBuilder compressed_data = new StringBuilder();
char[] array = data.toCharArray();
for(int i = 0; i < array.length; i++ ) {
this.currentChar = array[i];
if ( array[i] == this.flag ) {
compressed_data.append(this.flag + "0");
} else {
int compteur = 0;
do {
compteur++;
} while ( i+compteur < array.length && array[i+compteur] == this.currentChar && compteur < 9 );
if (compteur < 4) {
for(int j = 0; j < compteur; j++ ) {
compressed_data.append(this.currentChar);
}
} else {
compressed_data.append(this.currentChar);
compressed_data.append(this.flag);
compressed_data.append(compteur);
}
i+=compteur-1;
}
}
return compressed_data.toString();
}
// prend une data de type #0a#9a#7
@Override
public String decompress(String data) throws Exception {
StringBuilder decompress_data = new StringBuilder();
char[] array = data.toCharArray();
for (int i = 0; i < array.length; i++) {
if (array[i] == this.flag ) {
if ( array[i+1] == '0' ) {
decompress_data.append(this.flag);
} else
if ( array[i+1] <= '9' && array[i+1] > '0' ) {
for (int j = 1; j < (int) (array[i+1] - '0'); j++) {
decompress_data.append(array[i-1]);
}
}
else
{
throw new Exception("Caractère " + array[i] + " incorecte après le flag " + array[i-1] + "\ndécodé à ce stade : " + decompress_data.toString());
}
i++;
}else {
decompress_data.append(array[i]);
}
}
return decompress_data.toString();
}
}
/**
* @author dysta
*/
package TD7;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class Compression implements ICompression {
private char currentChar;
private char flag;
/**
* Create a compression/decompression methode
* @param flag the flag is used in your string
*/
public Compression(char flag) {
this.flag = flag;
}
@Override
public void compress(String data, Writer out) throws IOException {
StringBuilder compressed_data = new StringBuilder();
char[] array = data.toCharArray();
for(int i = 0; i < array.length; i++ ) {
this.currentChar = array[i];
if ( array[i] == this.flag ) {
compressed_data.append(this.flag + "0");
} else {
int compteur = 0;
do {
compteur++;
} while ( i+compteur < array.length && array[i+compteur] == this.currentChar && compteur < 9 );
if (compteur < 4) {
for(int j = 0; j < compteur; j++ ) {
compressed_data.append(this.currentChar);
}
} else {
compressed_data.append(this.currentChar);
compressed_data.append(this.flag);
compressed_data.append(compteur);
}
i+=compteur-1;
}
}
out.write(compressed_data.toString());
out.flush();
}
@Override
public void uncompress(String data, Writer out) throws IOException {
StringBuilder decompress_data = new StringBuilder();
char[] array = data.toCharArray();
for (int i = 0; i < array.length; i++) {
if (array[i] == this.flag ) {
if ( array[i+1] == '0' ) {
decompress_data.append(this.flag);
} else
if ( array[i+1] <= '9' && array[i+1] > '0' ) {
for (int j = 1; j < (int) (array[i+1] - '0'); j++) {
decompress_data.append(array[i-1]);
}
}
else
{
decompress_data.delete(0, decompress_data.length());
decompress_data.append("Caractère " + array[i] + " incorecte après le flag " + array[i-1] + "\ndécodé à ce stade : " + decompress_data.toString() + "\n");
}
i++;
}else {
decompress_data.append(array[i]);
}
}
out.write(decompress_data.toString());
out.flush();
}
@Override
public void compress(Reader in, Writer out) throws IOException {
StringBuilder str = new StringBuilder();
int c;
while ( (c = in.read() ) != -1 ) {
str.append((char) c);
}
this.compress(str.toString(), out);
}
@Override
public void uncompress(Reader in, Writer out) throws IOException {
StringBuilder str = new StringBuilder();
int c;
while ( (c = in.read() ) != -1 ) {
str.append((char) c);
}
this.compress(str.toString(), out);
}
}
package TD6;
public interface ICompression {
public String compress(String data) throws Exception;
public String decompress(String data) throws Exception;
}
package TD7;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public interface ICompression {
public void compress(String data, Writer out) throws IOException;
public void uncompress(String data, Writer out) throws IOException;
public void compress(Reader in, Writer out) throws IOException;
public void uncompress(Reader in, Writer out) throws IOException;
}
package TD6;
public class RLE {
public static void main(String[] args) {
Compression c = new Compression('#');
try {
String a = c.decompress("a#7#0#0b##6a#8b#7c#9c#0a");
System.out.println(a);
} catch ( Exception e ) {
System.out.println(e.getMessage());
}
}
}
package TD7;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import TD7.Compression;
public class RLE {
public static void main(String[] args) {
Compression c = new Compression('#');
BufferedWriter out1 = null;
BufferedWriter out2 = null;
BufferedWriter out3 = null;
BufferedReader in = null;
try {
out1 = new BufferedWriter(new FileWriter("file.out"));
out2 = new BufferedWriter(new FileWriter("file2.out"));
out3 = new BufferedWriter(new FileWriter("file3.out"));
c.uncompress("a#7#0#0b#6a#8b#7c#9c#0a", out1);
c.compress("###aaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbboooooooooocccccccccccpppaaaaa#####", out2);
in = new BufferedReader(new FileReader("file.out"));
c.compress(in, out3);
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
if ( out1 != null && out2 != null && out3 != null )
try {
out1.close();
out2.close();
out3.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment