@Test public void testFoldOverlyLongNonWhitespace() throws Exception { String ninety = "1234567890123456789012345678901234567890" + "12345678901234567890123456789012345678901234567890"; String input = String.format("testing 1 2 %s testing %s", ninety, ninety); String expected = String.format( "testing 1 2\r\n %s\r\n testing\r\n %s", ninety, ninety); Assert.assertEquals(expected, MimeUtil.fold(input, 0)); }
public static String fold(String s, int usedCharacters) { final int maxCharacters = 76; final int length = s.length(); if (usedCharacters + length <= maxCharacters) return s; StringBuilder sb = new StringBuilder(); int lastLineBreak = -usedCharacters; int wspIdx = indexOfWsp(s, 0); while (true) { if (wspIdx == length) { sb.append(s.substring(Math.max(0, lastLineBreak))); return sb.toString(); } int nextWspIdx = indexOfWsp(s, wspIdx + 1); if (nextWspIdx - lastLineBreak > maxCharacters) { sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx)); sb.append("\r\n"); lastLineBreak = wspIdx; } wspIdx = nextWspIdx; } }
MimeUtil { public static String fold(String s, int usedCharacters) { final int maxCharacters = 76; final int length = s.length(); if (usedCharacters + length <= maxCharacters) return s; StringBuilder sb = new StringBuilder(); int lastLineBreak = -usedCharacters; int wspIdx = indexOfWsp(s, 0); while (true) { if (wspIdx == length) { sb.append(s.substring(Math.max(0, lastLineBreak))); return sb.toString(); } int nextWspIdx = indexOfWsp(s, wspIdx + 1); if (nextWspIdx - lastLineBreak > maxCharacters) { sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx)); sb.append("\r\n"); lastLineBreak = wspIdx; } wspIdx = nextWspIdx; } } }
MimeUtil { public static String fold(String s, int usedCharacters) { final int maxCharacters = 76; final int length = s.length(); if (usedCharacters + length <= maxCharacters) return s; StringBuilder sb = new StringBuilder(); int lastLineBreak = -usedCharacters; int wspIdx = indexOfWsp(s, 0); while (true) { if (wspIdx == length) { sb.append(s.substring(Math.max(0, lastLineBreak))); return sb.toString(); } int nextWspIdx = indexOfWsp(s, wspIdx + 1); if (nextWspIdx - lastLineBreak > maxCharacters) { sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx)); sb.append("\r\n"); lastLineBreak = wspIdx; } wspIdx = nextWspIdx; } } private  MimeUtil(); }
MimeUtil { public static String fold(String s, int usedCharacters) { final int maxCharacters = 76; final int length = s.length(); if (usedCharacters + length <= maxCharacters) return s; StringBuilder sb = new StringBuilder(); int lastLineBreak = -usedCharacters; int wspIdx = indexOfWsp(s, 0); while (true) { if (wspIdx == length) { sb.append(s.substring(Math.max(0, lastLineBreak))); return sb.toString(); } int nextWspIdx = indexOfWsp(s, wspIdx + 1); if (nextWspIdx - lastLineBreak > maxCharacters) { sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx)); sb.append("\r\n"); lastLineBreak = wspIdx; } wspIdx = nextWspIdx; } } private  MimeUtil(); static boolean isSameMimeType(String pType1, String pType2); static boolean isMessage(String pMimeType); static boolean isMultipart(String pMimeType); static boolean isBase64Encoding(String pTransferEncoding); static boolean isQuotedPrintableEncoded(String pTransferEncoding); static String createUniqueBoundary(); static String createUniqueMessageId(String hostName); static String formatDate(Date date, TimeZone zone); static String fold(String s, int usedCharacters); static String unfold(String s); static String unscrambleHeaderValue(String headerValue); }
MimeUtil { public static String fold(String s, int usedCharacters) { final int maxCharacters = 76; final int length = s.length(); if (usedCharacters + length <= maxCharacters) return s; StringBuilder sb = new StringBuilder(); int lastLineBreak = -usedCharacters; int wspIdx = indexOfWsp(s, 0); while (true) { if (wspIdx == length) { sb.append(s.substring(Math.max(0, lastLineBreak))); return sb.toString(); } int nextWspIdx = indexOfWsp(s, wspIdx + 1); if (nextWspIdx - lastLineBreak > maxCharacters) { sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx)); sb.append("\r\n"); lastLineBreak = wspIdx; } wspIdx = nextWspIdx; } } private  MimeUtil(); static boolean isSameMimeType(String pType1, String pType2); static boolean isMessage(String pMimeType); static boolean isMultipart(String pMimeType); static boolean isBase64Encoding(String pTransferEncoding); static boolean isQuotedPrintableEncoded(String pTransferEncoding); static String createUniqueBoundary(); static String createUniqueMessageId(String hostName); static String formatDate(Date date, TimeZone zone); static String fold(String s, int usedCharacters); static String unfold(String s); static String unscrambleHeaderValue(String headerValue); static final String ENC_QUOTED_PRINTABLE; static final String ENC_BINARY; static final String ENC_BASE64; static final String ENC_8BIT; static final String ENC_7BIT; }