<commit_before>/*
 * eXist Open Source Native XML Database Copyright (C) 2001-2005, <NAME>.
 * Meier (<EMAIL>)
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * $Id$
 */
package org.exist.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.exist.util.io.FastByteArrayInputStream;
import org.exist.util.io.FastByteArrayOutputStream;

public class Compressor {

    /**
     * The method <code>compress</code>
     *
     * @param whatToCompress a <code>byte[]</code> value
     * @return a <code>byte[]</code> value
     * @exception IOException if an error occurs
     */
    public static byte[] compress(byte[] whatToCompress) throws IOException {
        return compress(whatToCompress, whatToCompress.length);
    }
    
    /**
     * The method <code>compress</code>
     *
     * @param whatToCompress a <code>byte[]</code> value
     * @param length an <code>int</code> value
     * @return a <code>byte[]</code> value
     * @exception IOException if an error occurs
     */
    public static byte[] compress(byte[] whatToCompress, int length) throws IOException {
        try (final FastByteArrayOutputStream baos = new FastByteArrayOutputStream(length);
                final ZipOutputStream gzos = new ZipOutputStream(baos)) {
            gzos.setMethod(ZipOutputStream.DEFLATED);
            gzos.putNextEntry(new ZipEntry(length + ""));
            gzos.write(whatToCompress, 0, length);
            gzos.closeEntry();
            gzos.finish();
            return baos.toByteArray();
        }
    }
    
    /**
     * The method <code>uncompress</code>
     *
     * @param whatToUncompress a <code>byte[]</code> value
     * @return a <code>byte[]</code> value
     * @exception IOException if an error occurs
     */
    public static byte[] uncompress(byte[] whatToUncompress)
	throws IOException {
        try (final FastByteArrayOutputStream baos = new FastByteArrayOutputStream()) {
            uncompress(whatToUncompress, baos);
            return baos.toByteArray();
        }
    }
    
    public static void uncompress(byte[] whatToUncompress, OutputStream os)
    throws IOException {
        try (final FastByteArrayInputStream bais = new FastByteArrayInputStream(whatToUncompress);
             final ZipInputStream gzis = new ZipInputStream(bais)) {
            final ZipEntry zipentry = gzis.getNextEntry();
            Integer.parseInt(zipentry.getName());
            final byte[] buf = new byte[512];
            int bread;
            while ((bread = gzis.read(buf)) != -1)
                os.write(buf, 0, bread);
            gzis.closeEntry();
        }
    }
}

<commit_msg>[ignore] Remove dead code<commit_after>/*
 * eXist Open Source Native XML Database Copyright (C) 2001-2005, <NAME>.
 * Meier (<EMAIL>)
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * $Id$
 */
package org.exist.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.exist.util.io.FastByteArrayInputStream;
import org.exist.util.io.FastByteArrayOutputStream;

public class Compressor {

    /**
     * The method <code>compress</code>
     *
     * @param whatToCompress a <code>byte[]</code> value
     * @return a <code>byte[]</code> value
     * @exception IOException if an error occurs
     */
    public static byte[] compress(byte[] whatToCompress) throws IOException {
        return compress(whatToCompress, whatToCompress.length);
    }
    
    /**
     * The method <code>compress</code>
     *
     * @param whatToCompress a <code>byte[]</code> value
     * @param length an <code>int</code> value
     * @return a <code>byte[]</code> value
     * @exception IOException if an error occurs
     */
    public static byte[] compress(byte[] whatToCompress, int length) throws IOException {
        try (final FastByteArrayOutputStream baos = new FastByteArrayOutputStream(length);
                final ZipOutputStream gzos = new ZipOutputStream(baos)) {
            gzos.setMethod(ZipOutputStream.DEFLATED);
            gzos.putNextEntry(new ZipEntry(length + ""));
            gzos.write(whatToCompress, 0, length);
            gzos.closeEntry();
            gzos.finish();
            return baos.toByteArray();
        }
    }
    
    /**
     * The method <code>uncompress</code>
     *
     * @param whatToUncompress a <code>byte[]</code> value
     * @return a <code>byte[]</code> value
     * @exception IOException if an error occurs
     */
    public static byte[] uncompress(byte[] whatToUncompress)
	throws IOException {
        try (final FastByteArrayOutputStream baos = new FastByteArrayOutputStream()) {
            uncompress(whatToUncompress, baos);
            return baos.toByteArray();
        }
    }
    
    public static void uncompress(byte[] whatToUncompress, OutputStream os)
    throws IOException {
        try (final FastByteArrayInputStream bais = new FastByteArrayInputStream(whatToUncompress);
             final ZipInputStream gzis = new ZipInputStream(bais)) {
            gzis.getNextEntry();    // move to the first entry in the zip stream!
            final byte[] buf = new byte[512];
            int bread;
            while ((bread = gzis.read(buf)) != -1)
                os.write(buf, 0, bread);
            gzis.closeEntry();
        }
    }
}


{'fraction_non_alphanumeric': 0.09867751780264497, 'fraction_numerical': 0.0075570411277430605, 'mean_word_length': 2.844692737430168, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2118348', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1850, 'n_words': 618}