<commit_before>/*
 * Cuelib library for manipulating cue sheets.
 * Copyright (C) 2007-2008 <NAME>
 * 
 * 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.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package jwbroek.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Utility class for piping data from an InputStream to an OutputStream, or to nowhere. This class is particularly useful
 * for reading the output streams of a java.lang.Process, as such a Process may block if its output is not read.
 * @author jwbroek
 */
public class StreamPiper implements Runnable
{
  private InputStream from;
  private OutputStream to;
  
  /**
   * Pipe all input from the InputStream to the OutputStream. The OutputStream is explicitly allowed to be null.
   * In such a case, all input will be discarded. In any case, the OutputStream will not be closed by StreamPiper,
   * but the InputStream will, once its end is reached.
   * @param from
   * @param to
   */
  public StreamPiper(InputStream from, OutputStream to)
  {
    this.from = from;
    this.to = to;
  }
  
  /**
   * Perform the data piping.
   */
  public void run()
  {
    try
    {
      int input = this.from.read();
      while (input != -1)
      {
        if (this.to != null)
        {
          this.to.write(input);
        }
        input = this.from.read();
      }
    }
    catch (IOException e)
    {
      // Nothing we can do.
    }
    finally
    {
      try
      {
        this.from.close();
      }
      catch (IOException e)
      {
        // Nothing we can do.
      }
    }<commit_msg>Can now choose to have the StreamPiper close the output stream.<commit_after>/*
 * Cuelib library for manipulating cue sheets.
 * Copyright (C) 2007-2008 <NAME>
 * 
 * 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.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package jwbroek.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Utility class for piping data from an InputStream to an OutputStream, or to nowhere. This class is particularly useful
 * for reading the output streams of a java.lang.Process, as such a Process may block if its output is not read.
 * @author jwbroek
 */
public class StreamPiper implements Runnable
{
  private InputStream from;
  private OutputStream to;
  private boolean closeOutput;
  
  /**
   * Pipe all input from the InputStream to the OutputStream. The OutputStream is explicitly allowed to be null.
   * In such a case, all input will be discarded. In any case, the OutputStream will not be closed by StreamPiper,
   * but the InputStream will, once its end is reached.
   * @param from
   * @param to
   */
  public StreamPiper(InputStream from, OutputStream to)
  {
    this(from, to, false);
  }
  
  /**
   * Pipe all input from the InputStream to the OutputStream. The OutputStream is explicitly allowed to be null.
   * In such a case, all input will be discarded. In any case, the OutputStream will only be closed by StreamPiper if
   * this is requested, while the InputStream will always be, once its end is reached.
   * @param from
   * @param to
   * @param closeOutput
   */
  public StreamPiper(InputStream from, OutputStream to, boolean closeOutput)
  {
    this.from = from;
    this.to = to;
    this.closeOutput = closeOutput;
  }
  
  /**
   * Perform the data piping.
   */
  public void run()
  {
    try
    {
      int input = this.from.read();
      while (input != -1)
      {
        if (this.to != null)
        {
          this.to.write(input);
        }
        input = this.from.read();
      }
    }
    catch (IOException e)
    {
      // Nothing we can do.
    }
    finally
    {
      try
      {
        this.from.close();
      }
      catch (IOException e)
      {
        // Nothing we can do.
      }
      if (this.closeOutput && this.to != null)
      {
        try
        {
          this.to.close();
        }
        catch (IOException e)
        {
          // Nothing we can do.
        }
      }
    }
{'fraction_non_alphanumeric': 0.0773542600896861, 'fraction_numerical': 0.008221225710014948, 'mean_word_length': 2.4986928104575163, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2863588', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1475, 'n_words': 662}