<commit_before>/*
 * Copyright (C) 2009 the original author(s).
 *
 * Licensed 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 org.sonatype.gshell.util.io;

import jline.Terminal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.gshell.command.IO;

import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.PrintStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * An input pipe which can be interrupted.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 2.0
 */
public class InputPipe
    extends Thread
{
    private static final Logger log = LoggerFactory.getLogger(InputPipe.class);

    private final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1024);

    private final Terminal term;

    private final InputStream in;

    private final PrintStream err;

    private final Callable<Boolean> interruptHandler;

    private final CountDownLatch startSignal = new CountDownLatch(1);

    private volatile boolean interrupt;

    private volatile boolean running;

    public InputPipe(final IO io, final Callable<Boolean> interruptHandler) {
        assert io != null;
        this.term = io.getTerminal();
        this.in = io.streams.in;
        this.err = io.streams.err;
        assert interruptHandler != null;
        this.interruptHandler = interruptHandler;
    }

    public void close() {
        if (running) {
            log.trace("Closing");
            Thread.currentThread().interrupt();
            running = false;
        }
    }
    
    private int read() throws IOException {
// FIXME: See if this is really needed and figure out why...
//            if (term instanceof AnsiWindowsTerminal) {
//                c = ((AnsiWindowsTerminal) term).readDirectChar(in);
//            }
//            else {
//                c = terminal.readCharacter(in);
//            }
        return term.readCharacter(in);
    }

    @Override
    public void start() {
        super.start();

        // Wait for the run-loop to actually start before we return, to avoid race-conditions
        try {
            startSignal.await();
        }
        catch (InterruptedException e) {
            // ignore
        }
    }

    public void run() {
        log.trace("Running");
        running = true;

        try {
            startSignal.countDown();
            
            while (running) {
                int c = read();

                switch (c) {
                    case -1:
                        queue.put(c);
                        return;

                    case 3:
                        err.println("^C");
                        interrupt = interruptHandler.call();
                        break;
<commit_msg>Add some comments<commit_after>/*
 * Copyright (C) 2009 the original author(s).
 *
 * Licensed 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 org.sonatype.gshell.util.io;

import jline.Terminal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.gshell.command.IO;

import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.PrintStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * An input pipe which can be interrupted.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 2.0
 */
public class InputPipe
    extends Thread
{
    private static final Logger log = LoggerFactory.getLogger(InputPipe.class);

    private final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1024);

    private final Terminal term;

    private final InputStream in;

    private final PrintStream err;

    private final Callable<Boolean> interruptHandler;

    private final CountDownLatch startSignal = new CountDownLatch(1);

    private volatile boolean interrupt;

    private volatile boolean running;

    public InputPipe(final IO io, final Callable<Boolean> interruptHandler) {
        assert io != null;
        this.term = io.getTerminal();
        this.in = io.streams.in;
        this.err = io.streams.err;
        assert interruptHandler != null;
        this.interruptHandler = interruptHandler;
    }

    public void close() {
        if (running) {
            log.trace("Closing");
            Thread.currentThread().interrupt();
            running = false;
        }
    }
    
    private int read() throws IOException {
// FIXME: See if this is really needed and figure out why...
//            if (term instanceof AnsiWindowsTerminal) {
//                c = ((AnsiWindowsTerminal) term).readDirectChar(in);
//            }
//            else {
//                c = terminal.readCharacter(in);
//            }
        return term.readCharacter(in);
    }

    @Override
    public void start() {
        super.start();

        // Wait for the run-loop to actually start before we return, to avoid race-conditions
        try {
            startSignal.await();
        }
        catch (InterruptedException e) {
            // ignore
        }
    }

    public void run() {
        log.trace("Running");
        running = true;

        try {
            startSignal.countDown();
            
            // NOTE: There is a small window here, where the input stream might read from the queue, before the read() call is invoked
            
            while (running) {
                int c = read();

                switch (c) {
                    case -1:
                        queue.put(c);
                        return;

                    case 3:
                        err.println("^C");
                        interrupt = interruptHandler.call();
                        break;

{'fraction_non_alphanumeric': 0.09377700950734659, 'fraction_numerical': 0.005473926822241429, 'mean_word_length': 2.1487528344671203, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7392404', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1781, 'n_words': 578}