Repository navigation
Expand file tree
/
Copy pathVMUtils.java
More file actions
154 lines (128 loc) · 4.84 KB
/
Copy pathVMUtils.java
File metadata and controls
154 lines (128 loc) · 4.84 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.common.runtime.vm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tron.common.runtime.config.SystemProperties;
import java.io.*;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterOutputStream;
import static java.lang.String.format;
import static org.apache.commons.codec.binary.Base64.decodeBase64;
import static org.apache.commons.codec.binary.Base64.encodeBase64String;
public final class VMUtils {
private static final Logger LOGGER = LoggerFactory.getLogger("VM");
private VMUtils() {
}
public static void closeQuietly(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException ioe) {
// ignore
}
}
private static File createProgramTraceFile(SystemProperties config, String txHash) {
File result = null;
if (config.vmTrace()) {
File file = new File(new File("./", "vm_trace"), txHash + ".json");
if (file.exists()) {
if (file.isFile() && file.canWrite()) {
result = file;
}
} else {
try {
file.getParentFile().mkdirs();
file.createNewFile();
result = file;
} catch (IOException e) {
// ignored
}
}
}
return result;
}
private static void writeStringToFile(File file, String data) {
OutputStream out = null;
try {
out = new FileOutputStream(file);
if (data != null) {
out.write(data.getBytes("UTF-8"));
}
} catch (Exception e){
LOGGER.error(format("Cannot write to file '%s': ", file.getAbsolutePath()), e);
} finally {
closeQuietly(out);
}
}
public static void saveProgramTraceFile(SystemProperties config, String txHash, String content) {
File file = createProgramTraceFile(config, txHash);
if (file != null) {
writeStringToFile(file, content);
}
}
private static final int BUF_SIZE = 4096;
private static void write(InputStream in, OutputStream out, int bufSize) throws IOException {
try {
byte[] buf = new byte[bufSize];
for (int count = in.read(buf); count != -1; count = in.read(buf)) {
out.write(buf, 0, count);
}
} finally {
closeQuietly(in);
closeQuietly(out);
}
}
public static byte[] compress(byte[] bytes) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
DeflaterOutputStream out = new DeflaterOutputStream(baos, new Deflater(), BUF_SIZE);
write(in, out, BUF_SIZE);
return baos.toByteArray();
}
public static byte[] compress(String content) throws IOException {
return compress(content.getBytes("UTF-8"));
}
public static byte[] decompress(byte[] data) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length);
ByteArrayInputStream in = new ByteArrayInputStream(data);
InflaterOutputStream out = new InflaterOutputStream(baos, new Inflater(), BUF_SIZE);
write(in, out, BUF_SIZE);
return baos.toByteArray();
}
public static String zipAndEncode(String content) {
try {
return encodeBase64String(compress(content));
} catch (Exception e) {
LOGGER.error("Cannot zip or encode: ", e);
return content;
}
}
public static String unzipAndDecode(String content) {
try {
byte[] decoded = decodeBase64(content);
return new String(decompress(decoded), "UTF-8");
} catch (Exception e) {
LOGGER.error("Cannot unzip or decode: ", e);
return content;
}
}
}