author | Zhiwei Yan <zhiwei.yan@amlogic.com> | 2016-11-25 12:39:41 (GMT) |
---|---|---|
committer | Tellen Yu <tellen.yu@amlogic.com> | 2016-12-01 10:23:03 (GMT) |
commit | d59ecada5558944880dd97f0af88e710506bf3ef (patch) | |
tree | bed97dcdfabccf6d2d6d3382a0183cd01f0e9c62 | |
parent | eea7a037eb0efeb1f8e956c80b817ee8fee7a708 (diff) | |
download | SubTitle-d59ecada5558944880dd97f0af88e710506bf3ef.zip SubTitle-d59ecada5558944880dd97f0af88e710506bf3ef.tar.gz SubTitle-d59ecada5558944880dd97f0af88e710506bf3ef.tar.bz2 |
pd#135340:inspect whether subtitle decoded by iso88591 or GBK is correct, if GBK has messy code,then use iso88591 to decode while iso88591 is nomal
Change-Id: Ic913fb5b2b4fc98f259bfa1b8346c0eeaa27310c
-rw-r--r-- | src/com/subtitleparser/SubtitleFile.java | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/com/subtitleparser/SubtitleFile.java b/src/com/subtitleparser/SubtitleFile.java index efbe22c..5d7941e 100644 --- a/src/com/subtitleparser/SubtitleFile.java +++ b/src/com/subtitleparser/SubtitleFile.java @@ -295,6 +295,40 @@ public class SubtitleFile extends LinkedList { Log.d ("subtitleFile", "appendSubtitleFile" + index); } + public String bytesToHexString(byte[] bArray) { + StringBuffer sb = new StringBuffer(bArray.length); + String sTemp; + for (int i = 0; i < bArray.length; i++) { + sTemp = Integer.toHexString(0xFF & bArray[i]); + if (sTemp.length() < 2) + sb.append(0); + sb.append(sTemp.toUpperCase()); + } + return sb.toString(); + } + + public String stringToHexString(String s) { + StringBuffer sb = new StringBuffer(s.length()); + String sTemp; + for (int i = 0; i < s.length(); i++) { + sTemp = Integer.toHexString(s.charAt(i)); + if (sTemp.length() < 2) + sb.append(0); + sb.append(sTemp.toUpperCase()); + } + return sb.toString(); + } + public boolean JudgeGBK(byte[] bArray) { + boolean hasGBK = false; + for (int i = 0; i < bArray.length-1; i++) { + int bArray1 = bArray[i] & 0xff; + int bArray2 = bArray[i+1]& 0xff; + if (bArray1 > 128 && bArray1 <255 && bArray2 > 63 && bArray2 <255 && bArray2 != 127) { + hasGBK = true; + } + } + return hasGBK; + } public void appendSubtitle (int index, int start, int end, byte[] bytearray, String encode1) { //public int appendSubtitle(int index) { String encode = encode1; @@ -305,6 +339,24 @@ public class SubtitleFile extends LinkedList { SubtitleTime startTime = null; SubtitleTime endTime = null; String text = null; + String texttemp = ""; + String ISO88591S = "iso88591"; + String GBKS = "GBK"; + String temp1 = ""; + String temp3 = ""; + if (encode.equals(GBKS) && !JudgeGBK(bytearray)) { + texttemp = bytesToHexString(bytearray); + try { + temp1 = new String(bytearray,ISO88591S); + } catch (UnsupportedEncodingException e) { + Log.e("subtitleFile", "erro while decodeing with iso88591"); + } + temp3 = stringToHexString(temp1); + if (texttemp.equals(temp3)) { + encode = ISO88591S; + } + } + try { text = new String (bytearray, encode); } |