Example usage for org.apache.commons.compress.compressors.bzip2 BZip2CompressorOutputStream write

List of usage examples for org.apache.commons.compress.compressors.bzip2 BZip2CompressorOutputStream write

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.bzip2 BZip2CompressorOutputStream write.

Prototype

public void write(final byte[] buf, int offs, final int len) throws IOException 

Source Link

Usage

From source file:CompressTransfer.java

/**
 * ?/*from   w  w  w .  j  a va 2  s  . c  o  m*/
 *
 * @param is
 * @param os
 * @throws Exception
 */
public static void compress(InputStream is, OutputStream os) throws Exception {

    BZip2CompressorOutputStream gos = new BZip2CompressorOutputStream(os);

    int count;
    byte data[] = new byte[BUFFER];
    while ((count = is.read(data, 0, BUFFER)) != -1) {
        gos.write(data, 0, count);
    }

    gos.finish();

    gos.flush();
    gos.close();
}

From source file:CompressTransfer.java

public static void transfer(FileInputStream srcIs, FileOutputStream destOut) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BZip2CompressorOutputStream gos = new BZip2CompressorOutputStream(destOut);
    int count;/*from w  ww .  ja  v a2  s .  c o m*/
    byte data[] = new byte[BUFFER];
    while ((count = srcIs.read(data, 0, BUFFER)) != -1) {
        //out.write(data, 0, count);
        gos.write(data, 0, count);
    }
    gos.flush();
    gos.close();
}

From source file:com.yahoo.ycsb.db.RedisClient.java

public static String compress(String st) {
    if (compress != null && compress.equals("y")) {
        if (compressAlgo != null && (compressAlgo.equals("lz4") || compressAlgo.equals("lz4hc"))) {
            try {
                byte[] data = st.getBytes("ISO-8859-1");
                LZ4Compressor compressor;
                if (compressAlgo.equals("lz4")) {
                    compressor = lz4factory.fastCompressor();
                } else {
                    compressor = lz4factory.highCompressor();
                }//  ww w  . jav  a 2s.com
                final int decompressedLength = data.length;
                int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
                byte[] compressed = new byte[maxCompressedLength];
                int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0,
                        maxCompressedLength);
                byte[] compressed2 = Arrays.copyOf(compressed, compressedLength);
                String ret = decompressedLength + "|" + new String(compressed2, "ISO-8859-1");
                return ret;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (compressAlgo != null && compressAlgo.equals("bzip2")) {
            try {
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(st.getBytes("ISO-8859-1"));
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                BZip2CompressorOutputStream bzOut = new BZip2CompressorOutputStream(baos);
                final byte[] buffer = new byte[8192];
                int n = 0;
                while (-1 != (n = byteArrayInputStream.read(buffer))) {
                    bzOut.write(buffer, 0, n);
                }
                bzOut.close();
                return new String(baos.toByteArray(), "ISO-8859-1");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (compressAlgo != null && compressAlgo.equals("snappy")) {
            try {
                byte[] compressed = Snappy.compress(st, "ISO-8859-1");
                String ret = new String(compressed, "ISO-8859-1");
                return ret;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return st;
}

From source file:adams.core.io.Bzip2Utils.java

/**
 * Compresses the specified file./*from w w  w .j  a v  a2  s. co m*/
 * <br><br>
 * See <a href="https://commons.apache.org/compress/examples.html" target="_blank">Apache commons/compress</a>.
 *
 * @param inputFile   the file to compress
 * @param buffer   the buffer size to use
 * @param outputFile   the destination file (the archive)
 * @param removeInput   whether to remove the input file
 * @return      the error message, null if everything OK
 */
public static String compress(File inputFile, int buffer, File outputFile, boolean removeInput) {
    String result;
    byte[] buf;
    int len;
    BZip2CompressorOutputStream out;
    BufferedInputStream in;
    String msg;
    FileInputStream fis;
    FileOutputStream fos;

    in = null;
    fis = null;
    out = null;
    fos = null;
    result = null;
    try {
        // does file already exist?
        if (outputFile.exists())
            System.err.println("WARNING: overwriting '" + outputFile + "'!");

        // create GZIP file
        buf = new byte[buffer];
        fos = new FileOutputStream(outputFile);
        out = new BZip2CompressorOutputStream(fos);
        fis = new FileInputStream(inputFile.getAbsolutePath());
        in = new BufferedInputStream(fis);

        // Transfer bytes from the file to the GZIP file
        while ((len = in.read(buf)) > 0)
            out.write(buf, 0, len);

        FileUtils.closeQuietly(in);
        FileUtils.closeQuietly(fis);
        FileUtils.closeQuietly(out);
        FileUtils.closeQuietly(fos);
        in = null;
        fis = null;
        out = null;
        fos = null;

        // remove input file?
        if (removeInput) {
            if (!inputFile.delete())
                result = "Failed to delete input file '" + inputFile + "' after successful compression!";
        }
    } catch (Exception e) {
        msg = "Failed to compress '" + inputFile + "': ";
        System.err.println(msg);
        e.printStackTrace();
        result = msg + e;
    } finally {
        FileUtils.closeQuietly(in);
        FileUtils.closeQuietly(fis);
        FileUtils.closeQuietly(out);
        FileUtils.closeQuietly(fos);
    }

    return result;
}

From source file:io.anserini.collection.WikipediaArticleTest.java

@Before
public void setUp() throws Exception {
    super.setUp();
    dType = new WikipediaCollection.Document("Sample Title", "Sample Content");

    String doc = "<mediawiki xmlns=\"http://www.mediawiki.org/xml/export-0.10/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xm\n"
            + "l/export-0.10.xsd\" version=\"0.10\" xml:lang=\"en\">\n" + "  <siteinfo>\n"
            + "    <sitename>Wiktionary</sitename>\n" + "    <dbname>enwiktionary</dbname>\n"
            + "    <base>https://en.wiktionary.org/wiki/Wiktionary:Main_Page</base>\n"
            + "    <generator>MediaWiki 1.31.0-wmf.26</generator>\n" + "    <case>case-sensitive</case>\n"
            + "    <namespaces>\n" + "      <namespace key=\"-2\" case=\"case-sensitive\">Media</namespace>\n"
            + "      <namespace key=\"-1\" case=\"first-letter\">Special</namespace>\n"
            + "      <namespace key=\"0\" case=\"case-sensitive\" />\n"
            + "      <namespace key=\"1\" case=\"case-sensitive\">Talk</namespace>\n" + " .... "
            + "    </namespaces>\n" + "  </siteinfo>\n" + "  <page>\n"
            + "    <title>Wiktionary:Welcome, newcomers</title>\n" + "    <ns>0</ns>\n" + "    <id>7</id>\n"
            + "<revision>\n" + "      <id>28863815</id>\n" + "      <parentid>18348012</parentid>\n"
            + "      <timestamp>2014-08-24T22:39:11Z</timestamp>\n" + "      <contributor>\n"
            + "        <username>Ready Steady Yeti</username>\n" + "        <id>1705268</id>\n"
            + "      </contributor>\n"
            + "      <comment>This link doesn't actually exist. If you don't believe me, check it yourself.</comment>\n"
            + "      <model>wikitext</model>\n" + "      <format>text/x-wiki</format>\n"
            + "      <text xml:space=\"preserve\">this is the \n" + " real content \n" + "      </text>\n"
            + "  </page>\n" + "</mediawiki>";

    tmpPath = createTempFile();// ww w .j a  v a  2  s .  co m
    OutputStream fout = Files.newOutputStream(tmpPath);
    BufferedOutputStream out = new BufferedOutputStream(fout);
    BZip2CompressorOutputStream tmpOut = new BZip2CompressorOutputStream(out);
    StringInputStream in = new StringInputStream(doc);
    final byte[] buffer = new byte[2048];
    int n = 0;
    while (-1 != (n = in.read(buffer))) {
        tmpOut.write(buffer, 0, n);
    }
    tmpOut.close();
}

From source file:io.anserini.document.WikipediaArticleTest.java

@Before
public void setUp() throws Exception {
    super.setUp();
    dType = new WikipediaArticle("Sample Title", "Sample Content");

    String doc = "<mediawiki xmlns=\"http://www.mediawiki.org/xml/export-0.10/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xm\n"
            + "l/export-0.10.xsd\" version=\"0.10\" xml:lang=\"en\">\n" + "  <siteinfo>\n"
            + "    <sitename>Wiktionary</sitename>\n" + "    <dbname>enwiktionary</dbname>\n"
            + "    <base>https://en.wiktionary.org/wiki/Wiktionary:Main_Page</base>\n"
            + "    <generator>MediaWiki 1.31.0-wmf.26</generator>\n" + "    <case>case-sensitive</case>\n"
            + "    <namespaces>\n" + "      <namespace key=\"-2\" case=\"case-sensitive\">Media</namespace>\n"
            + "      <namespace key=\"-1\" case=\"first-letter\">Special</namespace>\n"
            + "      <namespace key=\"0\" case=\"case-sensitive\" />\n"
            + "      <namespace key=\"1\" case=\"case-sensitive\">Talk</namespace>\n" + " .... "
            + "    </namespaces>\n" + "  </siteinfo>\n" + "  <page>\n"
            + "    <title>Wiktionary:Welcome, newcomers</title>\n" + "    <ns>0</ns>\n" + "    <id>7</id>\n"
            + "<revision>\n" + "      <id>28863815</id>\n" + "      <parentid>18348012</parentid>\n"
            + "      <timestamp>2014-08-24T22:39:11Z</timestamp>\n" + "      <contributor>\n"
            + "        <username>Ready Steady Yeti</username>\n" + "        <id>1705268</id>\n"
            + "      </contributor>\n"
            + "      <comment>This link doesn't actually exist. If you don't believe me, check it yourself.</comment>\n"
            + "      <model>wikitext</model>\n" + "      <format>text/x-wiki</format>\n"
            + "      <text xml:space=\"preserve\">this is the \n" + " real content \n" + "      </text>\n"
            + "  </page>\n" + "</mediawiki>";

    tmpPath = createTempFile();//from   w w  w  .j  a  va2 s  . c  om
    OutputStream fout = Files.newOutputStream(tmpPath);
    BufferedOutputStream out = new BufferedOutputStream(fout);
    BZip2CompressorOutputStream tmpOut = new BZip2CompressorOutputStream(out);
    StringInputStream in = new StringInputStream(doc);
    final byte[] buffer = new byte[2048];
    int n = 0;
    while (-1 != (n = in.read(buffer))) {
        tmpOut.write(buffer, 0, n);
    }
    tmpOut.close();
}

From source file:freenet.support.compress.Bzip2Compressor.java

@Override
public long compress(InputStream is, OutputStream os, long maxReadLength, long maxWriteLength)
        throws IOException, CompressionOutputSizeException {
    if (maxReadLength <= 0)
        throw new IllegalArgumentException();
    BZip2CompressorOutputStream bz2os = null;
    try {//from   w  w  w .  j a  v  a2 s.  c o  m
        CountedOutputStream cos = new CountedOutputStream(os);
        bz2os = new BZip2CompressorOutputStream(HeaderStreams.dimOutput(BZ_HEADER, cos));
        long read = 0;
        // Bigger input buffer, so can compress all at once.
        // Won't hurt on I/O either, although most OSs will only return a page at a time.
        byte[] buffer = new byte[32768];
        while (true) {
            int l = (int) Math.min(buffer.length, maxReadLength - read);
            int x = l == 0 ? -1 : is.read(buffer, 0, buffer.length);
            if (x <= -1)
                break;
            if (x == 0)
                throw new IOException("Returned zero from read()");
            bz2os.write(buffer, 0, x);
            read += x;
            if (cos.written() > maxWriteLength)
                throw new CompressionOutputSizeException();
        }
        bz2os.flush();
        cos.flush();
        bz2os.close();
        bz2os = null;
        if (cos.written() > maxWriteLength)
            throw new CompressionOutputSizeException();
        return cos.written();
    } finally {
        if (bz2os != null) {
            bz2os.flush();
            bz2os.close();
        }

    }
}

From source file:org.apache.avro.file.BZip2Codec.java

@Override
public ByteBuffer compress(ByteBuffer uncompressedData) throws IOException {

    ByteArrayOutputStream baos = getOutputBuffer(uncompressedData.remaining());
    BZip2CompressorOutputStream outputStream = new BZip2CompressorOutputStream(baos);

    try {//from   w w  w.  ja  v  a  2s .  c  o m
        outputStream.write(uncompressedData.array(), uncompressedData.position(), uncompressedData.remaining());
    } finally {
        outputStream.close();
    }

    ByteBuffer result = ByteBuffer.wrap(baos.toByteArray());
    return result;
}

From source file:org.dbpedia.spotlight.lucene.index.external.utils.SSERedirectCleaner.java

public static void compressInBz2(String originalFile, String inputFile, String outFile)
        throws FileNotFoundException, IOException {
    final File original = new File(originalFile);
    final File source = new File(inputFile);
    final File destination = new File(outFile);
    final BZip2CompressorOutputStream output = new BZip2CompressorOutputStream(
            new FileOutputStream(destination));
    final FileInputStream input = new FileInputStream(source);
    final byte[] buffer = new byte[8024];
    int n = 0;//from w  ww . j a va2 s. c o m
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
    }
    input.close();
    output.close();
    if (source.delete()) {
        LOG.info(source.getName() + " is deleted.");
    } else {
        LOG.error("Delete operation is failed.");
    }
    if (destination.renameTo(original)) {
        LOG.info(destination.getName() + " is renamed.");
    } else {
        LOG.error("Rename operation is failed.");
    }
}