Adding support for multiple compression/decompression methods

master
Nelson R. Perez 2016-12-02 14:17:26 -05:00
parent f0d0037355
commit a6ae9e6773
4 changed files with 38 additions and 21 deletions

View File

@ -57,7 +57,7 @@ public abstract class FileBin {
System.arraycopy(rawData, 4, compressedData, 0, compressedData.length); System.arraycopy(rawData, 4, compressedData, 0, compressedData.length);
System.out.println("Despues:"+byteToString(compressedData)); System.out.println("Despues:"+byteToString(compressedData));
byte[] wallet_object_bytes = Util.decompress(compressedData); byte[] wallet_object_bytes = Util.decompress(compressedData, Util.XZ);
String wallet_string = new String(wallet_object_bytes, "UTF-8"); String wallet_string = new String(wallet_object_bytes, "UTF-8");
JsonObject wallet = new JsonParser().parse(wallet_string).getAsJsonObject(); JsonObject wallet = new JsonParser().parse(wallet_string).getAsJsonObject();
if (wallet.get("wallet").isJsonArray()) { if (wallet.get("wallet").isJsonArray()) {
@ -120,7 +120,7 @@ public abstract class FileBin {
jsonAccountName.add("name", new JsonParser().parse(accountName)); jsonAccountName.add("name", new JsonParser().parse(accountName));
accountNames.add(jsonAccountName); accountNames.add(jsonAccountName);
wallet_object.add("linked_accounts", accountNames); wallet_object.add("linked_accounts", accountNames);
byte[] compressedData = Util.compress(wallet_object.toString().getBytes("UTF-8")); byte[] compressedData = Util.compress(wallet_object.toString().getBytes("UTF-8"), Util.XZ);
System.out.println("Antes:"+byteToString(compressedData)); System.out.println("Antes:"+byteToString(compressedData));
MessageDigest md = MessageDigest.getInstance("SHA-256"); MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] checksum = md.digest(compressedData); byte[] checksum = md.digest(compressedData);

View File

@ -53,11 +53,11 @@ public class Invoice implements JsonSerializable {
public static String toQrCode(Invoice invoice){ public static String toQrCode(Invoice invoice){
String json = invoice.toJsonString(); String json = invoice.toJsonString();
return Base58.encode(Util.compress(json.getBytes())); return Base58.encode(Util.compress(json.getBytes(), Util.LZMA));
} }
public static Invoice fromQrCode(String encoded){ public static Invoice fromQrCode(String encoded){
String json = new String(Util.decompress(Base58.decode(encoded))); String json = new String(Util.decompress(Base58.decode(encoded), Util.LZMA));
Gson gson = new Gson(); Gson gson = new Gson();
return gson.fromJson(json, Invoice.class); return gson.fromJson(json, Invoice.class);
} }

View File

@ -720,7 +720,7 @@ public class Test {
String test = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; String test = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
System.out.println("to compress"); System.out.println("to compress");
System.out.println(Util.bytesToHex(test.getBytes())); System.out.println(Util.bytesToHex(test.getBytes()));
byte[] compressed = Util.compress(test.getBytes()); byte[] compressed = Util.compress(test.getBytes(), Util.XZ);
System.out.println("compressed"); System.out.println("compressed");
System.out.println(Util.bytesToHex(compressed)); System.out.println(Util.bytesToHex(compressed));
} }

View File

@ -1,19 +1,27 @@
package com.luminiasoft.bitshares; package com.luminiasoft.bitshares;
import java.io.ByteArrayInputStream; import org.tukaani.xz.FinishableOutputStream;
import org.tukaani.xz.LZMA2Options; import org.tukaani.xz.LZMA2Options;
import java.io.ByteArrayOutputStream; import org.tukaani.xz.LZMAInputStream;
import java.io.IOException; import org.tukaani.xz.LZMAOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.tukaani.xz.XZInputStream; import org.tukaani.xz.XZInputStream;
import org.tukaani.xz.XZOutputStream; import org.tukaani.xz.XZOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* Class used to encapsulate common utility methods * Class used to encapsulate common utility methods
*/ */
public class Util { public class Util {
final private static char[] hexArray = "0123456789abcdef".toCharArray(); public static final String TAG = "Util";
private static final char[] hexArray = "0123456789abcdef".toCharArray();
public static final int LZMA = 0;
public static final int XZ = 1;
public static byte[] hexToBytes(String s) { public static byte[] hexToBytes(String s) {
int len = s.length(); int len = s.length();
@ -38,16 +46,21 @@ public class Util {
/** /**
* Utility function that compresses data using the LZMA algorithm. * Utility function that compresses data using the LZMA algorithm.
* @param inputBytes Input bytes of the data to be compressed. * @param inputBytes Input bytes of the data to be compressed.
* @param which Which subclass of the FinishableOutputStream to use.
* @return Compressed data * @return Compressed data
* @author Henry Varona * @author Henry Varona
*/ */
public static byte[] compress(byte[] inputBytes) { public static byte[] compress(byte[] inputBytes, int which) {
XZOutputStream out = null; FinishableOutputStream out = null;
try { try {
ByteArrayInputStream input = new ByteArrayInputStream(inputBytes); ByteArrayInputStream input = new ByteArrayInputStream(inputBytes);
ByteArrayOutputStream output = new ByteArrayOutputStream(2048); ByteArrayOutputStream output = new ByteArrayOutputStream(2048);
LZMA2Options options = new LZMA2Options(); LZMA2Options options = new LZMA2Options();
out = new XZOutputStream(output, options); if(which == Util.LZMA) {
out = new LZMAOutputStream(output, options, -1);
}else if(which == Util.XZ){
out = new XZOutputStream(output, options);
}
byte[] buf = new byte[inputBytes.length]; byte[] buf = new byte[inputBytes.length];
int size; int size;
while ((size = input.read(buf)) != -1) { while ((size = input.read(buf)) != -1) {
@ -69,17 +82,22 @@ public class Util {
/** /**
* Utility function that decompresses data that has been compressed using the LZMA algorithm * Utility function that decompresses data that has been compressed using the LZMA algorithm
* by the {@link Util#compress(byte[])} method. * by the {@link Util#compress(byte[], int)} method.
* @param inputBytes Compressed data * @param inputBytes Compressed data.
* @param which Which subclass if InputStream to use.
* @return Uncompressed data * @return Uncompressed data
* @author Henry Varona * @author Henry Varona
*/ */
public static byte[] decompress(byte[] inputBytes) { public static byte[] decompress(byte[] inputBytes, int which) {
XZInputStream in = null; InputStream in = null;
try { try {
ByteArrayInputStream input = new ByteArrayInputStream(inputBytes); ByteArrayInputStream input = new ByteArrayInputStream(inputBytes);
ByteArrayOutputStream output = new ByteArrayOutputStream(2048); ByteArrayOutputStream output = new ByteArrayOutputStream(2048);
in = new XZInputStream(input); if(which == XZ) {
in = new XZInputStream(input);
}else if(which == LZMA){
in = new LZMAInputStream(input);
}
int size; int size;
while ((size = in.read()) != -1) { while ((size = in.read()) != -1) {
output.write(size); output.write(size);
@ -92,8 +110,7 @@ public class Util {
try { try {
in.close(); in.close();
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); }
}
} }
return null; return null;
} }