public static String deflow(String text,boolean delSp){
  String[] lines=text.split("\r\n|\n",-1);
  StringBuffer result=null;
  StringBuffer resultLine=new StringBuffer();
  int resultLineQuoteDepth=0;
  boolean resultLineFlowed=false;
  for (int i=0; i <= lines.length; i++) {
    String line=i < lines.length ? lines[i] : null;
    int actualQuoteDepth=0;
    if (line != null && line.length() > 0) {
      if (line.equals(RFC2646_SIGNATURE))       resultLineFlowed=false;
 else       if (line.charAt(0) == RFC2646_QUOTE) {
        actualQuoteDepth=1;
        while (actualQuoteDepth < line.length() && line.charAt(actualQuoteDepth) == RFC2646_QUOTE)         actualQuoteDepth++;
        if (resultLineQuoteDepth != actualQuoteDepth)         resultLineFlowed=false;
        line=line.substring(actualQuoteDepth);
      }
 else {
        if (resultLineQuoteDepth > 0)         resultLineFlowed=false;
      }
      if (line.length() > 0 && line.charAt(0) == RFC2646_SPACE)       line=line.substring(1);
    }
 else     if (line == null)     resultLineFlowed=false;
    if (!resultLineFlowed && i > 0) {
      if (resultLineQuoteDepth > 0)       resultLine.insert(0,RFC2646_SPACE);
      for (int j=0; j < resultLineQuoteDepth; j++)       resultLine.insert(0,RFC2646_QUOTE);
      if (result == null)       result=new StringBuffer();
 else       result.append(RFC2646_CRLF);
      result.append(resultLine.toString());
      resultLine=new StringBuffer();
      resultLineFlowed=false;
    }
    resultLineQuoteDepth=actualQuoteDepth;
    if (line != null) {
      if (!line.equals(RFC2646_SIGNATURE) && line.endsWith("" + RFC2646_SPACE) && i < lines.length - 1) {
        if (delSp)         line=line.substring(0,line.length() - 1);
        resultLineFlowed=true;
      }
 else       resultLineFlowed=false;
      resultLine.append(line);
    }
  }
  return result.toString();
}

public static String deflow ( String text , boolean delSp ) { String [ ] lines = text . split ( "\r\n|\n" , - 1 ) ; StringBuffer result = null ; StringBuffer resultLine = new StringBuffer ( ) ; int resultLineQuoteDepth = 0 ; boolean resultLineFlowed = false ; for ( int i = 0 ; i <= lines . length ; i ++ ) { String line = i < lines . length ? lines [ i ] : null ; int actualQuoteDepth = 0 ; if ( line != null && line . length ( ) > 0 ) { if ( line . equals ( RFC2646_SIGNATURE ) ) resultLineFlowed = false ; else if ( line . charAt ( 0 ) == RFC2646_QUOTE ) { actualQuoteDepth = 1 ; while ( actualQuoteDepth < line . length ( ) && line . charAt ( actualQuoteDepth ) == RFC2646_QUOTE ) actualQuoteDepth ++ ; if ( resultLineQuoteDepth != actualQuoteDepth ) resultLineFlowed = false ; line = line . substring ( actualQuoteDepth ) ; } else { if ( resultLineQuoteDepth > 0 ) resultLineFlowed = false ; } if ( line . length ( ) > 0 && line . charAt ( 0 ) == RFC2646_SPACE ) line = line . substring ( 1 ) ; } else if ( line == null ) resultLineFlowed = false ; if ( ! resultLineFlowed && i > 0 ) { if ( resultLineQuoteDepth > 0 ) resultLine . insert ( 0 , RFC2646_SPACE ) ; for ( int j = 0 ; j < resultLineQuoteDepth ; j ++ ) resultLine . insert ( 0 , RFC2646_QUOTE ) ; if ( result == null ) result = new StringBuffer ( ) ; else result . append ( RFC2646_CRLF ) ; result . append ( resultLine . toString ( ) ) ; resultLine = new StringBuffer ( ) ; resultLineFlowed = false ; } resultLineQuoteDepth = actualQuoteDepth ; if ( line != null ) { if ( ! line . equals ( RFC2646_SIGNATURE ) && line . endsWith ( "" + RFC2646_SPACE ) && i < lines . length - 1 ) { if ( delSp ) line = line . substring ( 0 , line . length ( ) - 1 ) ; resultLineFlowed = true ; } else resultLineFlowed = false ; resultLine . append ( line ) ; } } return result . toString ( ) ; }
decodes a text previously wrapped using " format = flowed " .
