private String trimForCsv(final StringBuilder source) {
        int n = source.length();
        final StringBuilder sb = new StringBuilder(n);
        boolean spaceEnabled = false;
        boolean spacePending = false;
        for (int i = 0; i < n; i++) {
            final char c = source.charAt(i);
            if (Character.isWhitespace(c)) {
                if (spaceEnabled) {
                    spacePending = true;
                    spaceEnabled = false;
                }
            } else {

                if (Character.isLetter(c) || Character.isDigit(c)) {
                    if (spacePending) {
                        sb.append(' ');
                        spacePending = false;
                    }
                    spaceEnabled = true;
                } else {
                    spacePending = false;
                    spaceEnabled = false;
                }
                sb.append(c);
            }
        }
        n = sb.length();
        if (n > 0 && sb.charAt(n - 1) == ' ') {
            sb.setLength(n - 1);
        }
        return sb.toString();
    }
Trims spaces from a range of characters intended for a CSV output

@param source the standard source file
@return the equivalent string with spaces removed.
src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFile.java