📄 stringloadthread.java
字号:
package textViewer;import midletLib.*;import java.io.InputStream;import java.io.IOException;import javax.microedition.midlet.*;import java.lang.*;public class StringLoadThread extends Thread { private InputStream in; private ResourceLoadedListener callback; private int increments; private int incrementSz = 0; private Object rsrcID; private MIDlet thisMidlet; private StringBuffer fileBuffer; public StringLoadThread(InputStream in, ResourceIncrementLoadedListener callback, Object rsrcID, MIDlet thisMidlet, int increments, int incrementSz) { this.in = in; this.callback = callback; this.rsrcID = rsrcID; this.increments = increments; this.incrementSz = incrementSz; this.thisMidlet = thisMidlet; } public StringLoadThread(InputStream in, ResourceIncrementLoadedListener callback, Object rsrcID, MIDlet thisMidlet, int incrementSz) { this.in = in; this.callback = callback; this.rsrcID = rsrcID; this.increments = 1; this.incrementSz = incrementSz; this.thisMidlet = thisMidlet; } public StringLoadThread(InputStream in, ResourceLoadedListener callback, Object rsrcID, MIDlet thisMidlet) { this.in = in; this.callback = callback; this.rsrcID = rsrcID; this.incrementSz = 0; // this.increments = 0; this.thisMidlet = thisMidlet; } public void run() { if (in == null) { callback.resourceLoaded(rsrcID, "Text Viewer Error (StringLoadThread.run()): can't find text file to read from"); return; } int dataSz; try { dataSz = Integer.parseInt(thisMidlet.getAppProperty("data-file-size")); } catch (NumberFormatException e) {dataSz = 16;} //TODO: constant catch (NullPointerException e) {dataSz = 16;} this.fileBuffer = new StringBuffer(dataSz); try { if (incrementSz == 0) {loadRest();} else { boolean moreLeft = true; for(int curIncrement=0;(curIncrement<increments) && (moreLeft); curIncrement++) { moreLeft = loadIncrement(incrementSz); ((ResourceIncrementLoadedListener)callback).moreOfResourceLoaded(rsrcID, fileBuffer.toString()); } loadRest(); } in.close(); } catch(IOException e) { fileBuffer.append("Text Viewer Error during file read: " + e.toString()); try {in.close();} catch (IOException e2) {} } callback.resourceLoaded(rsrcID, fileBuffer.toString()); } /* public void run() { // throw new IllegalStateException("reached run"); callback.resourceLoaded(rsrcID, "Loaded!"); } */ private boolean loadIncrement(int incrementSz) throws IOException { int c = in.read(); for (int count = 0; (count < incrementSz) && (c >= 0); count++) { fileBuffer.append( (char)c ); c = in.read(); } if (c >= 0) { fileBuffer.append( (char)c ); return true; } else {return false;} } private void loadRest() throws IOException { int c = in.read(); while (c >= 0) { fileBuffer.append( (char)c ); c = in.read(); } }}/* old parts // seems to be just as slow as the above, and requires much more memory for some reason String ret = "";// actually, this will be initialized later either way, oh well /* // char[] cArray; // cArray = new char[dataSz]; // cArray = new char[1000]; InputStreamReader fileIoReader = new InputStreamReader(in); int result = fileIoReader.read(cArray); while (result >= 0) { fileB.append(cArray, 0, result); // fileB.append(Integer.toString(result) + ":"); result = fileIoReader.read(cArray); } /* DataInputStream dis = new DataInputStream(in); ret = dis.readUTF(); // I think there's some sort of UTF problem with this that I don't get. Apparently my Debian text files are already in UTF (at least, trying to convert them with Perl or "tcs" doesn't seem to do anything). Maybe the special Java UTF is screwing things up?... // ret = "Text Viewer Error during file read: " + e.toString();... // return ret;... // System.gc(); // moved this to main class */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -