All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zeroturnaround.exec.ProcessOutput Maven / Gradle / Ivy

There is a newer version: 1.12
Show newest version
/*
 * Copyright (C) 2014 ZeroTurnaround 
 * Contains fragments of code from Apache Commons Exec, rights owned
 * by Apache Software Foundation (ASF).
 *
 * 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.zeroturnaround.exec;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Standard output of a finished process.
 *
 * @author Rein Raudjärv
 * @see ProcessExecutor
 */
public class ProcessOutput {

  /**
   * Process output (not null).
   */
  private final byte[] data;

  public ProcessOutput(byte[] data) {
    this.data = data;
  }

  /**
   * @return binary output of the finished process.
   */
  public byte[] getBytes() {
    return data;
  }

  /**
   * @return output of the finished process converted to a String using platform's default encoding.
   */
  public String getString() {
    return new String(getBytes());
  }

  /**
   * @return output of the finished process converted to UTF-8 String.
   */
  public String getUTF8() {
    return getString("UTF-8");
  }

  /**
   * @return output of the finished process converted to a String.
   *
   * @param charset The name of a supported char set.
   */
  public String getString(String charset) {
    try {
      return new String(getBytes(), charset);
    }
    catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(e.getMessage());
    }
  }

  /**
   * @return output lines of the finished process converted using platform's default encoding.
   */
  public List getLines() {
    return getLinesFrom(getString());
  }

  /**
   * @return output lines of the finished process converted using UTF-8.
   */
  public List getLinesAsUTF8() {
    return getLinesFrom(getUTF8());
  }

  /**
   * @return output lines of the finished process converted using a given char set.
   *
   * @param charset The name of a supported char set.
   */
  public List getLines(String charset) {
    return getLinesFrom(getString(charset));
  }

  private static List getLinesFrom(String output) {
    // Split using both Windows and UNIX line separators
    List result = new ArrayList();
    StringTokenizer st = new StringTokenizer(output, "\n\r");
    while (st.hasMoreTokens()) {
      result.add(st.nextToken());
    }
    return result;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy