forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
69 changed files
with
786 additions
and
274 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
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
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
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
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
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
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
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,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright 1999-2019 Seata.io Group. | ||
~ | ||
~ 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 | ||
~ | ||
~ http://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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>io.seata</groupId> | ||
<artifactId>seata-compressor</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>seata-compressor-deflater</artifactId> | ||
<packaging>jar</packaging> | ||
<name>seata-compressor-deflater ${project.version}</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>seata-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
37 changes: 37 additions & 0 deletions
37
...ta-compressor-deflater/src/main/java/io/seata/compressor/deflater/DeflaterCompressor.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,37 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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 | ||
* | ||
* http://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 io.seata.compressor.deflater; | ||
|
||
import io.seata.common.loader.LoadLevel; | ||
import io.seata.core.compressor.Compressor; | ||
|
||
/** | ||
* @author dongzl | ||
*/ | ||
@LoadLevel(name = "DEFLATER") | ||
public class DeflaterCompressor implements Compressor { | ||
|
||
@Override | ||
public byte[] compress(byte[] bytes) { | ||
return DeflaterUtil.compress(bytes); | ||
} | ||
|
||
@Override | ||
public byte[] decompress(byte[] bytes) { | ||
return DeflaterUtil.decompress(bytes); | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
...or/seata-compressor-deflater/src/main/java/io/seata/compressor/deflater/DeflaterUtil.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,78 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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 | ||
* | ||
* http://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 io.seata.compressor.deflater; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.zip.Deflater; | ||
import java.util.zip.Inflater; | ||
|
||
/** | ||
* @author dongzl | ||
*/ | ||
public class DeflaterUtil { | ||
|
||
private DeflaterUtil() { | ||
|
||
} | ||
|
||
private static final int BUFFER_SIZE = 8192; | ||
|
||
public static byte[] compress(byte[] bytes) { | ||
if (bytes == null) { | ||
throw new NullPointerException("bytes is null"); | ||
} | ||
int lenght = 0; | ||
Deflater deflater = new Deflater(); | ||
deflater.setInput(bytes); | ||
deflater.finish(); | ||
byte[] outputBytes = new byte[BUFFER_SIZE]; | ||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { | ||
while (!deflater.finished()) { | ||
lenght = deflater.deflate(outputBytes); | ||
bos.write(outputBytes, 0, lenght); | ||
} | ||
deflater.end(); | ||
return bos.toByteArray(); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Deflater compress error", e); | ||
} | ||
} | ||
|
||
public static byte[] decompress(byte[] bytes) { | ||
if (bytes == null) { | ||
throw new NullPointerException("bytes is null"); | ||
} | ||
int length = 0; | ||
Inflater inflater = new Inflater(); | ||
inflater.setInput(bytes); | ||
byte[] outputBytes = new byte[BUFFER_SIZE]; | ||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();) { | ||
while (!inflater.finished()) { | ||
length = inflater.inflate(outputBytes); | ||
if (length == 0) { | ||
break; | ||
} | ||
bos.write(outputBytes, 0, length); | ||
} | ||
inflater.end(); | ||
return bos.toByteArray(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Deflater decompress error", e); | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...pressor-deflater/src/main/resources/META-INF/services/io.seata.core.compressor.Compressor
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 @@ | ||
io.seata.compressor.deflater.DeflaterCompressor |
34 changes: 34 additions & 0 deletions
34
...ompressor-deflater/src/test/java/io/seata/compressor/deflater/DeflaterCompressorTest.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,34 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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 | ||
* | ||
* http://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 io.seata.compressor.deflater; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @author dongzl | ||
*/ | ||
public class DeflaterCompressorTest { | ||
|
||
@Test | ||
public void testCompressAndDecompress() { | ||
DeflaterCompressor compressor = new DeflaterCompressor(); | ||
byte[] bytes = "seata".getBytes(); | ||
bytes = compressor.compress(bytes); | ||
bytes = compressor.decompress(bytes); | ||
Assertions.assertEquals(new String(bytes), "seata"); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...eata-compressor-deflater/src/test/java/io/seata/compressor/deflater/DeflaterUtilTest.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,47 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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 | ||
* | ||
* http://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 io.seata.compressor.deflater; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @author dongzl | ||
*/ | ||
public class DeflaterUtilTest { | ||
|
||
@Test | ||
public void test_compress() { | ||
Assertions.assertThrows(NullPointerException.class, () -> { | ||
DeflaterUtil.compress(null); | ||
}); | ||
} | ||
|
||
@Test | ||
public void test_decompress() { | ||
Assertions.assertThrows(NullPointerException.class, () -> { | ||
DeflaterUtil.decompress(null); | ||
}); | ||
} | ||
|
||
@Test | ||
public void test_compressEqualDecompress() { | ||
byte[] compress = DeflaterUtil.compress("seata".getBytes()); | ||
byte[] decompress = DeflaterUtil.decompress(compress); | ||
Assertions.assertEquals("seata", new String(decompress)); | ||
} | ||
|
||
} |
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
Oops, something went wrong.