mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 14:05:52 +01:00
Add encoding detection
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package util
|
||||
|
||||
import java.net.{URLDecoder, URLEncoder}
|
||||
import org.mozilla.universalchardet.UniversalDetector
|
||||
|
||||
object StringUtil {
|
||||
|
||||
@@ -25,4 +26,15 @@ object StringUtil {
|
||||
def escapeHtml(value: String): String =
|
||||
value.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """)
|
||||
|
||||
def convertFromByteArray(content: Array[Byte]): String = new String(content, detectEncoding(content))
|
||||
|
||||
def detectEncoding(content: Array[Byte]): String = {
|
||||
val detector = new UniversalDetector(null)
|
||||
detector.handleData(content, 0, content.length)
|
||||
detector.dataEnd()
|
||||
detector.getDetectedCharset match {
|
||||
case null => "UTF-8"
|
||||
case e => e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user