📄 systool.java
字号:
package com.talkweb.micp.icpl.systool;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
public class Systool {
public final static String conUrl =
"http://128.128.7.250:8080/movieticket/dispatcher";
public final static String binUrl="http://128.128.7.250:8080/iplayer/iplayer";
public static Hashtable[] decodeStream(InputStream is, ProgressUI progress,
int contentLength) throws Exception {
DataInputStream dis = new DataInputStream(is);
int num = dis.readInt();
int length[] = new int[num];
Hashtable[] ht = new Hashtable[num];
for (int i = 0; i < num; ++i) {
ht[i] = new Hashtable();
byte b[] = new byte[16]; //文件名、文件ID
for (int j = 0; j < 16; ++j) {
b[j] = dis.readByte();
}
ht[i].put("ID", new String(b).trim());
byte btype[] = new byte[8]; //文件类型
for (int j = 0; j < 8; ++j) {
btype[j] = dis.readByte();
}
ht[i].put("TYPE", new String(btype).trim().toUpperCase());
length[i] = dis.readInt();
}
int read = 0;
byte buffer[] = new byte[1024]; //分配1k内存
for (int i = 0; i < num; ++i) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int b = -1;
int len = length[i];
int total = 0;
int toRead = buffer.length < len ? buffer.length : len;
while (((b = dis.read(buffer, 0, toRead)) != -1)) {
bos.write(buffer, 0, b);
total += b;
read += b;
progress.onProgress(read * 100 / contentLength);
if (total < len) {
toRead = len - total;
toRead = buffer.length > toRead ? toRead : buffer.length;
} else {
break;
}
}
bos.flush();
ht[i].put("STREAM_BYTE", bos.toByteArray());
bos.close();
bos = null;
}
buffer = null;
return ht;
}
public static Hashtable getInStream(String sUrl, String sData) throws
Exception {
HttpConnection conn = Systool.openHttpConnection(sUrl, sData);
int code = conn.getResponseCode();
if (code != HttpConnection.HTTP_OK) {
throw new Exception("连接服务器错误,返回码为:" + code);
}
int length = (int)conn.getLength();
InputStream is = conn.openInputStream();
Hashtable ht = new Hashtable(2);
ht.put("length",new Integer(length));
ht.put("stream",is);
conn.close();
conn = null;
return ht;
}
public static String doPost(String sUrl, String sData) throws Exception {
InputStream is = (InputStream)getInStream(sUrl,sData).get("stream");
String sXml = Systool.getXmlFromStream(is);
is.close();
is = null;
return sXml;
}
public static String getXmlFromStream(InputStream is) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
int b = -1;
while ((b = is.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, b);
}
return KXmlTool.trimall(new String(bos.toByteArray(), "UTF-8"));
}
public static HttpConnection openHttpConnection(String sUrl, String sData) throws
Exception {
HttpConnection conn = null;
conn = (HttpConnection) Connector.open(sUrl);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Configuration/CLDC-1.0");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",
String.valueOf(sData.length()));
OutputStream os = conn.openOutputStream();
os.write(sData.getBytes());
return conn;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -