/**
   * Set the current buffer's content to the specified {@link String}. The visual console will be
   * modified to show the current buffer.
   *
   * @param buffer the new contents of the buffer.
   */
  private final void setBuffer(final String buffer) throws IOException {
    // don't bother modifying it if it is unchanged
    if (buffer.equals(buf.buffer.toString())) {
      return;
    }

    // obtain the difference between the current buffer and the new one
    int sameIndex = 0;

    for (int i = 0, l1 = buffer.length(), l2 = buf.buffer.length(); (i < l1) && (i < l2); i++) {
      if (buffer.charAt(i) == buf.buffer.charAt(i)) {
        sameIndex++;
      } else {
        break;
      }
    }

    int diff = buf.buffer.length() - sameIndex;

    backspace(diff); // go back for the differences
    killLine(); // clear to the end of the line
    buf.buffer.setLength(sameIndex); // the new length
    putString(buffer.substring(sameIndex)); // append the differences
  }
  /**
   * Paste the contents of the clipboard into the console buffer
   *
   * @return true if clipboard contents pasted
   */
  public boolean paste() throws IOException {
    Clipboard clipboard;
    try { // May throw ugly exception on system without X
      clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    } catch (Exception e) {
      return false;
    }

    if (clipboard == null) {
      return false;
    }

    Transferable transferable = clipboard.getContents(null);

    if (transferable == null) {
      return false;
    }

    try {
      Object content = transferable.getTransferData(DataFlavor.plainTextFlavor);

      /*
       * This fix was suggested in bug #1060649 at
       * http://sourceforge.net/tracker/index.php?func=detail&aid=1060649&group_id=64033&atid=506056
       * to get around the deprecated DataFlavor.plainTextFlavor, but it
       * raises a UnsupportedFlavorException on Mac OS X
       */
      if (content == null) {
        try {
          content = new DataFlavor().getReaderForText(transferable);
        } catch (Exception e) {
        }
      }

      if (content == null) {
        return false;
      }

      String value;

      if (content instanceof Reader) {
        // TODO: we might want instead connect to the input stream
        // so we can interpret individual lines
        value = "";

        String line = null;

        for (BufferedReader read = new BufferedReader((Reader) content);
            (line = read.readLine()) != null; ) {
          if (value.length() > 0) {
            value += "\n";
          }

          value += line;
        }
      } else {
        value = content.toString();
      }

      if (value == null) {
        return true;
      }

      putString(value);

      return true;
    } catch (UnsupportedFlavorException ufe) {
      if (debugger != null) debug(ufe + "");

      return false;
    }
  }