📄 reader.java
字号:
package com.ismyway.anyview.others;
import java.util.Calendar;
import javax.microedition.lcdui.Command;
import com.ismyway.anyview.win.About;
import com.ismyway.anyview.win.Welcome;
import com.ismyway.fairyui.Component;
import com.ismyway.fairyui.Panel;
import com.ismyway.util.ArrayList;
import com.ismyway.util.FileSystemReader;
import com.ismyway.util.Res;
import com.ismyway.util.TxtLine;
public abstract class Reader extends Panel {
/**
* 文件访问对象
*/
protected FileSystemReader fsa;
/**
* 当前视图情况下可以显示的行数
*/
protected int lineOfPage = 0;
/**
* 当前起始行
*/
protected int currentLine = 0;
/**
* 当前视图内首行的偏移
*/
protected int currentOffset = 0;
/**
* 当前缓冲区的开始在文件中的位置
*/
//private int bufferStartOffset = 0;
/**
* 分析后的文件行信息
*/
protected TxtLine[] mylines; //
/**
* 16K的缓冲长度
*/
//protected int BufferLenght = 16384;
public static int bufferLength = 4096;
/**
* 文件缓冲
*/
protected byte[] bufferTxt; //
/**
* 保存缓冲区尾部数据的缓冲
*/
protected byte[] bufferbuffer = new byte[0];
/**
* 是否是文档的最后一页
*/
protected boolean eof = false;
/**
* 是否是文档的最前一页
*/
protected boolean bof = true;
/**
* 保存过度的颜色
*/
protected int[] colors;
/**
* 是否加载成功
*/
protected boolean readSuccess = true;
/**
* 错误信息
*/
protected String errorMsg = About.ver;
/**
* 编码方式
* 1:GB2312
* 2:UNICODE
* 3:UTF-8
*/
protected byte encodeing = 1;
public final static byte GB2312 = 1;
public final static byte UNICODE = 2;
public final static byte UTF8 = 3;
protected String fullpath;
protected ArrayList lists;
protected int paddintLeft, readerWidth;
protected int[] pagePadding = new int[2];
protected boolean flushScreen = false;
protected long lastTime = System.currentTimeMillis();
public static Reader instance = null;
public Reader() {
}
public Reader(ArrayList lists, String currentFile, byte encode) {
this.lists = lists;
this.fullpath = currentFile;
this.encodeing = encode;
/*for (int i = 0;i < lists.size(); i++) {
String s = (String)lists.get(i);
System.out.println(s);
}
System.out.println("currentFile: " + currentFile);*/
pagePadding[0] = (Configure.padding >> 8) & 0xff; //左
pagePadding[1] = Configure.padding & 0xff; //右
pagePadding[0] = pagePadding[0] > 8 ? 8 : pagePadding[0];
pagePadding[1] = pagePadding[1] > 8 ? 8 : pagePadding[1];
paddintLeft = pagePadding[0];
//System.out.println(paddintLeft + ", " + pagePadding[0] + ", " + pagePadding[1]);
readerWidth = mainCanvas.getWidth() - pagePadding[0] - pagePadding[1];
}
protected void reopen(String currentFile, byte encode) {
this.fullpath = currentFile;
this.encodeing = encode;
instance = this;
}
public final static int[] getTransColors(int c1, int c2, int step) {
int[] colors = new int[step];
int r1, g1, b1, r2, g2, b2, sr, sg, sb, r, g, b;
r1 = c1 >> 16;
g1 = (c1 >> 8) & 0xff;
b1 = c1 & 0xff;
r2 = c2 >> 16;
g2 = (c2 >> 8) & 0xff;
b2 = c2 & 0xff;
sr = (r1 - r2) / (step - 1);
sg = (g1 - g2) / (step - 1);
sb = (b1 - b2) / (step - 1);
for (int i = 1; i < step - 1; i++) {
r = r1 - sr * i;
g = g1 - sg * i;
b = b1 - sb * i;
colors[i] = r << 16 | g << 8 | b;
}
colors[0] = c1;
colors[step - 1] = c2;
return colors;
}
public boolean keyRepeated(int key) {
return keyReleased(key);
}
public boolean keyReleased(int key) {
//System.out.println("panel key: " + key + ", hotpots = " + hotpots.size());
// if (rows.size() < 1) {
// return true;
// }
key = Settings.mapKey(key, true);
boolean repaint = false;
if (key == Settings.VKEY_UP || key == Settings.VKEY_NUM2) { //上
VKey_Up();
repaint = true;
} else if (key == Settings.VKEY_DOWN || key == Settings.VKEY_NUM8) { //下
VKey_Down();
repaint = true;
} else if (key == Settings.VKEY_FIRE || key == Settings.VKEY_NUM5) {
VKey_Fire();
repaint = true;
} else if (key == Settings.VKEY_LEFT || key == Settings.VKEY_NUM4 || key == Settings.VKEY_USER1) {
VKey_Left();
repaint = true;
} else if (key == Settings.VKEY_RIGHT || key == Settings.VKEY_NUM6 || key == Settings.VKEY_USER2) {
VKey_Right();
repaint = true;
} else if (key == Settings.VKEY_LSOFT || key == Settings.VKEY_STAR) {
if (getCommandListener() != null && leftCmd != null) {
if (null == leftPopup) {
getCommandListener().commandAction(leftCmd, null);
repaint = true;
} else {
mainCanvas.showPopup(leftPopup, Settings.ANIMATE_UP);
}
}
} else if (key == Settings.VKEY_RSOFT || key == Settings.VKEY_POUND) {
if (getCommandListener() != null && rightCmd != null) {
if (null == rightPopup) {
getCommandListener().commandAction(rightCmd, null);
repaint = true;
} else {
mainCanvas.showPopup(rightPopup, Settings.ANIMATE_UP);
}
}
} else if (key == Settings.VKEY_NUM1) {
VKey_Num1();
} else if (key == Settings.VKEY_NUM3) {
VKey_Num3();
} else if (key == Settings.VKEY_NUM7) {
VKey_Num7();
} else if (key == Settings.VKEY_NUM9) {
VKey_Num9();
} else if (key == Settings.VKEY_NUM0) {
VKey_Num0();
}
return repaint;
}
abstract public void VKey_Up();
abstract public void VKey_Down();
abstract public void VKey_Left();
abstract public void VKey_Right();
abstract public void VKey_Fire();
public void VKey_Num1() {
}
public void VKey_Num3() {
}
public void VKey_Num7() {
}
public void VKey_Num9() {
}
public void VKey_Num0() {
}
public void callback(Component component, Command cmd) {
String func = cmd.getLabel();
if (func.equals(Res.get("Close"))) {
close();
mainCanvas.setCurrent(new Welcome());
} else if (func.equals(Res.get("Delete"))) {
} else if (func.equals(Res.get("Open"))) {
} else if (func.equals(Res.get("UTF-8"))) {
} else if (func.equals(Res.get("UNICODE"))) {
} else if (func.equals(Res.get("GB2312"))) {
}
}
public boolean isAnimated() {
return true;
}
public boolean clock() {
boolean repaint = super.clock();
if (repaint) {
return repaint;
}
if (System.currentTimeMillis() - lastTime < 1000L) {
return false;
}
lastTime = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR);
int min = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);
int pm = calendar.get(Calendar.AM_PM);
hour = (pm == 1) ? hour + 12 : hour;
StringBuffer sb = new StringBuffer();
sb.append((hour < 10) ? "0" : "");
sb.append(hour).append(":");
sb.append((min < 10) ? "0" : "").append(min);
sb.append(":").append((sec < 10) ? "0" : "").append(sec);
String str = sb.toString();
if (Configure.showPercent && getPercent() > -1) {
str = getPercent() + "% " + sb.toString();
} else {
str = sb.toString();
}
if (Configure.showMemory) {
rightString = str + " | " + (Runtime.getRuntime().freeMemory() >> 10) + "k";
} else {
rightString = str;
}
return true;
}
public int getPercent() {
return -1;
}
protected static String byte2unicode(byte[] b) {
StringBuffer sb = new StringBuffer();
int index = 0;
while (null != b) {
int v1 = b[index] & 0xff;
if (index + 1 >= b.length) {
break;
}
int v2 = b[index + 1] & 0xff;
char ch = (char) (v1 + (v2 << 8));
sb.append(ch);
index += 2;
if (index >= b.length) {
break;
}
}
return sb.toString();
}
protected static String byte2utf8(byte[] b) {
String str = "";
int start = 0;
byte[] abytes = new byte[b.length - start];
System.arraycopy(b, start, abytes, 0, abytes.length);
try {
str = new String(abytes, "UTF-8");
abytes = null;
} catch (Exception ex) {
abytes = null;
//str = Res.get("decode error");
str = ex.toString();
}
return str;
}
public boolean pointerReleased(int x, int y) {
if (super.pointerReleased(x, y)) {
return true;
}
if (showBottom && (y > getHeight() - getBottomHeight() || lastPointerY > getHeight() - getBottomHeight())) {
return false;
}
int disy = lastPointerY - y;
int disx = lastPointerX - x;
if (disx * disx + disy * disy > Configure.slipDistance * Configure.slipDistance
|| System.currentTimeMillis() - pointerPressedtime > 2000) {
if (null != leftPopup) {
int sx = mainCanvas.getWidth() - leftPopup.getWidth();
int sy = mainCanvas.getHeight() - leftPopup.getHeight();
sx = x > sx ? sx : x;
sy = y > sy ? sy : y;
leftPopup.setLocation(sx, sy);
mainCanvas.showPopup(leftPopup, Settings.ANIMATE_UP);
return true;
}
}
if (lastPointerY < getHeight() / 2) {
if (y < getHeight() / 2) {
VKey_Left();
return true;
}
} else {
if (y > getHeight() / 2) {
VKey_Right();
return true;
}
}
return false;
}
public final static String memory() {
long totalMemory = Runtime.getRuntime().totalMemory();
long freeMemory = Runtime.getRuntime().freeMemory();
return (freeMemory / 1024) + "k/" + (totalMemory / 1024) + "k";
}
public void close() {
instance = null;
}
public FileSystemReader getFileSystemReader() {
return fsa;
}
/**
* 保存阅读记录
*/
abstract public void makeSnap();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -