Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## lp.wrap
Class WrapperUtils

    java.lang.Object
      lp.wrap.WrapperUtils

    public class WrapperUtils

extends Object

This class provides some static methods that can be useful when handling external commands that receive data on their standard input. It provides:

- convenience methods for extracting a command name from the command string (`getCommandName(String)` and `getShortCommandName(String)`),
- methods for transfering bytes from an `InputStream` to an `OutputStream` (`transfer(InputStream, OutputStream)`) or to a `StringBuilder` (`transfer(InputStream, StringBuilder)`)

Version:
  1.0.0
Author:
  Martin Slota

Field Summary
`private byte[]` | `byteBuffer` A byte buffer for the `transfer(InputStream, OutputStream)` method.
`private char[]` | `charBuffer` A character buffer for the `transfer(InputStream, StringBuilder)` method.
`private static WrapperUtils` | `instance` Reference to the singleton instance of this class.

Constructor Summary
`private` | `WrapperUtils()` A private constructor to ensure this is really a singleton.

Method Summary
`String` | `dumpToString(InputStream from)` Returns the contents of an `InputStream` as a string.
`Process` | `exec(String command)` Executes the command `command` through `Runtime.exec(String)` and puts the contents of `input` on the process's standard input.
`String` | `getCommandName(String command)` Returns the command's name given the command string, i.e. the part of the command string that precedes the first whitespace character.
`static WrapperUtils` | `getInstance()` Returns the singleton instance.
`String` | `getShortCommandName(String command)` Returns the command's name without the preceding path given the command string, i.e. the part of the command's name (as defined in `getCommandName(String)`) that follows the last `File.separatorChar` character.
`void` | `transfer(InputStream from, OutputStream to)` Transfers the contents of `from` to `to`.
`StringBuilder` | `transfer(InputStream from, StringBuilder to)` Transfers the contents of `from` to `to` using the default character encoding.
`void` | `transfer(String from, OutputStream to)` Transfers the contents of `from` to `to` using the default character encoding.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### byteBuffer

    private final byte[] byteBuffer

  A byte buffer for the `transfer(InputStream, OutputStream)` method.

### charBuffer

    private final char[] charBuffer

  A character buffer for the `transfer(InputStream, StringBuilder)` method.

### instance

    private static WrapperUtils instance

  Reference to the singleton instance of this class.

Constructor Detail

### WrapperUtils

    private WrapperUtils()

  A private constructor to ensure this is really a singleton.

Method Detail

### getInstance

    public static WrapperUtils getInstance()

  Returns the singleton instance.

### getCommandName

    public String getCommandName(String command)

  Returns the command's name given the command string, i.e. the part of the command string that precedes the first whitespace character. For example, if the separator character is '/' and the `command` is "/usr/bin/mkdir tmp", then the result will be "/usr/bin/mkdir". If `command` is `null`, then `null` is returned.

  Parameters:
    `command` - the command string
  Returns:
    the command's name

### getShortCommandName

    public String getShortCommandName(String command)

  Returns the command's name without the preceding path given the command string, i.e. the part of the command's name (as defined in `getCommandName(String)`) that follows the last `File.separatorChar` character. For example, if the separator character is '/' and the `command` is "/usr/bin/mkdir tmp", then the result will be "mkdir". If `command` is `null`, then `null` is returned.

  Parameters:
    `command` - the command string
  Returns:
    the command's name
  Throws:
    `IllegalArgumentException` - if `command` is `null`.
  See Also:
    `getCommandName(String)`

### transfer

    public void transfer(InputStream from,
                         OutputStream to)

  Transfers the contents of `from` to `to`. Invokes `from.close()` after the transfer. This method is thread-safe.

  Parameters:
    `from` - source of the transfer
    `to` - destination of the transfer
  Throws:
    `NullPointerException` - if `from` contains some bytes and `to` is `null` (in other cases the null values don't matter)
    `IOException` - (wrapped in an `ExceptionAdapter`) if an I/O error occurs during the transfer

### transfer

    public void transfer(String from,
                         OutputStream to)

  Transfers the contents of `from` to `to` using the default character encoding. This method is thread-safe. If `from` is `null`, nothing is appended.

  Parameters:
    `from` - string convert to bytes and append to `to`
    `to` - destination of the transfer
  Throws:
    `NullPointerException` - if `from` contains some bytes and `to` is `null` (otherwise the null values are gracefully ignored)
    `IOException` - (wrapped in an `ExceptionAdapter`) if an I/O error occurs during the transfer

### transfer

    public StringBuilder transfer(InputStream from,
                                  StringBuilder to)

  Transfers the contents of `from` to `to` using the default character encoding. Invokes `from.close()` after the transfer. This method is thread-safe.

  Parameters:
    `from` - source of the transfer
    `to` - destination of the transfer
  Throws:
    `NullPointerException` - if `from` contains some bytes and `to` is `null` (otherwise the null values are gracefully ignored)
    `IOException` - (wrapped in an `ExceptionAdapter`) if an I/O error occurs during the transfer

### dumpToString

    public String dumpToString(InputStream from)

  Returns the contents of an `InputStream` as a string. Uses the default encoding to convert bytes to characters. If `from` is `null`, an empty string is returned. This method is thread-safe.

  Parameters:
    `from` - the source input stream
  Returns:
    the string with contents of `from`
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) if an I/O error occurs during the conversion

### exec

    public Process exec(String command)
                 throws WrapperException

  Executes the command `command` through `Runtime.exec(String)` and puts the contents of `input` on the process's standard input. The created `Process` instance is returned.

  Parameters:
    `command` - the command to be executed
  Returns:
    the created `Process` object
  Throws:
    `NullPointerException` - if `command` is `null`
    `IllegalArgumentException` - if `command` is an empty string
    `WrapperException` - if an `IOException` occurs while creating the process

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD