📄 functions.java
字号:
package mujmail.util;/*MujMail - Simple mail client for J2MECopyright (C) 2005 Pavel Machek <pavel@ucw.cz>Copyright (C) 2006 Nguyen Son Tung <n.sontung@gmail.com>Copyright (C) 2008 David Hauzar <david.hauzar.mujmail@gmail.com> This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */import mujmail.*;import java.util.Calendar;import java.util.Enumeration;import java.util.Random;import java.util.TimeZone;import java.util.Vector;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.rms.RecordStore;import mujmail.ordering.Comparator;/** * Static class that provides basic common functions used in the application. * @author Pavel Machek */public class Functions { private static final boolean DEBUG = false; //criterias for sort() //for headers public static final byte SRT_HDR_TIME = 0; public static final byte SRT_HDR_SUBJECT = 1; public static final byte SRT_HDR_TO = 2; public static final byte SRT_HDR_FROM = 3; public static final byte SRT_HDR_SIZE = 4; public static final byte SRT_HDR_RECORD_ID = 5; public static final byte SRT_HDR_MSGID = 6; //for address book contact public static final byte SRT_CNT_NAME = 16; //sort orders public static final byte SRT_ORDER_INC = 0; public static final byte SRT_ORDER_DEC = 1; /** * Creates vector of strings. Each string will correspond to one recipient. * * @param rcp string terminated by " *" is a string containing * emails separated by commas, '*' or space, there might be To: Bcc: Cc * name of a recipient might be enclosed in "" or not, ie: * To: mail@ads.com *Bcc:"someone here" <mail@ads.com>, [manySpaces] nextperson@here.com, * Cc: crazy@nuts.it, Tung <Tung@there.cz> * @return vector of strings of recipients. */ public static Vector parseRcp(String rcp) { Vector rcps = new Vector(); String buff = new String(rcp); int sb, sc; //sb is beginning index of email address, sc is of ending if (!buff.endsWith("*")) { buff += "*"; } while (buff.length() >= 6) { if (buff.indexOf("<") != -1 && buff.indexOf("<") < buff.indexOf("@")) //did we skipped some email? its when buff.indexOf(">") > buff.indexOf("@") { sb = buff.indexOf("<") + 1; } //email begins with < else if (buff.indexOf(":") != -1 && buff.indexOf(":") < buff.indexOf("@")) { sb = buff.indexOf(":") + 1; } //another email after Cc: or Bcc: etc else { if (buff.charAt(0) == ',') //next email { buff = buff.substring(1).trim(); } sb = 0; //we skipped some simple address like thisperson@here.com, *Cc: crazy@nuts.it } if (sb > 0 && buff.charAt(sb - 1) == '<') //email's terminated by > { sc = buff.indexOf(">", sb + 1); } else { //email's terminated by , or space* sc = buff.indexOf(",", sb + 1) < buff.indexOf(" *", sb + 1) ? buff.indexOf(",", sb + 1) : buff.indexOf(" *", sb + 1); } if (sc == -1) //email's terminated by space or * { sc = buff.indexOf(" ", sb + 1) == -1 ? buff.indexOf("*", sb + 1) : buff.indexOf(" ", sb + 1); } rcps.addElement(buff.substring(sb, sc).trim()); buff = buff.substring(sc + 1).trim(); } return rcps; } public static String encodeRcpNames(String rcp) { StringBuffer output = new StringBuffer(); int length = rcp.length(); char c; int j, i = 0; while (i < length) { c = rcp.charAt(i); output.append(c); if (c == '"') { j = rcp.indexOf(c, i + 1); if (j != -1) { output.append(Decode.encodeHeaderField(rcp.substring(i + 1, j)) + '"'); i = j; } } ++i; } return output.toString(); } /** * From given string containing email address parses just email address. * @param s string containing email address enclosed in brackets. * @return email address. */ public static String emailOnly(String s) { if (s.indexOf("<") == -1) { return s; } return s.substring(s.indexOf("<") + 1, s.indexOf(">")); } public static String genID() { return Long.toString(System.currentTimeMillis(), 36) + "." + Integer.toString(Math.abs(new Random().nextInt()), 36); } /** * Returns unicode string size in bytes * * @param s string to get size for * @return size in bytes */ public static int getStringByteSize(String s) { return 1 + s.length() * 2; } /** * Gets unix time from date encoded in given string. * @param str a string in format Tue, 28 Nov 2006 17:00:05 [-+]hhmm * @return unix time */ public static long getStrToLongTime(String str) { if (str == null) { return 0; } try { String s = str.trim().toLowerCase(); Calendar time = Calendar.getInstance(); if (!Character.isDigit(s.charAt(0))) {//its format is Tue, 28 Nov 2006 17:00:05 [-+]hhmm. skip the day of week part s = s.substring(s.indexOf(" ")).trim(); } time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2).trim())); s = s.substring(s.indexOf(" ") + 1).trim(); if (s.startsWith("jan")) { time.set(Calendar.MONTH, Calendar.JANUARY); } else if (s.startsWith("feb")) { time.set(Calendar.MONTH, Calendar.FEBRUARY); } else if (s.startsWith("mar")) { time.set(Calendar.MONTH, Calendar.MARCH); } else if (s.startsWith("apr")) { time.set(Calendar.MONTH, Calendar.APRIL); } else if (s.startsWith("may")) { time.set(Calendar.MONTH, Calendar.MAY); } else if (s.startsWith("jun")) { time.set(Calendar.MONTH, Calendar.JUNE); } else if (s.startsWith("jul")) { time.set(Calendar.MONTH, Calendar.JULY); } else if (s.startsWith("aug")) { time.set(Calendar.MONTH, Calendar.AUGUST); } else if (s.startsWith("sep")) { time.set(Calendar.MONTH, Calendar.SEPTEMBER); } else if (s.startsWith("oct")) { time.set(Calendar.MONTH, Calendar.OCTOBER); } else if (s.startsWith("nov")) { time.set(Calendar.MONTH, Calendar.NOVEMBER); } else { time.set(Calendar.MONTH, Calendar.DECEMBER); } s = s.substring(s.indexOf(" ") + 1).trim(); time.set(Calendar.YEAR, Integer.parseInt(s.substring(0, 4))); s = s.substring(s.indexOf(" ") + 1).trim(); time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(s.substring(0, 2))); time.set(Calendar.MINUTE, Integer.parseInt(s.substring(3, 5))); time.set(Calendar.SECOND, Integer.parseInt(s.substring(6, 8))); s = s.substring(8).trim(); int offset = 0; if (s.indexOf("-") != -1 || s.indexOf("+") != -1) { int x = Math.max(s.indexOf("-"), s.indexOf("+")) + 1; offset = Integer.parseInt(s.substring(x, x + 2)) * 60 + Integer.parseInt(s.substring(x + 2, x + 4)); offset *= 60 * 1000; if (s.charAt(x) == '-') { offset = -offset; } TimeZone timezone = TimeZone.getDefault(); offset = timezone.useDaylightTime() ? timezone.getRawOffset() - offset + 3600000 : timezone.getRawOffset() - offset; } return time.getTime().getTime() + offset; } catch (Exception ex) { return 0; } } /** * Removes html tags from given string. * @param text string where html tags will be removed. * @return text without html tags. */ public static String removeTags(String text) { if (text == null) { return null; } StringBuffer newText = new StringBuffer(""); boolean inquote = false, intag = false; char c; for (int i = 0; i < text.length(); ++i) { c = text.charAt(i); if (intag) { if (c == '"') { inquote = !inquote; } else if (text.charAt(i) == '>' && !inquote) { intag = false; } } else { if (c == '<') { intag = true; inquote = false; } else { newText.append(c); } } } return newText.toString(); } /** * Converts an integer to a string. If the result has less digits than the * specified length, it is left-padded with zeroes. the number must not * exceed 9999 and nonnegative. */ public static String intToStr(int value, int length) { String result = Integer.toString(value); if (length > result.length()) { result = "0000".substring(4 - (length - result.length())) + result; } return result; } /** * Gets string representation of local time zone. * @return */ public static String getLocalTimeZone() { TimeZone timezone = TimeZone.getDefault(); int offset = timezone.useDaylightTime() ? timezone.getRawOffset() / 1000 + 3600 : timezone.getRawOffset() / 1000; char sign = offset >= 0 ? '+' : '-'; offset = Math.abs(offset); return sign + intToStr(offset / 3600, 2) + intToStr(offset % 3600, 2); } /** * Causes the currently executing thread to sleep (temporarily cease execution) * for the specified number of milliseconds. The thread does not lose * ownership of any monitors. * @param msec the length of time to sleep in milliseconds. */ public static void sleep(int msec) { try { Thread.sleep(msec); } catch (Exception x) { x.printStackTrace(); } } /** * Loads icon of given name. * @param name the name of the ico. * @return the image representing the icon. */ public static Image getIcon(String name) { String imgRoot = "/icons/"; Image img = null; try { img = Image.createImage(imgRoot + name); } catch (java.io.IOException x) { x.printStackTrace(); } return img; } /** * Make a copy of given vector. * @param vector the vector to be copyed * @return the copy of given vector. */ public static Vector copyVector(Vector vector) { Vector newVector = new Vector(vector.size()); for (int i = 0; i < vector.size(); i++) { newVector.addElement(vector.elementAt(i)); } return newVector; } /** * Creates new vector from given enumeration. * @param enumeration the enumeration to be copyed. * @return new vector with elements from given enumeration. */ public static Vector copyEnumerationToVector(Enumeration enumeration) { Vector newVector = new Vector(); while (enumeration.hasMoreElements()) { newVector.addElement(enumeration.nextElement()); } return newVector; } /** * Scales given image. * @param src the image to be scaled. * @param width the width of scaled image. * @param height the height of scaled image. * @return scaled image. */ public static Image scaleImage(Image src, int width, int height) { int srcWidth = src.getWidth(); int srcHeight = src.getHeight(); Image tmp = Image.createImage(width, srcHeight); Graphics g = tmp.getGraphics(); int delta = (srcWidth << 16) / width; int pos = delta / 2; for (int x = 0; x < width; x++) { g.setClip(x, 0, 1, srcHeight); g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP); pos += delta; } Image dst = Image.createImage(width, height); g = dst.getGraphics(); delta = (srcHeight << 16) / height; pos = delta / 2; for (int y = 0; y < height; y++) { g.setClip(0, y, width, 1); g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP); pos += delta; } return dst; } /** * Converts given string to CRLF format. * @param text the string that is not CRLF to be converted. * @return string in CRLF */ public static String toCRLF(String text) { StringBuffer buffer = new StringBuffer(text); int j = text.length(), i = 0; while (i < j) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -