📄 tools.java
字号:
package org.gggeye.easymf.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import org.gggeye.easymf.log.Logger;
/**
* 提供一系列的工具<br/>
* 1. String 工具<br/>
* 2. Phone UA 的工具<br/>
* 3. 读取数据的工具<br/>
* @author wuhua
* <a href="http://wuhua.3geye.net">我的博客</a>
*
*/
public class Tools {
/**
* 切割str字符串
*
* @param str
* @param regex
* @return
*/
public static String[] split(String bufferstr, String regex) {
if (bufferstr == null)
return null;
Vector split = new Vector();
while (true) // 处理从网络上获得的数据并对其进行处理
{
int index = bufferstr.indexOf(regex);
if (index == -1) {
if (bufferstr != null && !bufferstr.equals(""))
split.addElement(bufferstr);
// log.debug("bufferstr=" +bufferstr);s
break;
}
split.addElement(bufferstr.substring(0, index));
// log.debug("Str=" + bufferstr.substring(0, index));
bufferstr = bufferstr.substring(index + 1, bufferstr.length());
// log.debug("bufferstr=" +bufferstr);
}
String[] strs = new String[split.size()];
split.copyInto(strs);
split.removeAllElements();
split = null;
return strs;
}
/**
* Converts String to UTF-8 String. Code from Colibry IM messenger used
* @param s String to convert
* @return converted String
*/
public static String toUTFString(String s) {
int i = 0;
StringBuffer stringbuffer = new StringBuffer();
for (int j = s.length(); i < j; i++) {
int c = (int) s.charAt(i);
if ((c >= 1) && (c <= 0x7f)) {
stringbuffer.append((char) c);
}
if (((c >= 0x80) && (c <= 0x7ff)) || (c == 0)) {
stringbuffer.append((char) (0xc0 | (0x1f & (c >> 6))));
stringbuffer.append((char) (0x80 | (0x3f & c)));
}
if ((c >= 0x800) && (c <= 0xffff)) {
stringbuffer.append(((char) (0xe0 | (0x0f & (c >> 12)))));
stringbuffer.append((char) (0x80 | (0x3f & (c >> 6))));
stringbuffer.append(((char) (0x80 | (0x3f & c))));
}
}
return stringbuffer.toString();
}
/**
* 释放内存<br/>
* 当空闲内存小于指定内存时候释放.
*
* @param addtionMemory -- 指定内存
*/
public final static void release(long addtionMemory) {
long freeMemory = Runtime.getRuntime().freeMemory();
//如果空闲的内存小于指定的内存,则回收
if(freeMemory<=addtionMemory){
System.gc();
}
}
/**
* 从指定的InputStream读取数据
* @param _inputStream
* @return
*/
public final static byte[] read(InputStream _inputStream){
try {
byte[] body = null;
byte[] buff = new byte[1024];
long total = 0;
int count = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((count = _inputStream.read(buff)) > 0) {
// 超过100K
if (total >= java.lang.Runtime.getRuntime().freeMemory() + 102400) {
System.gc();
if (total >= Runtime.getRuntime().freeMemory() + 102400) {
buff = null;
body = null;
System.gc();
return null;
}
}
baos.write(buff, 0, count);
total += count;
}
body = baos.toByteArray();
buff = null;
baos.close();
_inputStream.close();
return body;
} catch (Exception e) {
Logger.info(e);
}
return null;
}
/**
* 读取一张图片
* @param _path
* @return
*/
public final static Image readImage(String _path){
try {
return Image.createImage(_path);
} catch (IOException e) {
Logger.debug(_path);
}
return null;
}
/**
* 获取UTF-8 字符的编码值
* @param _str
* @return
*/
public final static String toHexString(String _str){
StringBuffer tHex = new StringBuffer();
char[] tChars = _str.toCharArray();
for(int i=0; i < _str.length(); i++){
tHex.append("\\u" + Integer.toHexString((int)tChars[i]));
}
return tHex.toString();
}
/*
* Replace all instances of a String in a String. @param s String to alter.
* @param f String to look for. @param r String to replace it with, or null
* to just remove it.
*/
public static String replace(String s, String f, String r) {
if (s == null)
return s;
if (f == null)
return s;
if (r == null)
r = "";
int index01 = s.indexOf(f);
while (index01 != -1) {
s = s.substring(0, index01) + r + s.substring(index01 + f.length());
index01 += r.length();
index01 = s.indexOf(f, index01);
}
return s;
}
/**
* Method removes HTML tags from given string.
*
* @param text
* Input parameter containing HTML tags (eg. <b>cat</b>)
* @return String without HTML tags (eg. cat)
*/
public static String removeHtml(String text) {
try {
int idx = text.indexOf("<");
if (idx == -1)
return text;
String plainText = "";
String htmlText = text;
int htmlStartIndex = htmlText.indexOf("<", 0);
if (htmlStartIndex == -1) {
return text;
}
while (htmlStartIndex >= 0) {
plainText += htmlText.substring(0, htmlStartIndex);
int htmlEndIndex = htmlText.indexOf(">", htmlStartIndex);
htmlText = htmlText.substring(htmlEndIndex + 1);
htmlStartIndex = htmlText.indexOf("<", 0);
}
plainText = plainText.trim();
return plainText;
} catch (Exception e) {
System.err.println("Error while removing HTML: " + e.toString());
return text;
}
}
public static final String endocer(String _url) {
if (_url == null) {
return "";
}
String res = _url;
try {
StringBuffer sb = new StringBuffer();
byte body[] = null;
try {
body = _url.getBytes("UTF-8");
} catch (UnsupportedEncodingException ex) {
body = _url.getBytes();
}
for (int i = 0; i < body.length; i++) {
byte byte0 = body[i];
do {
if (byte0 == 32) {
sb.append('+');
break;
} else if (byte0 == 10) {
sb.append("%0A");
break;
} else if (byte0 >= 48 && byte0 <= 57 || byte0 >= 65
&& byte0 <= 90 || byte0 >= 97 && byte0 <= 122
|| byte0 == 46 || byte0 == 45 || byte0 == 95
|| byte0 == 42 || byte0 >= 0) {
sb.append((char) byte0);
break;
} else {
if (byte0 < 0) {
sb.append('%');
String s1 = Integer.toHexString(byte0);
if (s1.length() < 2) {
sb.append('0');
} else if (s1.length() > 2) {
s1 = s1.substring(s1.length() - 2);
}
sb.append(s1);
} else if (byte0 >= 0) {
sb.append((char) byte0);
}
}
} while (false);
}
res = sb.toString();
} catch (Exception e) {
}
return res;
}
public final static String decoder(String _url){
try {
return decoder(_url,"UTF-8");
} catch (UnsupportedEncodingException e) {
Logger.debug(e);
}
return null;
}
/**
* 把字符串decode成URL编码
* 比如 中文 编程%DC%CC
* @param _url
* @param _enc
* @return
* @throws UnsupportedEncodingException
*/
public final static String decoder(String _url, String _enc)
throws UnsupportedEncodingException {
boolean needToChange = false;
StringBuffer sb = new StringBuffer();
int numChars = _url.length();
int i = 0;
if (_enc != null && _enc.length() == 0) {
throw new UnsupportedEncodingException(
"URLDecoder: empty string enc parameter");
}
while (i < numChars) {
char c = _url.charAt(i);
switch (c) {
case '+':
sb.append(' ');
i++;
needToChange = true;
break;
case '%':
/*
* Starting with this instance of %, process all consecutive
* substrings of the form %xy. Each substring %xy will yield a
* byte. Convert all consecutive bytes obtained this way to
* whatever character(s) they represent in the provided
* encoding.
*/
try {
// (numChars-i)/3 is an upper bound for the number
// of remaining bytes
byte[] bytes = new byte[(numChars - i) / 3];
int pos = 0;
while (((i + 2) < numChars) && (c == '%')) {
bytes[pos++] = (byte) Integer.parseInt(_url.substring(
i + 1, i + 3), 16);
i += 3;
if (i < numChars)
c = _url.charAt(i);
}
// A trailing, incomplete byte encoding such as
// "%x" will cause an exception to be thrown
if ((i < numChars) && (c == '%'))
throw new IllegalArgumentException(
"URLDecoder: Incomplete trailing escape (%) pattern");
String str = null;
if (_enc != null) {
str = new String(bytes, 0, pos, _enc);
} else {
str = new String(bytes, 0, pos);
}
sb.append(str);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"URLDecoder: Illegal hex characters in escape (%) pattern - "
+ e.getMessage());
}
needToChange = true;
break;
default:
sb.append(c);
i++;
break;
}
}
return (needToChange ? sb.toString() : _url);
}
/**
* 此方法是对图片进行放大,缩小,并为实现按比例进行,所以程序调用的时候,应该自行处理
* @param src -- 原图像
* @param targetWidth -- 目标图像宽度
* @param targetHeight -- 目标图像高度
* @return 返回一个放大,缩小后的图片对象
*/
public final static Image scale(Image src, int targetWidth, int targetHeight) {
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Image tmp = Image.createImage(targetWidth, srcHeight);
Graphics g = tmp.getGraphics();
int delta = (srcWidth << 16) / targetWidth;
int pos = delta / 2;
for (int x = 0; x < targetWidth; x++) {
g.setClip(x, 0, 1, srcHeight);
g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
pos += delta;
}
Image dst = Image.createImage(targetWidth, targetHeight);
g = dst.getGraphics();
delta = (srcHeight << 16) / targetHeight;
pos = delta / 2;
for (int y = 0; y < targetHeight; y++) {
g.setClip(0, y, targetWidth, 1);
g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
pos += delta;
}
return dst;
}
/**
* 透明图片
* @param _src
* @param _alpha
* @return
*/
public static Image alphaImage(Image _src, int _alpha) {
Image result = _src;
try{
int w, h;
w = _src.getWidth();
h = _src.getHeight();
int len = w * h;
int srcRgb[] = new int[len];
int dscRgb[] = new int[len];
try {
_src.getRGB(srcRgb, 0, w, 0, 0, w, h);
} catch (Exception ex) {
ex.printStackTrace();
}
for (int i = 0; i < len; i++) {
dscRgb[i] = (0x00FFFFFF & srcRgb[i]) | (_alpha << 24);//srcRgb[i] + (Alpha << 24);//
}
result = null;
result = Image.createRGBImage(dscRgb, w, h, true);
srcRgb = null;
dscRgb = null;
}catch(Exception e){}
return result;
}
public String getIMEI(){
String tImei= System.getProperty("IMEI");
System.getProperty("phone.IMEI");
System.getProperty("com.siemens.IMEI");
System.getProperty("com.nokia.mid.imei");
System.getProperty("com.sonyericsson.imei");
return tImei;
}
public String getSystemProperty(String _key){
try{
return System.getProperty(_key);
}catch(Exception e){
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -