X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fio%2FLZ4InputOutputStreamSupport.java;fp=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fio%2FLZ4InputOutputStreamSupport.java;h=689b58564bc00dd623ea701a0515715f9b7d5597;hb=e7e69069ae5ecaacc9ea0e47cb40cdf68237d636;hp=0000000000000000000000000000000000000000;hpb=6ef0b898f2117a4bb3a510c0df7af340f4fc8eca;p=controller.git diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/io/LZ4InputOutputStreamSupport.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/io/LZ4InputOutputStreamSupport.java new file mode 100644 index 0000000000..689b58564b --- /dev/null +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/io/LZ4InputOutputStreamSupport.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.cluster.io; + +import com.google.common.io.ByteSource; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import net.jpountz.lz4.LZ4Factory; +import net.jpountz.lz4.LZ4FrameInputStream; +import net.jpountz.lz4.LZ4FrameOutputStream; +import net.jpountz.lz4.LZ4FrameOutputStream.FLG.Bits; +import net.jpountz.xxhash.XXHashFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class LZ4InputOutputStreamSupport extends InputOutputStreamFactory { + private static final Logger LOG = LoggerFactory.getLogger(LZ4InputOutputStreamSupport.class); + private static final LZ4Factory LZ4_FACTORY = LZ4Factory.fastestInstance(); + private static final XXHashFactory HASH_FACTORY = XXHashFactory.fastestInstance(); + + private final LZ4FrameOutputStream.BLOCKSIZE blocksize; + + LZ4InputOutputStreamSupport(final LZ4FrameOutputStream.BLOCKSIZE blocksize) { + this.blocksize = blocksize; + } + + @Override + public InputStream createInputStream(final ByteSource input) throws IOException { + final InputStream stream = input.openStream(); + try { + return new LZ4FrameInputStream(stream, LZ4_FACTORY.safeDecompressor(), HASH_FACTORY.hash32()); + } catch (IOException e) { + stream.close(); + LOG.warn("Error loading with lz4 decompression, using default one", e); + return input.openBufferedStream(); + } + } + + @Override + public InputStream createInputStream(final File file) throws IOException { + final FileInputStream fileInput = new FileInputStream(file); + try { + return new LZ4FrameInputStream(fileInput, LZ4_FACTORY.safeDecompressor(), HASH_FACTORY.hash32()); + } catch (IOException e) { + fileInput.close(); + LOG.warn("Error loading file with lz4 decompression, using default one", e); + return defaultCreateInputStream(file); + } + } + + @Override + public OutputStream createOutputStream(final File file) throws IOException { + return new LZ4FrameOutputStream(new FileOutputStream(file), blocksize, -1, LZ4_FACTORY.fastCompressor(), + HASH_FACTORY.hash32(), Bits.BLOCK_INDEPENDENCE); + } + + @Override + public OutputStream wrapOutputStream(final OutputStream output) throws IOException { + return new LZ4FrameOutputStream(output, blocksize, -1, LZ4_FACTORY.fastCompressor(), HASH_FACTORY.hash32(), + Bits.BLOCK_INDEPENDENCE); + } +}