<commit_before>/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 */

package jlibs.core.nio.channels;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.zip.Deflater;

/**
 * @author <NAME>
 */
public class DeflaterOutputChannel extends FilterOutputChannel{
    protected Deflater deflater = new Deflater();

    public DeflaterOutputChannel(OutputChannel delegate){
        this(delegate, new Deflater());
    }

    protected DeflaterOutputChannel(OutputChannel delegate, Deflater deflater){
        super(delegate);
        this.deflater = deflater;
        writeBuffer.limit(0);
    }

    private ByteBuffer writeBuffer = ByteBuffer.allocate(1024);

    @Override
    protected int onWrite(ByteBuffer src) throws IOException{
        int wrote = 0;
        if(deflater.needsInput()){
            wrote = src.remaining();
            deflater.setInput(src.array(), src.arrayOffset()+src.position(), wrote);
        }
        return wrote;
    }

    protected void doWritePending() throws IOException{
        while(true){
            if(writeBuffer.hasRemaining()){
                delegate.write(writeBuffer);
                if(writeBuffer.hasRemaining())
                    break;
            }
            if(deflater.finished()){
                deflater.end();
                break;
            }
            if(deflater.needsInput())
                break;
            int compressed = deflater.deflate(writeBuffer.array(), 0, writeBuffer.capacity());
            writeBuffer.position(0);
            writeBuffer.limit(compressed);
        }
    }

    @Override
    protected Status selfStatus(){
        return !writeBuffer.hasRemaining() && (deflater.needsInput() || deflater.finished()) ? Status.COMPLETED : Status.NEEDS_OUTPUT;
    }

    @Override
    protected void doClose() throws IOException{
        deflater.finish();
    }
}<commit_msg>Noticed that: deflater.needsInput() is always returning true, after deflater.end() is called onWrite should do nothing if deflater.needsInput() is false<commit_after>/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 */

package jlibs.core.nio.channels;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.zip.Deflater;

/**
 * @author <NAME>
 */
public class DeflaterOutputChannel extends FilterOutputChannel{
    protected Deflater deflater = new Deflater();

    public DeflaterOutputChannel(OutputChannel delegate){
        this(delegate, new Deflater());
    }

    protected DeflaterOutputChannel(OutputChannel delegate, Deflater deflater){
        super(delegate);
        this.deflater = deflater;
        writeBuffer.limit(0);
    }

    private ByteBuffer writeBuffer = ByteBuffer.allocate(1024);

    @Override
    protected int onWrite(ByteBuffer src) throws IOException{
        int wrote = 0;
        if(deflater.needsInput()){
            wrote = src.remaining();
            deflater.setInput(src.array(), src.arrayOffset()+src.position(), wrote);
            src.position(src.limit());
        }
        return wrote;
    }

    protected void doWritePending() throws IOException{
        while(true){
            if(writeBuffer.hasRemaining()){
                delegate.write(writeBuffer);
                if(writeBuffer.hasRemaining())
                    break;
            }
            if(deflater.finished()){
                deflater.end();
                break;
            }
            if(isOpen() && deflater.needsInput())
                break;
            int compressed = deflater.deflate(writeBuffer.array(), 0, writeBuffer.capacity());
            writeBuffer.position(0);
            writeBuffer.limit(compressed);
        }
    }

    @Override
    protected Status selfStatus(){
        return !writeBuffer.hasRemaining() && ((isOpen() && deflater.needsInput()) || deflater.finished()) ? Status.COMPLETED : Status.NEEDS_OUTPUT;
    }

    @Override
    protected void doClose() throws IOException{
        deflater.finish();
    }
}
{'fraction_non_alphanumeric': 0.09807010634107917, 'fraction_numerical': 0.005513981882630957, 'mean_word_length': 2.6252676659528906, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '376191', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1307, 'n_words': 397}