package com.kunfei.bookshelf.utils;
import androidx.annotation.NonNull;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import static android.text.TextUtils.isEmpty;
/**
* <Detect encoding .> Copyright (C) <2009> <Fluck,ACC http://androidos.cc/dev>
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* <p>
* EncodingDetect.java<br>
* 自动获取文件的编码
*
* @author Billows.Van
* @version 1.0
* @since Create on 2010-01-27 11:19:00
*/
public class EncodingDetect {
public static String getEncodeInHtml(@NonNull byte[] bytes) {
try {
String charsetStr = "UTF-8";
Document doc = Jsoup.parse(new String(bytes, charsetStr));
int a = doc.childNode(0).toString().indexOf("encoding");
if (a > 0) {
String e = doc.childNode(0).toString().substring(a);
int b = e.indexOf('"');
int c = e.indexOf('"', b + 1);
return e.substring(b + 1, c);
}
Elements metaTags = doc.getElementsByTag("meta");
for (Element metaTag : metaTags) {
String content = metaTag.attr("content");
String http_equiv = metaTag.attr("http-equiv");
charsetStr = metaTag.attr("charset");
if (!charsetStr.isEmpty()) {
if (!isEmpty(charsetStr)) {
return charsetStr;
}
}
if (http_equiv.toLowerCase().equals("content-type")) {
if (content.toLowerCase().contains("charset")) {
charsetStr = content.substring(content.toLowerCase().indexOf("charset") + "charset=".length());
} else {
charsetStr = content.substring(content.toLowerCase().indexOf(";") + 1);
}
if (!isEmpty(charsetStr)) {
return charsetStr;
}
}
}
} catch (Exception ignored) {
}
return getJavaEncode(bytes);
}
public static String getJavaEncode(@NonNull byte[] bytes) {
int len = bytes.length > 2000 ? 2000 : bytes.length;
byte[] cBytes = new byte[len];
System.arraycopy(bytes, 0, cBytes, 0, len);
BytesEncodingDetect bytesEncodingDetect = new BytesEncodingDetect();
String code = BytesEncodingDetect.javaname[bytesEncodingDetect.detectEncoding(cBytes)];
// UTF-16LE 特殊处理
if ("Unicode".equals(code)) {
if (cBytes[0] == -1) {
code = "UTF-16LE";
}
}
return code;
}
/**
* 得到文件的编码
*/
public static String getJavaEncode(@NonNull String filePath) {
BytesEncodingDetect s = new BytesEncodingDetect();
String fileCode = BytesEncodingDetect.javaname[s
.detectEncoding(new File(filePath))];
// UTF-16LE 特殊处理
if ("Unicode".equals(fileCode)) {
byte[] tempByte = BytesEncodingDetect.getFileBytes(new File(
filePath));
if (tempByte[0] == -1) {
fileCode = "UTF-16LE";
}
}
return fileCode;
}
/**
* 得到文件的编码
*/
public static String getJavaEncode(@NonNull File file) {
BytesEncodingDetect s = new BytesEncodingDetect();
String fileCode = BytesEncodingDetect.javaname[s.detectEncoding(file)];
// UTF-16LE 特殊处理
if ("Unicode".equals(fileCode)) {
byte[] tempByte = BytesEncodingDetect.getFileBytes(file);
if (tempByte[0] == -1) {
fileCode = "UTF-16LE";
}
}
return fileCode;
}
}
class BytesEncodingDetect extends Encoding {
// Frequency tables to hold the GB, Big5, and EUC-TW character
// frequencies
private int[][] GBFreq;
private int[][] GBKFreq;
private int[][] Big5Freq;
private int[][] Big5PFreq;
private int[][] EUC_TWFreq;
private int[][] KRFreq;
private int[][] JPFreq;
// int UnicodeFreq[94][128];
// public static String[] nicename;
// public static String[] codings;
public boolean debug;
public BytesEncodingDetect() {
super();
debug = false;
GBFreq = new int[94][94];
GBKFreq = new int[126][191];
Big5Freq = new int[94][158];
Big5PFreq = new int[126][191];
EUC_TWFreq = new int[94][94];
KRFreq = new int[94][94];
JPFreq = new int[94][94];
// Initialize the Frequency Table for GB, GBK, Big5, EUC-TW, KR, JP
initialize_frequencies();
}
/**
* Function : detectEncoding Aruguments: URL Returns : One of the encodings
* from the Encoding enumeration (GB2312, HZ, BIG5, EUC_TW, ASCII, or OTHER)
* Description: This function looks at the URL contents and assigns it a
* probability score for each encoding type. The encoding type with the
* highest probability is returned.
*/
public int detectEncoding(URL testurl) {
byte[] rawtext = new byte[10000];
int bytesread = 0, byteoffset = 0;
int guess = OTHER;
InputStream chinesestream;
try {
chinesestream = testurl.openStream();
while ((bytesread = chinesestream.read(rawtext, byteoffset,
rawtext.length - byteoffset)) > 0) {
byteoffset += bytesread;
}
;
chinesestream.close();
guess = detectEncoding(rawtext);
} catch (Exception e) {
System.err.println("Error loading or using URL " + e.toString());
guess = -1;
}
return guess;
}
/**
* Function : detectEncoding Aruguments: File Returns : One of the encodings
* from the Encoding enumeration (GB2312, HZ, BIG5, EUC_TW, ASCII, or OTHER)
* Description: This function looks at the file and assigns it a probability
* score for each encoding type. The encoding type with the highest
* probability is returned.
*/
public int detectEncoding(File testfile) {
byte[] rawtext = getFileBytes(testfile);
return detectEncoding(rawtext);
}
public static byte[] getFileBytes(File testfile) {
FileInputStream chinesefile;
byte[] rawtext;
rawtext = new byte[2000];
try {
chinesefile = new FileInputStream(testfile);
chinesefile.read(rawtext);
chinesefile.close();
} catch (Exception e) {
System.err.println("Error: " + e);
}
return rawtext;
}
/**
* Function : detectEncoding Aruguments: byte array Returns : One of the
* encodings from the Encoding enumeration (GB2312, HZ, BIG5, EUC_TW, ASCII,
* or OTHER) Description: This function looks at the byte array and assigns
* it a probability score for each encoding type. The encoding type with the
* highest probability is returned.
*/
public int detectEncoding(byte[] rawtext) {
int[] scores;
int index, maxscore = 0;
int encoding_guess = OTHER;
scores = new int[TOTALTYPES];
// Assign Scores
scores[GB2312] = gb2312_probability(rawtext);
sco
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本项目是一款结合Java和Kotlin语言的MyBookshelf应用程序,汇集了共计800个文件,其中Java源代码文件占330个,Kotlin文件91个,CSS、HTML和JavaScript等前端技术文件占一定比例,辅以44个PNG图片和18个JPG图片文件,以及少量配置和文档文件。项目结构完整,适用于学习前端技术整合实践,是软件开发者了解混合编程语言与前端技术融合的优秀案例。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java和Kotlin的MyBookshelf项目设计源码与前端技术整合实践 (797个子文件)
gradlew.bat 2KB
bookshelf.css 3KB
index.css 2KB
uFrmEditSource.dfm 19KB
uFrmMain.dfm 14KB
uFrmReplaceGroup.dfm 2KB
uFrmWait.dfm 1KB
BookSourceMgr.dpr 515B
BookSourceMgr.dproj 33KB
.gitignore 195B
.gitignore 48B
.gitignore 7B
build.gradle 7KB
build.gradle 1KB
build.gradle 690B
settings.gradle 30B
gradlew 5KB
index.html 12KB
bookshelf.html 1KB
Main.ico 22KB
favicon.ico 4KB
gradle-wrapper.jar 52KB
EncodingDetect.java 171KB
ReadBookActivity.java 79KB
PageLoader.java 70KB
XmlUtils.java 55KB
TimeUtils.java 52KB
ChapterContentHelp.java 50KB
SourceEditActivity.java 33KB
BookSourceBean.java 31KB
ReadAloudService.java 30KB
ReadBookControl.java 26KB
FilePickerIcon.java 26KB
MainActivity.java 25KB
ACache.java 25KB
FileUtils.java 23KB
PageView.java 23KB
FastScroller.java 21KB
SimulationPageAnim.java 21KB
ConvertUtils.java 21KB
BookDetailActivity.java 21KB
BookList.java 20KB
SearchBookActivity.java 20KB
ReadBookPresenter.java 20KB
TintHelper.java 19KB
BookChapterList.java 19KB
StringUtils.java 18KB
AnalyzeRule.java 18KB
ChangeSourceDialog.java 18KB
BookshelfHelp.java 18KB
BookSourceActivity.java 17KB
AnalyzeByJSoup.java 16KB
Selector.java 16KB
PageLoaderText.java 16KB
BitmapUtil.java 15KB
ReadStyleActivity.java 15KB
ZipUtils.java 15KB
DocumentUtil.java 15KB
RefreshRecyclerView.java 14KB
AnalyzeByRegex.java 13KB
ConfirmPopup.java 13KB
SmoothCheckBox.java 13KB
FastXmlSerializer.java 13KB
FindBookFragment.java 12KB
DownloadService.java 12KB
BookDetailPresenter.java 12KB
PageLoaderEpub.java 12KB
AnalyzeUrl.java 11KB
SearchBookModel.java 11KB
ReplaceRuleActivity.java 11KB
BookListFragment.java 11KB
MainPresenter.java 11KB
BaseExpandableRecyclerAdapter.java 11KB
BookSourceManager.java 11KB
HorizontalListView.java 11KB
ThemeStore.java 10KB
WebBook.java 10KB
PageLoaderNet.java 10KB
SearchBookPresenter.java 10KB
BookSource3Bean.java 10KB
GridSpacingItemDecoration.java 10KB
BookListPresenter.java 10KB
BookSourcePresenter.java 10KB
Debug.java 10KB
ChapterProvider.java 9KB
BaseModelImpl.java 9KB
SearchBookAdapter.java 9KB
DownloadTaskImpl.java 9KB
MoDialogHUD.java 9KB
BookShelfBean.java 9KB
ImportBookActivity.java 9KB
BookShelfListAdapter.java 9KB
MBaseActivity.java 9KB
BookCoverEditActivity.java 8KB
BasicPopup.java 8KB
ThemeSettingsFragment.java 8KB
UpLastChapterModel.java 8KB
ReplaceRuleManager.java 8KB
SectionedRecyclerViewAdapter.java 8KB
AboutActivity.java 8KB
共 797 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
csbysj2020
- 粉丝: 2639
- 资源: 5504
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功