LZ4 압축 및 압축해제 in JAVA
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.codec.binary.Base64;
import net.jpountz.lz4.LZ4FrameInputStream;
import net.jpountz.lz4.LZ4FrameOutputStream;
public class Lz4Compress {
public static final int DEFAULT_BUFFER_SIZE = 8192;
public static final int EOF = -1;
public static byte[] compress(final byte[] bytes) throws IOException {
try (ByteArrayOutputStream rstBao = new ByteArrayOutputStream();) {
try (LZ4FrameOutputStream zos = new LZ4FrameOutputStream(rstBao);) {
zos.write(bytes);
}
byte[] bts = rstBao.toByteArray();
return Base64.encodeBase64(bts);
}
}
public static byte[] uncompress(byte[] bytes) throws IOException {
byte[] result = null;
try (final InputStream input = new LZ4FrameInputStream(new ByteArrayInputStream(Base64.decodeBase64(bytes)));) {
try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
if (input != null) {
while (EOF != (input.read(buffer))) {
output.write(buffer);
}
}
result = output.toByteArray();
}
}
return result;
}
}
반응형
'Computer Science > Spring' 카테고리의 다른 글
[Spring] Optional 객체 여부에 따른 map 사용법 (1) | 2022.12.26 |
---|---|
[Spring] Get 호출시 response 에 한글 깨짐 현상 (0) | 2022.09.05 |
[Spring] AOP 패러다임 (0) | 2019.01.01 |
[Spring] Mysql / MyBatis insert 쿼리 수행하면서 PK 가져오기 (0) | 2018.12.27 |
[Spring] Spring project log4j 충돌 현상 (0) | 2018.12.20 |