📄 iavreader.java
字号:
package com.ismyway.anyview;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
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 IAVReader {
private String filename;
public final static byte[] Sign = {(byte) 0x80, 'I', 'A', 'V'};
public final static String IAVVersion = "1.0a";
public final static int ANCHOR = Graphics.LEFT | Graphics.TOP;
private byte[] Version;
private byte[] Name;
private byte[] Author;
private byte[] Maker;
private int CoverLen = 0;
private Image Cover = null;
private int ChapterNum = 0;
private byte[][] ChaptersNames;
private int currentChap = -1;
private int iavLen = 0; //文档总长度
private int[] chapOffset; //每章节在文档中的偏移,ChaptersNames=0时置空
private byte[] chapType; //每章节的数据类型,ChaptersNames=0时置空,并且接下来的数据为TXT文本
private byte[] temp;
private FileSystemReader fsa;
private boolean validDoc = true;
public IAVReader(String filename) {
this.filename = filename;
fsa = new FileSystemReader(this.filename);
iavLen = (int) fsa.fileSize();
analysisIav();
}
private void analysisIav() {
byte[] b = new byte[164];
b = fsa.read(164);
byte[] sign = getData(b, 0, 4);
for (int i = 0; i < Sign.length && validDoc; i++) {
if (sign[i] != Sign[i]) {
validDoc = false;
}
}
if (!validDoc) {
Cover = AnyView.cf.buildImage("非法的IAV文件!");
return;
}
Version = deleteZeroData(getData(b, 4, 6));
String str = new String(Version);
if (IAVVersion.compareTo(str) < 0) {
validDoc = false;
Cover = AnyView.cf.buildImage("无法打开 " + str + " 版本的文件!");
return;
}
Name = deleteZeroData(getData(b, 10, 50));
Author = deleteZeroData(getData(b, 60, 50));
Maker = deleteZeroData(getData(b, 110, 50));
CoverLen = getData(b, 160);
b = null;
if (CoverLen > 0) {
byte[] cover = getData(CoverLen);
Cover = Image.createImage(cover, 0, CoverLen);
}
ChapterNum = getInt();
if (ChapterNum > 0) { //定义章节
ChaptersNames = new byte[ChapterNum][];
chapOffset = new int[ChapterNum];
chapType = new byte[ChapterNum];
byte[] temp = getData(ChapterNum * 64);
int off = 0;
for (int i = 0; i < ChapterNum; i++) {
ChaptersNames[i] = getData(temp, off, 60);
chapType[i] = ChaptersNames[i][59];
ChaptersNames[i] = deleteZeroData(ChaptersNames[i]);
off += 60;
chapOffset[i] = getData(temp, off);
off += 4;
}
temp = null;
} else {
validDoc = false;
Cover = AnyView.cf.buildImage("章节不可为空!");
return;
}
}
public boolean isValidDoc() {
return validDoc;
}
private byte[] deleteZeroData(byte[] b) {
int counter = 0;
while (true) {
if (b[counter] == 0) {
break;
}
counter++;
}
byte[] data = new byte[counter];
System.arraycopy(b, 0, data, 0, counter);
return data;
}
public Image getCover() {
if (Cover != null) {
return Cover;
} else {
Image img = Image.createImage(240, 320);
Graphics g = img.getGraphics();
g.setColor(0);
g.fillRect(0, 0, 240, 320);
AnyView.cf.setColor(0xFFFFFF);
Image temp = AnyView.cf.buildImage(Name);
int x = (240 - temp.getWidth()) >> 1;
g.drawImage(temp, x, 50, ANCHOR);
temp = null;
temp = AnyView.cf.buildImage(Author);
x = (240 - temp.getWidth()) >> 1;
g.drawImage(temp, x, 80, ANCHOR);
temp = null;
temp = AnyView.cf.buildImage(Maker);
x = (240 - temp.getWidth()) >> 1;
g.drawImage(temp, x, 110, ANCHOR);
temp = null;
return img;
}
}
public void close() {
if (fsa != null) {
fsa.close();
fsa = null;
}
}
public byte[] getChapter(int index) {
if (!validDoc || index < 0 || index > ChapterNum - 1) {
return null;
}
int off = chapOffset[index] - fsa.offset;
int len = 0;
if (index == ChapterNum - 1) {
len = (int) iavLen - chapOffset[ChapterNum - 1];
} else {
len = chapOffset[index + 1] - chapOffset[index];
}
byte[] b = new byte[len];
fsa.skip(off);
b = fsa.read(len);
currentChap = index;
return b;
}
public byte[] getNextChapter() {
if (!validDoc) {
return null;
}
currentChap++;
currentChap = currentChap > ChapterNum - 1 ? ChapterNum - 1 :
currentChap;
int off = chapOffset[currentChap] - fsa.offset;
int len = 0;
if (currentChap == ChapterNum - 1) {
len = (int) iavLen - chapOffset[ChapterNum - 1];
} else {
len = chapOffset[currentChap + 1] - chapOffset[currentChap];
}
byte[] b = new byte[len];
fsa.skip(off);
b = fsa.read(len);
return b;
}
public byte[] getPreChapter() {
if (!validDoc) {
return null;
}
currentChap--;
currentChap = currentChap < 0 ? 0 : currentChap;
int off = chapOffset[currentChap] - fsa.offset;
int len = chapOffset[currentChap + 1] - chapOffset[currentChap];
byte[] b = new byte[len];
fsa.skip(off);
b = fsa.read(len);
return b;
}
public int getCurentChapterIndex() {
return currentChap;
}
/**
* 返回当前的章节名列表
* @return byte[][]
*/
public String[] getChapterNames() {
String[] list = null;
if (ChapterNum < 1) {
list = new String[] {"(无章节)"};
} else {
list = new String[ChapterNum];
for (int i = 0; i < ChapterNum; i++) {
try {
list[i] = "" + i + ". " +
new String(ChaptersNames[i], "UTF-8");
} catch (UnsupportedEncodingException ex) {
list[i] = "" + i;
}
}
}
return list;
}
private byte[] getData(byte[] src, int offset, int len) {
temp = new byte[len];
System.arraycopy(src, offset, temp, 0, len);
return temp;
}
private int getData(byte[] src, int offset) {
byte[] b = new byte[4];
System.arraycopy(src, offset, b, 0, 4);
int v = (b[0] & 0xff) << 24 | (b[1] & 0xff) << 16 | (b[2] & 0xff) << 8 |
(b[3] & 0xff);
return v;
}
private byte[] getData(int len) {
temp = new byte[len];
temp = fsa.read(len);
return temp;
}
private int getInt() {
byte[] b = getData(4);
int v = (b[0] & 0xff) << 24 | (b[1] & 0xff) << 16 | (b[2] & 0xff) << 8 |
(b[3] & 0xff);
return v;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -