private static void appendNewlineIfNecessary(StringBuffer buf, Writer out) throws IOException {
    int len = buf.length();

    if ((len != 0) && (buf.charAt(len - 1) != '\n')) {
      out.write('\n');
    }
  }
Appends a newline to the given buffer via the given writer, but only if it isn't empty and
doesn't already end with one.

@param buf {@code non-null;} the buffer in question
@param out {@code non-null;} the writer to use
android/android_framework/dex-maker/src/main/java/com/android/dx/util/TwoColumnOutput.java