-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
109 lines (90 loc) · 3.45 KB
/
Util.java
File metadata and controls
109 lines (90 loc) · 3.45 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
/**
* Copyright (C) 2020, ControlThings Oy Ab
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* @license Apache-2.0
*/
package utils;
import android.util.Base64;
import org.bson.BsonBinaryWriter;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.EncoderContext;
import org.bson.io.BasicOutputBuffer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* Created by jeppe on 11/16/16.
*/
public class Util {
final protected static char[] hexArray = "0123456789abcdef".toCharArray();
//public static native String bsonToString(byte[] bson_bytes);
public static String byteArrayToHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
private static Codec<Document> DOCUMENT_CODEC = new DocumentCodec();
public static byte[] jsonToByteArray(String json) {
final Document document = Document.parse(json);
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
DOCUMENT_CODEC.encode(writer, document, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
return outputBuffer.toByteArray();
}
public static String byteArrayToZipString(byte[] input) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(input);
gos.close();
byte[] compressed = os.toByteArray();
os.close();
return Base64.encodeToString(compressed, Base64.DEFAULT);
}
public static byte[] stringToByteArrayZip(String input) throws IOException{
byte[] DocumentCompressed = Base64.decode(input, Base64.DEFAULT);
ByteArrayInputStream is = new ByteArrayInputStream(DocumentCompressed);
GZIPInputStream gis = new GZIPInputStream(is);
byte[] buffer = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
while ((len = gis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
is.close();
gis.close();
out.close();
return out.toByteArray();
}
public static String prettyPrintException(Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
String stackTrace = sw.toString();
return stackTrace;
}
}