1 http://topic.csdn.net/u/20111214/20/2534c03f-5447-4541-91e2-1ee2ca97c786.html?2906
2 package Cool;
3
4 import java.io.UnsupportedEncodingException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import Adapter.MyTextView;
9 import Util.CT;
10 import android.widget.TextView;
11
12 public class TextReader {
13 /** 文件的显示控件 */
14 private TextView mTextView;
15
16 /** 文件名 */
17 private String mFilePath = null;
18
19 /** 默认的文件编码形式 */
20 private String mEncoding = "gb2312";
21
22 /** 文件末尾 */
23 private boolean mEndOfDoc = false;
24
25 /** 文件开头 */
26 private boolean mBeforeOfDoc = true;
27
28 /** 当前显示缓冲的显示行列表 */
29 private List<TxtLine> mCurrentList = new ArrayList<TxtLine>();
30
31 /** 读取文件的输入流工具类 */
32 private RandomAccessRead mRaf = null;
33
34 /** 用于保存部分文件的缓冲区大小 */
35 private int mDataLengthOfOneDoc = 100 * 1024;
36
37 /** 屏幕宽度和高度 */
38 private int mViewWidth, mViewHeight;
39
40 /** 文件大小 */
41 private long mFileLength;
42
43 /** 缓冲区中用于显示当前页面的第一行 */
44 private int mCurrentLine = 0;
45
46 /** 缓冲区中用于显示当前页面的起始索引 */
47 private int mCurrentOffset = 0;
48
49 /** 文件中用于显示当前页面的起始索引 */
50 private int mStartOffset = 0;
51
52 /** 用于显示当前页面的数组的起始索引 */
53 private int mDataStartLocation = 0;
54
55 /** 用于显示当前页面的数组的结尾索引 */
56 private int mDataEndLocation = 0;
57
58 /** 当前显示的文件大小百分比 */
59 private int mPercent = 0;
60
61 /** 文件中用于显示当前页面的结尾索引 */
62 private int mEndOffset = 0;
63
64 /** 用于显示当前页面的数组 */
65 private byte[] mScreenData = null;
66
67 /** 用于显示的缓冲数组 */
68 private byte[] mDisplayBuffer = null;
69
70 /**
71 * 构造函数
72 *
73 * @param mTextView
74 * 显示控件
75 * @param mScreenWidth
76 * 屏幕宽度
77 * @param mScreenHeight
78 * 屏幕高度
79 * @param mFilePath
80 * 文件路径
81 * @param mEncoding
82 * 文件编码
83 */
84 public TextReader(MyTextView mTextView, int mScreenWidth, int mScreenHeight,
85 String mFilePath, String mEncoding) {
86 this.mTextView = mTextView;
87 this.mFilePath = mFilePath;
88 this.mEncoding = mEncoding;
89 this.mViewWidth = mScreenWidth;
90 this.mViewHeight = mScreenHeight;
91 init();
92 }
93
94 public void readFile() {
95 readNextBuffer();
96 analyzeDisplayBuffer();
97 displayNextScreen(0);
98 }
99
100 /**
101 * 获取读取文件的输入流以及文件大小
102 */
103 private void init() {
104 // TODO Auto-generated method stub
105 this.mRaf = new RandomAccessRead(mFilePath);
106 this.mFileLength = mRaf.length();
107 if (this.mFileLength == 0) {
108 mTextView.setText(Constant.NODATAINFILE);
109 return;
110 }
111 }
112
113 /**
114 * 获取下一个缓冲区
115 */
116 private void readNextBuffer() {
117 mRaf.openNewStream();
118 mRaf.locate(mStartOffset);
119 byte[] b = new byte[mDataLengthOfOneDoc];
120 mCurrentOffset = mStartOffset;
121 int actualLength = mRaf.readBytes(b);
122 if (mStartOffset == 0) {
123 mBeforeOfDoc = true;
124 } else {
125 mBeforeOfDoc = false;
126 }
127 if (actualLength < mDataLengthOfOneDoc) {
128 mEndOfDoc = true;
129 } else {
130 mEndOfDoc = false;
131 }
132
133 if (actualLength == -1 && mScreenData.length == 0) {// 意外到了文件流的末尾或者
134 mTextView.setText("读取文件失或者文件缓冲区失败");
135 return;
136 }
137
138 if (mEndOfDoc) {
139 mDisplayBuffer = new byte[actualLength];
140 System.arraycopy(b, 0, mDisplayBuffer, 0, actualLength);
141 b = null;
142 System.gc();
143 return;
144 }
145 /** 最后一个换行符的索引 */
146 int readDataLength = actualLength;
147 int nLocation = 0;
148 while (readDataLength > 0) {
149 if ((b[readDataLength - 1] & 0xff) == 10) {
150 nLocation = readDataLength;
151 break;
152 }
153 readDataLength--;
154 }
155 if (nLocation == 0) {
156 System.exit(1);
157 }
158 int displayLength = nLocation;
159 mDisplayBuffer = new byte[displayLength];
160 System.arraycopy(b, 0, mDisplayBuffer, 0, displayLength);
161 b = null;
162 System.gc();
163 }
164
165 /**
166 * 获取上一个缓冲区
167 */
168 private void readPreBuffer() {
169 int offsetOfLastScreen = mCurrentList.get(mCurrentLine).offset;
170 if (offsetOfLastScreen <= mDataLengthOfOneDoc) {
171 mBeforeOfDoc = true;
172 // if(offsetOfLastScreen>=mFileLength){
173 // mEndOfDoc=true;
174 // }
175 byte[] b = new byte[offsetOfLastScreen];
176 mRaf.openNewStream();
177 int actualLength = mRaf.readBytes(b);
178 if (actualLength < offsetOfLastScreen) {
179 mEndOfDoc = true;
180 } else {
181 mEndOfDoc = false;
182 }
183 if (actualLength == -1 && mScreenData.length == 0) {// 意外到了文件流的末尾或者
184 mTextView.setText("读取文件失或者文件缓冲区失败");
185 return;
186 }
187
188 if (mEndOfDoc) {
189 mDisplayBuffer = new byte[actualLength];
190 System.arraycopy(b, 0, mDisplayBuffer, 0, actualLength);
191 b = null;
192 System.gc();
193 mCurrentOffset = 0;
194 return;
195 }
196 /** 最后一个换行符的索引 */
197 int readDataLength = actualLength;
198 int nLocation = 0;
199 while (readDataLength > 0) {
200 if ((b[readDataLength - 1] & 0xff) == 10) {
201 nLocation = readDataLength;
202 break;
203 }
204 readDataLength--;
205 }
206 if (nLocation == 0) {
207 System.exit(1);
208 }
209 int displayLength = nLocation;
210 mDisplayBuffer = new byte[displayLength];
211 System.arraycopy(b, 0, mDisplayBuffer, 0, displayLength);
212 b = null;
213 System.gc();
214 mCurrentOffset = 0;
215 return;
216 }
217
218 int skipLength = offsetOfLastScreen - mDataLengthOfOneDoc;
219 mRaf.openNewStream();
220 mRaf.locate(skipLength);
221 mCurrentOffset = skipLength;
222 byte[] b = new byte[mDataLengthOfOneDoc];
223 int readLength = mRaf.readBytes(b);
224 mBeforeOfDoc = false;
225 if (readLength < mDataLengthOfOneDoc) {
226 mEndOfDoc = true;
227 }
228 if (readLength == -1 && mScreenData.length == 0) {// 意外到了文件流的末尾或者
229 mTextView.setText("读取文件失或者文件缓冲区失败");
230 return;
231 }
232
233 int nlocation = 0;
234 while (nlocation < readLength) {
235 if ((b[readLength - 1] & 0xff) == 10) {
236 nlocation = readLength;
237 break;
238 }
239 readLength--;
240 }
241 if (nlocation == 0) {
242 System.exit(1);
243 }
244
245 mDisplayBuffer = new byte[readLength];
246 System.arraycopy(b, 0, mDisplayBuffer, 0, readLength);
247 b = null;
248 System.gc();
249 }
250
251 http://topic.csdn.net/u/20111026/15/18c77efa-f41c-4315-8023-550a3732bd76.html?14299