<commit_before>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 compressionFilters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.FilterChain;
import javax.servlet.GenericFilter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Implementation of <code>javax.servlet.Filter</code> used to compress
 * the ServletResponse if it is bigger than a threshold.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class CompressionFilter extends GenericFilter {

    private static final long serialVersionUID = 1L;

    /**
     * Minimal reasonable threshold.
     */
    private final int minThreshold = 128;

    /**
     * The threshold number to compress.
     */
    protected int compressionThreshold = 0;

    /**
     * Minimal reasonable buffer.
     */
    private final int minBuffer = 8192;  // 8KB is what tomcat would use by default anyway

    /**
     * The compression buffer size to avoid chunking.
     */
    protected int compressionBuffer = 0;

    /**
     * The mime types to compress.
     */
    protected String[] compressionMimeTypes = {"text/html", "text/xml", "text/plain"};

    /**
     * Debug level for this filter.
     */
    private int debug = 0;

    @Override
    public void init() {
        String str = getInitParameter("debug");
        if (str != null) {
            debug = Integer.parseInt(str);
        }

        str = getInitParameter("compressionThreshold");
        if (str != null) {
            compressionThreshold = Integer.parseInt(str);
            if (compressionThreshold != 0 && compressionThreshold < minThreshold) {
                if (debug > 0) {
                    System.out.println("compressionThreshold should be either 0 - no compression or >= " + minThreshold);
                    System.out.println("compressionThreshold set to " + minThreshold);
                }
                compressionThreshold = minThreshold;
            }
        }

        str = getInitParameter("compressionBuffer");
        if (str != null) {
            compressionBuffer = Integer.parseInt(str);
            if (compressionBuffer < minBuffer) {
                if (debug > 0) {
                    System.out.println("compressionBuffer should be >= " + minBuffer);
                    System.out.println("compressionBuffer set to " + minBuffer);
                }
                compressionBuffer = minBuffer;
            }<commit_msg>Fix a couple of minor FindBugs warnings<commit_after>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 compressionFilters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.FilterChain;
import javax.servlet.GenericFilter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Implementation of <code>javax.servlet.Filter</code> used to compress
 * the ServletResponse if it is bigger than a threshold.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class CompressionFilter extends GenericFilter {

    private static final long serialVersionUID = 1L;

    /**
     * Minimal reasonable threshold.
     */
    private static final int MIN_THRESHOLD = 128;

    /**
     * Minimal reasonable buffer.
     */
    // 8KB is what tomcat would use by default anyway
    private static final int MIN_BUFFER = 8192;

    /**
     * The threshold number to compress.
     */
    protected int compressionThreshold = 0;

    /**
     * The compression buffer size to avoid chunking.
     */
    protected int compressionBuffer = 0;

    /**
     * The mime types to compress.
     */
    protected String[] compressionMimeTypes = {"text/html", "text/xml", "text/plain"};

    /**
     * Debug level for this filter.
     */
    private int debug = 0;

    @Override
    public void init() {
        String str = getInitParameter("debug");
        if (str != null) {
            debug = Integer.parseInt(str);
        }

        str = getInitParameter("compressionThreshold");
        if (str != null) {
            compressionThreshold = Integer.parseInt(str);
            if (compressionThreshold != 0 && compressionThreshold < MIN_THRESHOLD) {
                if (debug > 0) {
                    System.out.println("compressionThreshold should be either 0 - no compression or >= " + MIN_THRESHOLD);
                    System.out.println("compressionThreshold set to " + MIN_THRESHOLD);
                }
                compressionThreshold = MIN_THRESHOLD;
            }
        }

        str = getInitParameter("compressionBuffer");
        if (str != null) {
            compressionBuffer = Integer.parseInt(str);
            if (compressionBuffer < MIN_BUFFER) {
                if (debug > 0) {
                    System.out.println("compressionBuffer should be >= " + MIN_BUFFER);
                    System.out.println("compressionBuffer set to " + MIN_BUFFER);
                }
                compressionBuffer = MIN_BUFFER;
            }
{'fraction_non_alphanumeric': 0.08882480173035329, 'fraction_numerical': 0.005767844268204758, 'mean_word_length': 2.642857142857143, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '3874910', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1723, 'n_words': 610}