package com.ismyway.anyview;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import java.util.Vector;
//import java.util.Stack;
import java.util.Calendar;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.*;
/**
* <p>Title: AnyView</p>
*
* <p>Description: E680(I) Reader</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: www.ismyway.com</p>
*
* @author ZhangJian
* @version 1.0
*/
public class TXTReaderCanvas extends Canvas {
private int ANCHOR = Graphics.LEFT | Graphics.TOP;
private int srcW, srcH;
private String filename;
private boolean rotate = false;
private FileSystemReader fsa;
private Image background = null;
private int txtLenght = 0; //当前文件文件的长度
int lineOfPage = 0; //当前视图情况下可以显示的行数
int viewWidth, viewHeight; //可视区域的高与宽
Image bufferImage; //缓冲区图像
int currentLine = 0; //当前起始行
private int currentOffset = 0; //当前视图内首行的偏移
//private int bufferStartOffset = 0; //当前缓冲区的开始在文件中的位置
Vector mylines = new Vector(); //保存每行的起始偏移
private int BufferLenght = 65536; //64K的缓冲长度
private byte[] bufferTxt; //文件缓冲
private byte[] bufferbuffer = new byte[0]; //保存缓冲区尾部数据的缓冲
private boolean eof = false; //是否是文档的最后一页
private boolean bof = true; //是否是文档的最前一页
//private Vector history = new Vector(); //当前阅读文档的返回点
//private int historyIndex = -1; //返回点的索引
private BookMark[] allBookMarks = null; //所有的书签
private Vector currentMarks = new Vector(); //当前书签中的所有书签项
private int currentBookMarkIndex = -1; //当前的书签
private int pressedx, pressedy, releasex, releasey;
private Vector finds = new Vector();
private Menu menu = null;
private boolean showMenu = false;
private AbstractWindow currentWindow = null;
private Selector selector = null;
private String[] menuitems = {"阅读信息", "选择书签", "添加书签", "删除书签", "自由跳转",
"分享片断", "搜 索", "返回菜单", "退出程序"};
Message message = new Message(); //提示消息
Image serachImage = null;
private boolean needsearch = false;
private String searchStr = "";
private int searchStrLenght = 0;
/*public final static int GB2312 = 0;
public final static int UTF8 = 1;
public final static int UNICODE = 2;
//private int txtCharSet = GB2312;*/
protected TXTReaderCanvas(String filename) {
this.filename = filename;
this.setFullScreenMode(true);
srcW = getWidth();
srcH = getHeight();
AnyView.cf.setColor(ColorSelector.colors[AnyView.frontColor]);
rotate = AnyView.readType == 1;
prepareBackGround();
rotate(rotate);
fsa = new FileSystemReader(filename);
/*byte[] b = fsa.read(4);
if (null != b || b.length > 2) {
if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
txtCharSet = UTF8;
} else if (b[0] == -1 && b[1] == -2 || b[0] == 72 && b[1] == 66) {
txtCharSet = UNICODE;
}
}
fsa.close();
fsa.open();*/
txtLenght = (int) fsa.fileSize();
openRec(); //读取老的记录
bof = currentOffset == 0;
fsa.skip(currentOffset);
readNextBuffer();
analysisBuffer();
prepareNextImage();
}
private void prepareBackGround() {
FileSystemReader fsr = new FileSystemReader(AnyView.Root +
"anyview/bg.png");
if (fsr.exists()) {
int len = (int) fsr.fileSize();
byte[] b = fsr.read(len);
/*
//创建半透明背景,在真机上失败
Image temp = Image.createImage(b, 0, b.length);
b = null;
if (temp != null) {
int w = temp.getWidth();
int h = temp.getHeight();
int[] v = new int[w * h];
temp.getRGB(v, 0, w, 0, 0, w, h);
for (int i = 0; i < v.length; i++) {
v[i] = 0x3F000000 | (v[i] >>> 8);
}
background = Image.createRGBImage(v, w, h, false);
v = null;
temp = null;
}*/
try {
background = Image.createImage(b, 0, b.length);
} catch (Exception ex) {
background = null;
}
/*b = null;
int w = background.getWidth();
int h = background.getHeight();
int[] v = new int[w * h];
int color = 0xAAFFFFFF; // | ColorSelector.colors[AnyView.backColor];
for (int i = 0; i < v.length; i++) {
v[i] = color;
}
Image temp = Image.createRGBImage(v, w, h, true);
Image mutable = Image.createImage(w, h);
Graphics g = mutable.getGraphics();
g.drawImage(background, 0, 0, ANCHOR);
g.drawImage(temp, 0, 0, ANCHOR);
v = null;
temp = null;
background = mutable;*/
}
fsr.close();
System.gc();
}
void readNextBuffer() {
needsearch = false;
int start = 0;
if (null != bufferbuffer) { //查看缓冲区尾部数据的缓冲中是否有数据
start = bufferbuffer.length;
}
byte[] b = fsa.read(BufferLenght);
if (b.length != BufferLenght) { //已读到了文章的尾部
eof = true;
bufferTxt = new byte[b.length + start];
//将缓冲区尾部数据的缓冲中的数据COPY至缓冲区中
System.arraycopy(bufferbuffer, 0, bufferTxt, 0, start);
System.arraycopy(b, 0, bufferTxt, start, b.length);
bufferbuffer = new byte[0]; //缓冲的缓冲为空
b = null;
System.gc();
return;
}
//删除缓冲区中最后一个回车以后的字符
eof = false;
int end = b.length;
if (end > 0) {
while (true) {
int v = b[end - 1] & 0xff;
if (v == 10) { //\n 换行
break;
}
end--;
}
}
if (0 == end) { //遇上了严重的错误,居然16K数据中没有一个回车符,不合情理
System.exit( -1);
}
bufferTxt = new byte[end + start];
int rest = BufferLenght - end;
System.arraycopy(bufferbuffer, 0, bufferTxt, 0, start);
System.arraycopy(b, 0, bufferTxt, start, end);
bufferbuffer = new byte[rest];
System.arraycopy(b, end, bufferbuffer, 0, rest);
b = null;
System.gc();
//是否需要进行编码转换
/*if (txtCharSet == UTF8) {
//ByteArrayInputStream bais = new ByteArrayInputStream(bufferTxt);
//DataInputStream dis = new DataInputStream(bais);
StringBuffer sb = new StringBuffer();
try {
sb.append(new String(bufferTxt, "UTF-8"));
bufferTxt
} catch (UnsupportedEncodingException ex) {
}
} else if (txtCharSet == UNICODE) {
}*/
}
void readPreviousBuffer() {
/*int start = currentOffset - 1 - BufferLenght;
start = start < 0 ? 0 : start;