kafka对lz4进行了重新封装
maven引入
org.apache.kafka
kafka-clients
0.8.2.2
2. 代码
/**
* 压缩
* @param data
* @return
* @throws IOException
*/
public static String compress(String data) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter out = new PrintWriter(new KafkaLZ4BlockOutputStream(baos, 4));
try {
out.print(data);
out.flush();
} finally {
if(out != null) {
out.close();
}
}
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
/**
* 解压缩
* @param data
* @return
* @throws IOException
*/
public static String deCompress(String data) throws IOException {
byte[] source = Base64.getDecoder().decode(data);
ByteArrayInputStream bais = new ByteArrayInputStream(source);
BufferedReader br = new BufferedReader(new InputStreamReader(new KafkaLZ4BlockInputStream(bais)));
StringBuffer sb = new StringBuffer();
String str = null;
try {
while((str = br.readLine()) != null) {
sb.append(str);
}
} finally {
if(br != null) {
br.close();
}
}
return sb.toString();
}