private List<String> wrapLines(String line, final int maxLength) {
    	List<String> rv = new ArrayList<String>();
        for (String restOfLine : line.split("\\n")) {
            while (restOfLine.length() > maxLength) {
                // try to wrap at space, but don't try too hard as some languages don't even have whitespaces.
                int lineLength;
                String candidate = restOfLine.substring(0, maxLength);
                int sp=candidate.lastIndexOf(' ');
                if(sp>maxLength*3/5)    lineLength=sp;
                else                    lineLength=maxLength;
                rv.add(restOfLine.substring(0, lineLength));
                restOfLine = restOfLine.substring(lineLength).trim();
            }
            rv.add(restOfLine);
        }
    	return rv;
    }
private List < String > wrapLines ( String line , final int maxLength ) { List < String > rv = new ArrayList < String > ( ) ; for ( String restOfLine : line . split ( "\\n" ) ) { while ( restOfLine . length ( ) > maxLength ) { int lineLength ; String candidate = restOfLine . substring ( 0 , maxLength ) ; int sp = candidate . lastIndexOf ( ' ' ) ; if ( sp > maxLength * 3 / 5 ) lineLength = sp ; else lineLength = maxLength ; rv . add ( restOfLine . substring ( 0 , lineLength ) ) ; restOfLine = restOfLine . substring ( lineLength ) . trim ( ) ; } rv . add ( restOfLine ) ; } return rv ; }
Wraps a line so that the resulting parts are not longer than a given maximum length.

@param line       Line to wrap
@param maxLength  maximum length for the resulting parts
@return list of all wrapped parts