Adding support for multiple compression/decompression methods
This commit is contained in:
parent
f0d0037355
commit
a6ae9e6773
4 changed files with 38 additions and 21 deletions
|
@ -57,7 +57,7 @@ public abstract class FileBin {
|
|||
System.arraycopy(rawData, 4, compressedData, 0, compressedData.length);
|
||||
|
||||
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");
|
||||
JsonObject wallet = new JsonParser().parse(wallet_string).getAsJsonObject();
|
||||
if (wallet.get("wallet").isJsonArray()) {
|
||||
|
@ -120,7 +120,7 @@ public abstract class FileBin {
|
|||
jsonAccountName.add("name", new JsonParser().parse(accountName));
|
||||
accountNames.add(jsonAccountName);
|
||||
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));
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
byte[] checksum = md.digest(compressedData);
|
||||
|
|
|
@ -53,11 +53,11 @@ public class Invoice implements JsonSerializable {
|
|||
|
||||
public static String toQrCode(Invoice invoice){
|
||||
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){
|
||||
String json = new String(Util.decompress(Base58.decode(encoded)));
|
||||
String json = new String(Util.decompress(Base58.decode(encoded), Util.LZMA));
|
||||
Gson gson = new Gson();
|
||||
return gson.fromJson(json, Invoice.class);
|
||||
}
|
||||
|
|
|
@ -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.";
|
||||
System.out.println("to compress");
|
||||
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(Util.bytesToHex(compressed));
|
||||
}
|
||||
|
|
|
@ -1,19 +1,27 @@
|
|||
package com.luminiasoft.bitshares;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import org.tukaani.xz.FinishableOutputStream;
|
||||
import org.tukaani.xz.LZMA2Options;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.tukaani.xz.LZMAInputStream;
|
||||
import org.tukaani.xz.LZMAOutputStream;
|
||||
import org.tukaani.xz.XZInputStream;
|
||||
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
|
||||
*/
|
||||
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) {
|
||||
int len = s.length();
|
||||
|
@ -38,16 +46,21 @@ public class Util {
|
|||
/**
|
||||
* Utility function that compresses data using the LZMA algorithm.
|
||||
* @param inputBytes Input bytes of the data to be compressed.
|
||||
* @param which Which subclass of the FinishableOutputStream to use.
|
||||
* @return Compressed data
|
||||
* @author Henry Varona
|
||||
*/
|
||||
public static byte[] compress(byte[] inputBytes) {
|
||||
XZOutputStream out = null;
|
||||
public static byte[] compress(byte[] inputBytes, int which) {
|
||||
FinishableOutputStream out = null;
|
||||
try {
|
||||
ByteArrayInputStream input = new ByteArrayInputStream(inputBytes);
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream(2048);
|
||||
LZMA2Options options = new LZMA2Options();
|
||||
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];
|
||||
int size;
|
||||
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
|
||||
* by the {@link Util#compress(byte[])} method.
|
||||
* @param inputBytes Compressed data
|
||||
* by the {@link Util#compress(byte[], int)} method.
|
||||
* @param inputBytes Compressed data.
|
||||
* @param which Which subclass if InputStream to use.
|
||||
* @return Uncompressed data
|
||||
* @author Henry Varona
|
||||
*/
|
||||
public static byte[] decompress(byte[] inputBytes) {
|
||||
XZInputStream in = null;
|
||||
public static byte[] decompress(byte[] inputBytes, int which) {
|
||||
InputStream in = null;
|
||||
try {
|
||||
ByteArrayInputStream input = new ByteArrayInputStream(inputBytes);
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream(2048);
|
||||
if(which == XZ) {
|
||||
in = new XZInputStream(input);
|
||||
}else if(which == LZMA){
|
||||
in = new LZMAInputStream(input);
|
||||
}
|
||||
int size;
|
||||
while ((size = in.read()) != -1) {
|
||||
output.write(size);
|
||||
|
@ -92,8 +110,7 @@ public class Util {
|
|||
try {
|
||||
in.close();
|
||||
} 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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue