Created
August 14, 2012 16:23
-
-
Save jlitewski/3350606 to your computer and use it in GitHub Desktop.
Dirty byte compression based on grouping like numbers together
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.io.File; | |
import java.io.DataOutputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.FileOutputStream; | |
import java.util.zip.GZIPOutputStream; | |
public class TestCodeDirtyCompression { | |
public static void main(String[] args) { | |
long start = System.currentTimeMillis(); | |
int[][][] array = new int[4][8][4]; | |
int result; | |
File file = new File("test.chunk"); | |
Random rng = new Random(); | |
try { | |
DataOutputStream stream = new DataOutputStream(new BufferedOutputStream( | |
new GZIPOutputStream(new FileOutputStream(file)), 2056)); | |
for(int c = 0; c < 8; c++) { //y | |
for(int b = 0; b < 4; b++) { //z | |
for(int a = 0; a < 4; a++) { //x | |
if(rng.nextBoolean()) result = 1; else result = 0; | |
//System.out.println(result); | |
array[a][c][b] = result; | |
} | |
} | |
} | |
int last = -1; | |
int clump = 0; | |
for(int c = 0; c < 8; c++) { //y | |
for(int b = 0; b < 4; b++) { //z | |
for(int a = 0; a < 4; a++) { //x | |
if(last == -1) { | |
clump++; | |
last = array[a][c][b]; | |
continue; | |
} | |
if(last == array[a][c][b]) { | |
clump++; | |
continue; | |
} else if(clump > 1) { | |
stream.writeInt(-1); | |
stream.writeInt(last); | |
stream.writeInt(clump); | |
clump = 1; | |
last = array[a][c][b]; | |
continue; | |
} else if(clump == 1) { | |
stream.writeInt(last); | |
last = array[a][c][b]; | |
} | |
} | |
} | |
} | |
if(clump != 0 && clump > 1) { | |
stream.writeInt(-1); | |
stream.writeInt(last); | |
stream.writeInt(clump); | |
} else if(clump == 1) { | |
stream.writeInt(last); | |
} | |
stream.flush(); | |
stream.close(); | |
long end = System.currentTimeMillis(); | |
System.out.println("Done in "+(end-start)+" ms"); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment