Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add compression utilities #214

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions eden-components/eden-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@
<optional>true</optional>
</dependency>

<!-- 压缩工具 -->
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.lz4</groupId>
<artifactId>lz4-java</artifactId>
<optional>true</optional>
</dependency>

<!-- 字节码增强 -->
<dependency>
<groupId>org.javassist</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ylzl.eden.commons.compression;

import lombok.experimental.UtilityClass;
import net.jpountz.lz4.LZ4FrameInputStream;
import net.jpountz.lz4.LZ4FrameOutputStream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
* Lz4 压缩工具集
*
* @author <a href="mailto:[email protected]">gyl</a>
* @since 2.4.x
*/
@UtilityClass
public class Lz4Utils {

public static byte[] compress(byte[] data, int level) throws IOException {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(data.length);
LZ4FrameOutputStream outputStream = new LZ4FrameOutputStream(byteArrayOutputStream)) {
outputStream.write(data);
outputStream.flush();
outputStream.close();
return byteArrayOutputStream.toByteArray();
}
}

public static byte[] decompress(byte[] data) throws IOException {
byte[] b = new byte[data.length];
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
LZ4FrameInputStream lz4InputStream = new LZ4FrameInputStream(byteArrayInputStream);
ByteArrayOutputStream resultOutputStream = new ByteArrayOutputStream(data.length)) {
while (true) {
int len = lz4InputStream.read(b, 0, b.length);
if (len <= 0) {
break;
}
resultOutputStream.write(b, 0, len);
}
resultOutputStream.flush();
resultOutputStream.close();
return resultOutputStream.toByteArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ylzl.eden.commons.compression;

import lombok.experimental.UtilityClass;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

/**
* Zlib 压缩工具集
*
* @author <a href="mailto:[email protected]">gyl</a>
* @since 2.4.x
*/
@UtilityClass
public class ZlibUtils {

public static byte[] compress(byte[] data, int level) throws IOException {
Deflater defeater = new Deflater(level);
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(data.length);
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater)) {
deflaterOutputStream.write(data);
deflaterOutputStream.finish();
deflaterOutputStream.close();
return byteArrayOutputStream.toByteArray();
} finally {
defeater.end();
}
}

public static byte[] decompress(byte[] data) throws IOException {
byte[] b = new byte[data.length];
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
InflaterInputStream inflaterInputStream = new InflaterInputStream(byteArrayInputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(data.length)) {
while (true) {
int len = inflaterInputStream.read(b, 0, b.length);
if (len <= 0) {
break;
}
byteArrayOutputStream.write(b, 0, len);
}
byteArrayOutputStream.flush();
return byteArrayOutputStream.toByteArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ylzl.eden.commons.compression;

import com.github.luben.zstd.ZstdInputStream;
import com.github.luben.zstd.ZstdOutputStream;
import lombok.experimental.UtilityClass;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
* Zstd 压缩工具集
*
* @author <a href="mailto:[email protected]">gyl</a>
* @since 2.4.x
*/
@UtilityClass
public class ZstdUtils {

public static byte[] compress(byte[] data, int level) throws IOException {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(data.length);
ZstdOutputStream outputStream = new ZstdOutputStream(byteArrayOutputStream, level)) {
outputStream.write(data);
outputStream.flush();
outputStream.close();
return byteArrayOutputStream.toByteArray();
}
}

public static byte[] decompress(byte[] data) throws IOException {
byte[] b = new byte[data.length];
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
ZstdInputStream zstdInputStream = new ZstdInputStream(byteArrayInputStream);
ByteArrayOutputStream resultOutputStream = new ByteArrayOutputStream(data.length)) {
while (true) {
int len = zstdInputStream.read(b, 0, b.length);
if (len <= 0) {
break;
}
resultOutputStream.write(b, 0, len);
}
resultOutputStream.flush();
resultOutputStream.close();
return resultOutputStream.toByteArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ylzl.eden.commons.compression;
14 changes: 14 additions & 0 deletions eden-components/eden-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
<javers.version>6.8.1</javers.version>
<java-object-diff.version>0.95</java-object-diff.version>
<ahocorasick.version>0.6.3</ahocorasick.version>
<zstd-jni.version>1.5.6-6</zstd-jni.version>
<lz4-java.version>1.8.0</lz4-java.version>

<!-- 关系型数据库 -->
<mysql-connector-java.version>8.0.32</mysql-connector-java.version>
Expand Down Expand Up @@ -1295,6 +1297,18 @@
<artifactId>ahocorasick</artifactId>
<version>${ahocorasick.version}</version>
</dependency>
<!-- zstd-jni -->
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>${zstd-jni.version}</version>
</dependency>
<!-- lz4-java -->
<dependency>
<groupId>org.lz4</groupId>
<artifactId>lz4-java</artifactId>
<version>${lz4-java.version}</version>
</dependency>

<!-- 数据库 -->
<!-- MySQL -->
Expand Down
Loading