-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
774b935
commit 5836f7f
Showing
6 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
eden-components/eden-commons/src/main/java/org/ylzl/eden/commons/compression/Lz4Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:shiyindaxiaojie@gmail.com">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(); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
eden-components/eden-commons/src/main/java/org/ylzl/eden/commons/compression/ZlibUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:shiyindaxiaojie@gmail.com">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(); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
eden-components/eden-commons/src/main/java/org/ylzl/eden/commons/compression/ZstdUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:shiyindaxiaojie@gmail.com">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(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...components/eden-commons/src/main/java/org/ylzl/eden/commons/compression/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters