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

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

Introduction

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

Prototype

public BZip2CompressorOutputStream(final OutputStream out, final int blockSize) throws IOException 

Source Link

Document

Constructs a new CBZip2OutputStream with specified blocksize.

Usage

From source file:de.upb.wdqa.wdvd.processors.output.CsvFeatureWriter.java

@Override
public void startRevisionProcessing() {
    logger.debug("Starting (" + featureFile + ")...");

    try {/*from   w ww  . j a va  2s.  c om*/
        OutputStreamWriter writer = new OutputStreamWriter(
                getPipedOutputStreamStream(new BZip2CompressorOutputStream(
                        new BufferedOutputStream(new FileOutputStream(featureFile)), BZIP2_BLOCKSIZE)),
                "utf-8");

        String[] header = new String[features.size()];

        for (int i = 0; i < features.size(); i++) {
            header[i] = features.get(i).getName();
        }

        csvPrinter = CSVFormat.RFC4180.withHeader(header).print(writer);

    } catch (IOException e) {
        logger.error("", e);
    }
}

From source file:msearch.io.MSFilmlisteSchreiben.java

public void filmlisteSchreibenJson(String datei, ListeFilme listeFilme) {
    MSLog.systemMeldung("Filme Schreiben (" + listeFilme.size() + " Filme) :");
    File file = new File(datei);
    File dir = new File(file.getParent());
    if (!dir.exists()) {
        if (!dir.mkdirs()) {
            MSLog.fehlerMeldung(915236478, MSLog.FEHLER_ART_PROG,
                    "MSearchIoXmlFilmlisteSchreiben.xmlSchreibenStart",
                    "Kann den Pfad nicht anlegen: " + dir.toString());
        }/*from   w  ww.  j  a  va 2 s .c  o m*/
    }
    MSLog.systemMeldung("   --> Start Schreiben nach: " + datei);
    try {
        String sender = "", thema = "";
        JsonFactory jsonF = new JsonFactory();
        JsonGenerator jg;
        if (datei.endsWith(MSConst.FORMAT_XZ)) {
            LZMA2Options options = new LZMA2Options();
            XZOutputStream out = new XZOutputStream(new FileOutputStream(file), options);
            jg = jsonF.createGenerator(out);
        } else if (datei.endsWith(MSConst.FORMAT_BZ2)) {
            bZip2CompressorOutputStream = new BZip2CompressorOutputStream(new FileOutputStream(file),
                    9 /*Blocksize: 1 - 9*/);
            jg = jsonF.createGenerator(bZip2CompressorOutputStream, JsonEncoding.UTF8);
        } else if (datei.endsWith(MSConst.FORMAT_ZIP)) {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
            ZipEntry entry = new ZipEntry(MSConst.XML_DATEI_FILME);
            zipOutputStream.putNextEntry(entry);
            jg = jsonF.createGenerator(zipOutputStream, JsonEncoding.UTF8);
        } else {
            jg = jsonF.createGenerator(new File(datei), JsonEncoding.UTF8);
        }
        jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier
        jg.writeStartObject();
        // Infos zur Filmliste
        jg.writeArrayFieldStart(ListeFilme.FILMLISTE);
        for (int i = 0; i < ListeFilme.MAX_ELEM; ++i) {
            jg.writeString(listeFilme.metaDaten[i]);
        }
        jg.writeEndArray();
        // Infos der Felder in der Filmliste
        jg.writeArrayFieldStart(ListeFilme.FILMLISTE);
        for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) {
            jg.writeString(DatenFilm.COLUMN_NAMES[DatenFilm.COLUMN_NAMES_JSON[i]]);
        }
        jg.writeEndArray();
        //Filme schreiben
        ListIterator<DatenFilm> iterator;
        DatenFilm datenFilm;
        iterator = listeFilme.listIterator();
        while (iterator.hasNext()) {
            datenFilm = iterator.next();
            jg.writeArrayFieldStart(DatenFilm.FILME_);
            for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) {
                int m = DatenFilm.COLUMN_NAMES_JSON[i];
                if (m == DatenFilm.FILM_SENDER_NR) {
                    if (datenFilm.arr[m].equals(sender)) {
                        jg.writeString("");
                    } else {
                        sender = datenFilm.arr[m];
                        jg.writeString(datenFilm.arr[m]);
                    }
                } else if (m == DatenFilm.FILM_THEMA_NR) {
                    if (datenFilm.arr[m].equals(thema)) {
                        jg.writeString("");
                    } else {
                        thema = datenFilm.arr[m];
                        jg.writeString(datenFilm.arr[m]);
                    }
                } else {
                    jg.writeString(datenFilm.arr[m]);
                }
            }
            jg.writeEndArray();
        }
        jg.writeEndObject();
        jg.close();
        MSLog.systemMeldung("   --> geschrieben!");
    } catch (Exception ex) {
        MSLog.fehlerMeldung(846930145, MSLog.FEHLER_ART_PROG, "IoXmlSchreiben.FilmeSchreiben", ex,
                "nach: " + datei);
    }
}

From source file:msearch.filmlisten.MSFilmlisteSchreiben.java

public void filmlisteSchreibenJson(String datei, ListeFilme listeFilme) {
    MSLog.systemMeldung("Filme schreiben (" + listeFilme.size() + " Filme) :");
    File file = new File(datei);
    File dir = new File(file.getParent());
    if (!dir.exists()) {
        if (!dir.mkdirs()) {
            MSLog.fehlerMeldung(915236478, MSLog.FEHLER_ART_PROG,
                    "MSearchIoXmlFilmlisteSchreiben.xmlSchreibenStart",
                    "Kann den Pfad nicht anlegen: " + dir.toString());
        }// w  w  w.ja va2  s.c om
    }
    MSLog.systemMeldung("   --> Start Schreiben nach: " + datei);
    try {
        String sender = "", thema = "";
        JsonFactory jsonF = new JsonFactory();
        JsonGenerator jg;
        if (datei.endsWith(MSConst.FORMAT_XZ)) {
            LZMA2Options options = new LZMA2Options();
            XZOutputStream out = new XZOutputStream(new FileOutputStream(file), options);
            jg = jsonF.createGenerator(out);
        } else if (datei.endsWith(MSConst.FORMAT_BZ2)) {
            bZip2CompressorOutputStream = new BZip2CompressorOutputStream(new FileOutputStream(file),
                    9 /*Blocksize: 1 - 9*/);
            jg = jsonF.createGenerator(bZip2CompressorOutputStream, JsonEncoding.UTF8);
        } else if (datei.endsWith(MSConst.FORMAT_ZIP)) {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
            ZipEntry entry = new ZipEntry(MSConst.XML_DATEI_FILME);
            zipOutputStream.putNextEntry(entry);
            jg = jsonF.createGenerator(zipOutputStream, JsonEncoding.UTF8);
        } else {
            jg = jsonF.createGenerator(new File(datei), JsonEncoding.UTF8);
        }
        jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier
        jg.writeStartObject();
        // Infos zur Filmliste
        jg.writeArrayFieldStart(ListeFilme.FILMLISTE);
        for (int i = 0; i < ListeFilme.MAX_ELEM; ++i) {
            jg.writeString(listeFilme.metaDaten[i]);
        }
        jg.writeEndArray();
        // Infos der Felder in der Filmliste
        jg.writeArrayFieldStart(ListeFilme.FILMLISTE);
        for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) {
            jg.writeString(DatenFilm.COLUMN_NAMES[DatenFilm.COLUMN_NAMES_JSON[i]]);
        }
        jg.writeEndArray();
        //Filme schreiben
        ListIterator<DatenFilm> iterator;
        DatenFilm datenFilm;
        iterator = listeFilme.listIterator();
        while (iterator.hasNext()) {
            datenFilm = iterator.next();
            jg.writeArrayFieldStart(DatenFilm.FILME_);
            for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) {
                int m = DatenFilm.COLUMN_NAMES_JSON[i];
                if (m == DatenFilm.FILM_SENDER_NR) {
                    if (datenFilm.arr[m].equals(sender)) {
                        jg.writeString("");
                    } else {
                        sender = datenFilm.arr[m];
                        jg.writeString(datenFilm.arr[m]);
                    }
                } else if (m == DatenFilm.FILM_THEMA_NR) {
                    if (datenFilm.arr[m].equals(thema)) {
                        jg.writeString("");
                    } else {
                        thema = datenFilm.arr[m];
                        jg.writeString(datenFilm.arr[m]);
                    }
                } else {
                    jg.writeString(datenFilm.arr[m]);
                }
            }
            jg.writeEndArray();
        }
        jg.writeEndObject();
        jg.close();
        MSLog.systemMeldung("   --> geschrieben!");
    } catch (Exception ex) {
        MSLog.fehlerMeldung(846930145, MSLog.FEHLER_ART_PROG, "IoXmlSchreiben.FilmeSchreiben", ex,
                "nach: " + datei);
    }
}

From source file:mediathek.controller.IoXmlSchreiben.java

private void xmlSchreibenStart() throws IOException, XMLStreamException {
    Log.systemMeldung("Start Schreiben nach: " + xmlFilePath.toAbsolutePath());
    final OutputStream outputStream = Files.newOutputStream(xmlFilePath);
    if (xmlFilePath.endsWith(GuiKonstanten.FORMAT_BZ2)) {
        bZip2CompressorOutputStream = new BZip2CompressorOutputStream(outputStream, 2);
        out = new OutputStreamWriter(bZip2CompressorOutputStream, Konstanten.KODIERUNG_UTF);
    } else if (xmlFilePath.endsWith(GuiKonstanten.FORMAT_ZIP)) {
        zipOutputStream = new ZipOutputStream(outputStream);
        ZipEntry entry = new ZipEntry(Konstanten.PROGRAMMNAME);
        zipOutputStream.putNextEntry(entry);
        out = new OutputStreamWriter(zipOutputStream, Konstanten.KODIERUNG_UTF);
    } else {//from w  w  w.jav a  2s.  c  om
        out = new OutputStreamWriter(outputStream, Konstanten.KODIERUNG_UTF);
    }

    XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
    writer = outFactory.createXMLStreamWriter(out);
    writer.writeStartDocument(Konstanten.KODIERUNG_UTF, "1.0");
    writer.writeCharacters("\n");//neue Zeile
    writer.writeStartElement(Konstanten.XML_START);
    writer.writeCharacters("\n");//neue Zeile
}

From source file:com.chenshu.compress.CompressOldTest.java

@Benchmark
public int commonsBZip2Compress() {
    ByteArrayOutputStream bout = null;
    BZip2CompressorOutputStream bzip2out = null;
    try {//from w  ww . jav  a2s  .  c om
        bout = new ByteArrayOutputStream(data.length);
        bzip2out = new BZip2CompressorOutputStream(bout, level);
        bzip2out.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bzip2out != null) {
            try {
                bzip2out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bout != null) {
            try {
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    byte[] bs = bout.toByteArray();
    return bs.length;
}

From source file:com.simiacryptus.text.CompressionUtil.java

/**
 * Encode bz byte [ ].//from   w  w w . j  a v a 2  s  .c o  m
 *
 * @param data the data
 * @return the byte [ ]
 */
public static byte[] encodeBZ(byte[] data) {
    try {
        int blockSize = 4;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        BZip2CompressorOutputStream compresser = new BZip2CompressorOutputStream(output, blockSize);
        compresser.write(data);
        compresser.close();
        byte[] bytes = output.toByteArray();
        return bytes;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.netty.handler.codec.compression.Bzip2DecoderTest.java

private static void testDecompression(final byte[] data) throws Exception {
    for (int blockSize = MIN_BLOCK_SIZE; blockSize <= MAX_BLOCK_SIZE; blockSize++) {
        final EmbeddedChannel channel = new EmbeddedChannel(new Bzip2Decoder());

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BZip2CompressorOutputStream bZip2Os = new BZip2CompressorOutputStream(os, blockSize);
        bZip2Os.write(data);/*from   w ww .  ja  v  a  2  s .c o  m*/
        bZip2Os.close();

        ByteBuf compressed = Unpooled.wrappedBuffer(os.toByteArray());
        channel.writeInbound(compressed);

        ByteBuf uncompressed = Unpooled.buffer();
        ByteBuf msg;
        while ((msg = channel.readInbound()) != null) {
            uncompressed.writeBytes(msg);
            msg.release();
        }
        final byte[] result = new byte[uncompressed.readableBytes()];
        uncompressed.readBytes(result);
        uncompressed.release();

        assertArrayEquals(data, result);
    }
}

From source file:msearch.filmlisten.MSFilmlisteSchreiben.java

private void xmlSchreibenStart(String datei) throws IOException, XMLStreamException {
    File file = new File(datei);
    File dir = new File(file.getParent());
    if (!dir.exists()) {
        if (!dir.mkdirs()) {
            MSLog.fehlerMeldung(947623049, MSLog.FEHLER_ART_PROG,
                    "MSearchIoXmlFilmlisteSchreiben.xmlSchreibenStart",
                    "Kann den Pfad nicht anlegen: " + dir.toString());
        }/*from  ww  w  .j a v a 2  s .c  o  m*/
    }
    MSLog.systemMeldung("   --> Start Schreiben nach: " + datei);
    XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
    if (datei.endsWith(MSConst.FORMAT_BZ2)) {
        bZip2CompressorOutputStream = new BZip2CompressorOutputStream(new FileOutputStream(file),
                9 /*Blocksize: 1 - 9*/);
        out = new OutputStreamWriter(bZip2CompressorOutputStream, MSConst.KODIERUNG_UTF);
    } else if (datei.endsWith(MSConst.FORMAT_ZIP)) {
        zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
        ZipEntry entry = new ZipEntry(MSConst.XML_DATEI_FILME);
        zipOutputStream.putNextEntry(entry);
        out = new OutputStreamWriter(zipOutputStream, MSConst.KODIERUNG_UTF);
    } else {
        out = new OutputStreamWriter(new FileOutputStream(file), MSConst.KODIERUNG_UTF);
    }
    writer = outFactory.createXMLStreamWriter(out);
    writer.writeStartDocument("UTF-8", "1.0");
    writer.writeCharacters("\n");//neue Zeile
    writer.writeStartElement(MSConst.XML_START);
    writer.writeCharacters("\n");//neue Zeile
}

From source file:io.netty.handler.codec.compression.Bzip2DecoderTest.java

@Test
public void testDecompressionOfBatchedFlowOfData() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    BZip2CompressorOutputStream bZip2Os = new BZip2CompressorOutputStream(os,
            rand.nextInt(MIN_BLOCK_SIZE, MAX_BLOCK_SIZE + 1));
    bZip2Os.write(BYTES_LARGE);//  w w w.j ava 2s.c o  m
    bZip2Os.close();

    final byte[] compressedArray = os.toByteArray();
    int written = 0, length = rand.nextInt(100);
    while (written + length < compressedArray.length) {
        ByteBuf compressed = Unpooled.wrappedBuffer(compressedArray, written, length);
        channel.writeInbound(compressed);
        written += length;
        length = rand.nextInt(100);
    }
    ByteBuf compressed = Unpooled.wrappedBuffer(compressedArray, written, compressedArray.length - written);
    channel.writeInbound(compressed);

    ByteBuf uncompressed = Unpooled.buffer();
    ByteBuf msg;
    while ((msg = channel.readInbound()) != null) {
        uncompressed.writeBytes(msg);
        msg.release();
    }
    final byte[] result = new byte[uncompressed.readableBytes()];
    uncompressed.readBytes(result);
    uncompressed.release();

    assertArrayEquals(BYTES_LARGE, result);
}