-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
MimeHeaderEncoder.kt
34 lines (26 loc) · 1.21 KB
/
MimeHeaderEncoder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.fsck.k9.mail.internet
import org.apache.james.mime4j.util.MimeUtil
object MimeHeaderEncoder {
@JvmStatic
fun encode(name: String, value: String): String {
// TODO: Fold long text that provides enough opportunities for folding and doesn't contain any characters that
// need to be encoded.
// Number of characters already used up on the first line (header field name + colon + space)
val usedCharacters = name.length + COLON_PLUS_SPACE_LENGTH
return if (hasToBeEncoded(value, usedCharacters)) {
MimeUtil.fold(EncoderUtil.encodeEncodedWord(value), usedCharacters)
} else {
value
}
}
private fun hasToBeEncoded(value: String, usedCharacters: Int): Boolean {
return exceedsRecommendedLineLength(value, usedCharacters) || charactersNeedEncoding(value)
}
private fun exceedsRecommendedLineLength(value: String, usedCharacters: Int): Boolean {
return usedCharacters + value.length > RECOMMENDED_MAX_LINE_LENGTH
}
private fun charactersNeedEncoding(text: String): Boolean {
return text.any { !it.isVChar() && !it.isWspOrCrlf() }
}
private const val COLON_PLUS_SPACE_LENGTH = 2
}