package com.xxx.arch.mw.nbp.common.csp;
import com.xxx.arch.mw.nbp.common.constant.CommonConstants;
import com.xxx.arch.mw.nbp.common.exception.NbpException;
import static com.xxx.arch.mw.nbp.common.csp.CompressorEnum.ZSTD;
/**
*
*/
public interface Compressor {
int BUFFER_LENGTH = CommonConstants.DEFAULT_COMPRESSION_BUFFER_SIZE;
CompressorEnum DEFAULT_COMPRESSOR_ALGORITHM = CompressorEnum.toEnumFromName(CommonConstants.DEFAULT_COMPRESSION_ALGORITHM);
/**
* compress data
*
* @param data
* @return
* @throws NbpException
*/
byte[] compress(byte[] data) throws NbpException;
}
package com.xxx.arch.mw.nbp.common.csp;
import com.xxx.arch.mw.nbp.common.constant.CommonConstants;
import com.xxx.arch.mw.nbp.common.exception.NbpException;
/**
*
*/
public interface Decompressor {
int BUFFER_LENGTH = CommonConstants.DEFAULT_COMPRESSION_BUFFER_SIZE;
/**
* dcompress data
* @param data
* @return
* @throws NbpException
*/
byte[] decompress(byte[] data) throws NbpException;
}
package com.xxx.arch.mw.nbp.common.csp;
import lombok.Getter;
/**
*
*/
public enum CompressorEnum {
REVERSED(0, "reversed", "仅反转"),
BZIP2(1, "bzip2", "BZIP2算法,压缩比高但耗时耗CPU"),
GZIP(2, "gz", "GZIP算法,压缩比高但耗时耗CPU"),
SNAPPY_FRAMED(3, "snappy-framed", "SNAPPY_FRAMED算法"),
DEFLATE(4, "deflate", "DEFLATE算法"),
PACK200(5, "pack200", "DEFLATE64算法"),
LZ4_BLOCK(6, "lz4-block", "LZ4_BLOCK算法"),
LZ4_FRAMED(7, "lz4-framed", "LZ4_FRAMED算法"),
ZSTD(8, "zstd", "ZSTD算法");
@Getter
private int id;
@Getter
private String name;
@Getter
private String desc;
CompressorEnum(int id, String name, String desc) {
this.id = id;
this.name = name;
this.desc = desc;
}
public static CompressorEnum toEnumFromId(int id) {
for (CompressorEnum eum : values()) {
if (eum.getId() == id) {
return eum;
}
}
return null;
}
public static CompressorEnum toEnumFromName(String name) {
if (name == null || name.length() == 0) {
return null;
}
for (CompressorEnum eum : values()) {
if (eum.getName().equalsIgnoreCase(name)) {
return eum;
}
}
return null;
}
}
package com.xxx.arch.mw.nbp.common.csp;
import com.xxx.arch.mw.nbp.common.domain.NbpCode;
import com.xxx.arch.mw.nbp.common.exception.NbpException;
import org.apache.commons.compress.compressors.CompressorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
/**
*
*/
public abstract class AbstractCompressor implements Compressor {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public byte[] compress(byte[] data) throws NbpException {
try (ByteArrayInputStream is = new ByteArrayInputStream(data);
ByteArrayOutputStream os = new ByteArrayOutputStream()) {
compress(is, os);
return os.toByteArray();
} catch (IOException | CompressorException e) {
logger.error("NBP-CLIENT UTIL", "compress data error with reason:", e);
throw new NbpException(NbpCode.COMPRESSION_ERROR.getCode(), e.getMessage(), e.getCause());
}
}
public abstract void compress(InputStream is, OutputStream os) throws IOException, CompressorException;
}
package com.xxx.arch.mw.nbp.common.csp;
import com.xxx.arch.mw.nbp.common.domain.NbpCode;
import com.xxx.arch.mw.nbp.common.exception.NbpException;
import org.apache.commons.compress.compressors.CompressorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
/**
*
*/
public abstract class AbstractDecompressor implements Decompressor {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public byte[] decompress(byte[] data) throws NbpException {
try (ByteArrayInputStream is = new ByteArrayInputStream(data);
ByteArrayOutputStream os = new ByteArrayOutputStream()) {
decompress(is, os);
return os.toByteArray();
} catch (IOException | CompressorException e) {
logger.error("NBP-CLIENT UTIL", "decompress data error with reason:", e);
throw new NbpException(NbpCode.COMPRESSION_ERROR.getCode(), e.getMessage(), e.getCause());
}
}
public abstract void decompress(InputStream is, OutputStream os) throws IOException, CompressorException;
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp;
import com.xxx.arch.mw.nbp.common.csp.bzip2.Bzip2Compressor;
import com.xxx.arch.mw.nbp.common.csp.deflate.DeflateCompressor;
import com.xxx.arch.mw.nbp.common.csp.gzip.GzipCompressor;
import com.xxx.arch.mw.nbp.common.csp.lz4block.Lz4BlockCompressor;
import com.xxx.arch.mw.nbp.common.csp.lz4framed.Lz4FramedCompressor;
import com.xxx.arch.mw.nbp.common.csp.pack200.Pack200Compressor;
import com.xxx.arch.mw.nbp.common.csp.reversed.ReversedCompressor;
import com.xxx.arch.mw.nbp.common.csp.snappyframed.SnappyFramedCompressor;
import com.xxx.arch.mw.nbp.common.csp.zstd.ZstdCompressor;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static com.xxx.arch.mw.nbp.common.csp.CompressorEnum.ZSTD;
/**
* @author shuchen
* @version V1.0.0
* @since 2020-07-14 11:20
*/
public class CompressorFactory {
private static final ConcurrentMap COMPRESSOR_MAP = new ConcurrentHashMap<>();
public static Compressor getDefaultCompressor() {
return getCompressor(Compressor.DEFAULT_COMPRESSOR_ALGORITHM);
}
public static Compressor getCompressor(CompressorEnum compressorEnum) {
Compressor compressor = COMPRESSOR_MAP.get(compressorEnum);
if (compressor == null) {
switch (compressorEnum) {
case REVERSED:
compressor = new ReversedCompressor();
break;
case BZIP2:
compressor = new Bzip2Compressor();
break;
case GZIP:
compressor = new GzipCompressor();
break;
case SNAPPY_FRAMED:
compressor = new SnappyFramedCompressor();
break;
case DEFLATE:
compressor = new DeflateCompressor();
break;
case PACK200:
compressor = new Pack200Compressor();
break;
case LZ4_BLOCK:
compressor = new Lz4BlockCompressor();
break;
case LZ4_FRAMED:
compressor = new Lz4FramedCompressor();
break;
case ZSTD:
compressor = new ZstdCompressor();
break;
default:
}
if (compressor != null) {
COMPRESSOR_MAP.putIfAbsent(compressorEnum, compressor);
}
}
return compressor;
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp;
import com.xxx.arch.mw.nbp.common.csp.bzip2.Bzip2Decompressor;
import com.xxx.arch.mw.nbp.common.csp.deflate.DeflateDecompressor;
import com.xxx.arch.mw.nbp.common.csp.gzip.GzipDecompressor;
import com.xxx.arch.mw.nbp.common.csp.lz4block.Lz4BlockDecompressor;
import com.xxx.arch.mw.nbp.common.csp.lz4framed.Lz4FramedDecompressor;
import com.xxx.arch.mw.nbp.common.csp.pack200.Pack200Decompressor;
import com.xxx.arch.mw.nbp.common.csp.reversed.ReversedDecompressor;
import com.xxx.arch.mw.nbp.common.csp.snappyframed.SnappyFramedDecompressor;
import com.xxx.arch.mw.nbp.common.csp.zstd.ZstdDecompressor;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static com.xxx.arch.mw.nbp.common.csp.CompressorEnum.DEFLATE;
import static com.xxx.arch.mw.nbp.common.csp.CompressorEnum.ZSTD;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-14 11:20
*/
public class DecompressorFactory {
private static final ConcurrentMap DECOMPRESSOR_MAP = new ConcurrentHashMap<>();
public static Decompressor getDefaultDecompressor() {
return getDecompressor(Compressor.DEFAULT_COMPRESSOR_ALGORITHM);
}
public static Decompressor getDecompressor(CompressorEnum compressorEnum) {
Decompressor decompressor = DECOMPRESSOR_MAP.get(compressorEnum);
if (decompressor == null) {
switch (compressorEnum) {
case REVERSED:
decompressor = new ReversedDecompressor();
break;
case BZIP2:
decompressor = new Bzip2Decompressor();
break;
case GZIP:
decompressor = new GzipDecompressor();
break;
case SNAPPY_FRAMED:
decompressor = new SnappyFramedDecompressor();
break;
case DEFLATE:
decompressor = new DeflateDecompressor();
break;
case PACK200:
decompressor = new Pack200Decompressor();
break;
case LZ4_BLOCK:
decompressor = new Lz4BlockDecompressor();
break;
case LZ4_FRAMED:
decompressor = new Lz4FramedDecompressor();
break;
case ZSTD:
decompressor = new ZstdDecompressor();
break;
default:
}
if (decompressor != null) {
DECOMPRESSOR_MAP.putIfAbsent(compressorEnum, decompressor);
}
}
return decompressor;
}
}
package com.xxx.arch.mw.nbp.common.csp.zstd;
import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import com.github.luben.zstd.ZstdOutputStream;
import org.apache.commons.compress.compressors.CompressorException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @date 2023/06/25
*/
public class ZstdCompressor extends AbstractCompressor {
@Override
public void compress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (ZstdOutputStream zos = new ZstdOutputStream(os)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = is.read(buffer, 0, buffer.length))) {
zos.write(buffer, 0, pos);
}
}
}
}
package com.xxx.arch.mw.nbp.common.csp.zstd;
import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import com.github.luben.zstd.ZstdInputStream;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @date 2023/06/25
*/
public class ZstdDecompressor extends AbstractDecompressor {
@Override
public void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (ZstdInputStream zis = new ZstdInputStream(is)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = zis.read(buffer, 0, buffer.length))) {
os.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.snappyframed;
import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class SnappyFramedCompressor extends AbstractCompressor {
@Override
public void compress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorOutputStream cos = new CompressorStreamFactory()
.createCompressorOutputStream(CompressorStreamFactory.SNAPPY_FRAMED, os)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = is.read(buffer, 0, buffer.length))) {
cos.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.snappyframed;
import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class SnappyFramedDecompressor extends AbstractDecompressor {
@Override
public void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorInputStream cis = new CompressorStreamFactory()
.createCompressorInputStream(CompressorStreamFactory.SNAPPY_FRAMED, is)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {
os.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.reversed;
import com.xxx.arch.mw.nbp.common.csp.Compressor;
import com.xxx.arch.mw.nbp.common.exception.NbpException;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class ReversedCompressor implements Compressor {
@Override
public byte[] compress(byte[] data) throws NbpException {
int length = data.length;
byte[] compressed = new byte[length];
for (int i = 0; i < length; i++) {
compressed[i] = data[length - 1 - i];
}
return compressed;
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.reversed;
import com.xxx.arch.mw.nbp.common.csp.Decompressor;
import com.xxx.arch.mw.nbp.common.exception.NbpException;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class ReversedDecompressor implements Decompressor {
@Override
public byte[] decompress(byte[] data) throws NbpException {
int length = data.length;
byte[] decompressed = new byte[length];
for (int i = 0; i < length; i++) {
decompressed[i] = data[length - 1 - i];
}
return decompressed;
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.pack200;
import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class Pack200Compressor extends AbstractCompressor {
@Override
public void compress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorOutputStream cos = new CompressorStreamFactory()
.createCompressorOutputStream(CompressorStreamFactory.PACK200, os)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = is.read(buffer, 0, buffer.length))) {
cos.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.pack200;
import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class Pack200Decompressor extends AbstractDecompressor {
@Override
public void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorInputStream cis = new CompressorStreamFactory()
.createCompressorInputStream(CompressorStreamFactory.PACK200, is)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {
os.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.lz4framed;
import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class Lz4FramedCompressor extends AbstractCompressor {
@Override
public void compress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorOutputStream cos = new CompressorStreamFactory()
.createCompressorOutputStream(CompressorStreamFactory.LZ4_FRAMED, os)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = is.read(buffer, 0, buffer.length))) {
cos.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.lz4framed;
import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class Lz4FramedDecompressor extends AbstractDecompressor {
@Override
public void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorInputStream cis = new CompressorStreamFactory()
.createCompressorInputStream(CompressorStreamFactory.LZ4_FRAMED, is)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {
os.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.lz4block;
import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class Lz4BlockCompressor extends AbstractCompressor {
@Override
public void compress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorOutputStream cos = new CompressorStreamFactory()
.createCompressorOutputStream(CompressorStreamFactory.LZ4_BLOCK, os)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = is.read(buffer, 0, buffer.length))) {
cos.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.lz4block;
import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class Lz4BlockDecompressor extends AbstractDecompressor {
@Override
public void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorInputStream cis = new CompressorStreamFactory()
.createCompressorInputStream(CompressorStreamFactory.LZ4_BLOCK, is)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {
os.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.gzip;
import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class GzipCompressor extends AbstractCompressor {
@Override
public void compress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorOutputStream cos = new CompressorStreamFactory()
.createCompressorOutputStream(CompressorStreamFactory.GZIP, os)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = is.read(buffer, 0, buffer.length))) {
cos.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.gzip;
import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class GzipDecompressor extends AbstractDecompressor {
@Override
public void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorInputStream cis = new CompressorStreamFactory()
.createCompressorInputStream(CompressorStreamFactory.GZIP, is)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {
os.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.deflate;
import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class DeflateCompressor extends AbstractCompressor {
@Override
public void compress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorOutputStream cos = new CompressorStreamFactory()
.createCompressorOutputStream(CompressorStreamFactory.DEFLATE, os)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = is.read(buffer, 0, buffer.length))) {
cos.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.deflate;
import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class DeflateDecompressor extends AbstractDecompressor {
@Override
public void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorInputStream cis = new CompressorStreamFactory()
.createCompressorInputStream(CompressorStreamFactory.DEFLATE, is)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {
os.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.bzip2;
import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class Bzip2Compressor extends AbstractCompressor {
@Override
public void compress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorOutputStream cos = new CompressorStreamFactory()
.createCompressorOutputStream(CompressorStreamFactory.BZIP2, os)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = is.read(buffer, 0, buffer.length))) {
cos.write(buffer, 0, pos);
}
}
}
}
/*
* Copyright 2017-2018 Iabc Co.Ltd.
*
* 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 com.xxx.arch.mw.nbp.common.csp.bzip2;
import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Project: remote-repository
*
* @author shuchen
* @version V1.0.0
* @since 2020-07-13 09:53
*/
public class Bzip2Decompressor extends AbstractDecompressor {
@Override
public void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {
try (CompressorInputStream cis = new CompressorStreamFactory()
.createCompressorInputStream(CompressorStreamFactory.BZIP2, is)) {
int pos;
byte[] buffer = new byte[BUFFER_LENGTH];
while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {
os.write(buffer, 0, pos);
}
}
}
}
压缩
final CompressorEnum compressorEnum = CompressorEnum.toEnumFromName(propertyDTO.getPublisher().getCompressAlgorithm());
final Compressor compressor = compressorEnum != null ?
CompressorFactory.getCompressor(compressorEnum) : CompressorFactory.getDefaultCompressor();
byte[] body = ConverterUtil.toBody(singleDetail.getUserContext());
final byte[] compressedBody = compressor.compress(body);
final String compressedContext = Base64.getEncoder().encodeToString(compressedBody);
解压缩
final CompressorEnum compressorEnum = CompressorEnum
.toEnumFromName(singleDetail.getUserContext().get(InstanceKey.COMPRESSED_ALGORITHM_KEY));
final Decompressor decompressor = compressorEnum != null ?
DecompressorFactory.getDecompressor(compressorEnum) : DecompressorFactory.getDefaultDecompressor();
final byte[] body = decompressor.decompress(compressedBody);