📄 gziptest.java
字号:
package example;import java.io.*;import javax.microedition.io.Connector;import javax.microedition.io.ContentConnection;import javax.microedition.io.StreamConnection;import javax.microedition.io.HttpConnection;import javax.microedition.lcdui.Gauge;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.Display;import javax.microedition.midlet.*;import java.util.Vector;import com.tinyline.util.GZIPInputStream;public class GZIPTest extends MIDlet implements Runnable { // 文件的地址可以是资源文件,也可以是网上的文件,例如: String url1 = "/1.jad.gz"; String url2 ="http://www.tinyline.com/svgt/old_samples/road_qc.svgz"; // 注意,在本程序中使用在.jad文件中的自定义的参数 // 参见setupURLList方法 Vector urls; Gauge gauge; Form form; /** * 开始一个线程 */ public GZIPTest() { gauge = new Gauge("Progress", false, 10, 0); form = new Form("Progress"); form.append(gauge); Display.getDisplay(this).setCurrent(form); setupURLList(); } /** * 开始一个线程 */ public void startApp() { new Thread(this).start(); } /** * 线程的主方法 */ public void run() { try { gauge.setLabel("获取内容"); gauge.setValue(urls.size()); for (int i = 0; i < urls.size(); i++) { // 从URL得到其内容 String url = (String)urls.elementAt(i); getViaContentConnection(url); } } catch (Exception e) { e.printStackTrace(); } gauge.setValue(10); notifyDestroyed(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } void getViaContentConnection(String url) throws IOException { ContentConnection c = null; InputStream ist = null; GZIPInputStream gis = null; byte[] buf = new byte[512]; int len; try { if (url.startsWith("/")) { // 从资源文件中得到 ist = getClass().getResourceAsStream(url); } else if ( url.startsWith("http:")) { // 从网络上获取 c = (ContentConnection)Connector.open(url); ist = c.openInputStream(); } // 如果是压缩文件,则生成一个 GZIPInputStream对象 if(url.endsWith(".z") || url.endsWith(".gz") || url.endsWith(".svgz") || url.endsWith(".jar") || url.endsWith(".zip")) { // 则生成一个 GZIPInputStream对象 ist = new GZIPInputStream(ist); } long t0,t1; // 时间 t0 = System.currentTimeMillis(); // 读取文件中的内容 while ((len = ist.read(buf)) >= 0) { // 显示内容 System.out.println(""+ new String(buf)); } t1 = System.currentTimeMillis(); System.out.println(url+ " 用时间: " + (t1-t0)); } finally { if (ist != null) ist.close(); if (c != null) c.close(); } } // 获取在.jad文件中定义的 URL-xxx 参数 private void setupURLList() { urls = new Vector(); for (int n = 1; n < 100; n++) { String nthUrl = "URL-"+ n; String url = getAppProperty(nthUrl); if (url == null || url.length() == 0) break; urls.addElement(url); } System.out.println( "urls:" + urls.size() ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -