📄 util.java
字号:
/******************************************************************************* Jimm - Mobile Messaging - J2ME ICQ clone Copyright (C) 2003-05 Jimm Project This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the 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 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ******************************************************************************** File: src/jimm/comm/Util.java Version: ###VERSION### Date: ###DATE### Author(s): Manuel Linsmayer, Andreas Rossbacher, Sergey Chernov, Andrey B. Ivlev, Artyomov Denis, Igor Palkin *******************************************************************************/package jimm.comm;import java.io.*;import java.util.*;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.Graphics;//#sijapp cond.if target!="DEFAULT"#import javax.microedition.media.Manager;import javax.microedition.media.Player;//#sijapp cond.end#import jimm.DebugLog;import jimm.ContactList;import jimm.Options;import jimm.util.ResourceBundle;public class Util{ public static Image createThumbnail(Image image, int width, int height) { int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); if ((height == 0) && (width != 0)) height = width * sourceHeight / sourceWidth; else if ((width == 0) && (height != 0)) width = height * sourceWidth / sourceHeight; else if (sourceHeight >= sourceWidth) width = height * sourceWidth / sourceHeight; else height = width * sourceHeight / sourceWidth; Image thumb = Image.createImage(width, height); Graphics g = thumb.getGraphics(); int dx; int dy; for (int y = 0; y < height; y++) { dy = y * sourceHeight / height; for (int x = 0; x < width; x++) { g.setClip(x, y, 1, 1); dx = x * sourceWidth / width; g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP); } } Image immutableThumb = Image.createImage(thumb); return immutableThumb; } public static void PrintCapabilities(String caption, byte[] caps) { if (caps == null) return; for (int n = 0; n < caps.length; n += 16) { if (caps.length - n < 15) continue; byte[] b = new byte[16]; System.arraycopy(caps, n, b, 0, 16); String bytes = new String(); bytes += "{"; for (int i = 0; i < b.length; i++) { if ((i == 4) || (i == 6) || (i == 8) || (i == 10)) { bytes += "-"; } String bj = Integer.toHexString(b[i] & 0xFF); if (bj.length() == 1) bj = "0"+bj; bytes += bj; } bytes += "}"; DebugLog.addText(caption + bytes); } } // Password encryption key public static final byte[] PASSENC_KEY = explodeToBytes( "F3,26,81,C4,39,86,DB,92,71,A3,B9,E6,53,7A,95,7C", ',', 16); // Online status (set values) public static final int SET_STATUS_AWAY = 0x0001; public static final int SET_STATUS_CHAT = 0x0020; public static final int SET_STATUS_DND = 0x0013; public static final int SET_STATUS_INVISIBLE = 0x0100; public static final int SET_STATUS_NA = 0x0005; public static final int SET_STATUS_OCCUPIED = 0x0011; public static final int SET_STATUS_ONLINE = 0x0000; public static final int SET_STATUS_EVIL = 0x3000; public static final int SET_STATUS_DEPRESSION = 0x4000; public static final int SET_STATUS_HOME = 0x5000; public static final int SET_STATUS_WORK = 0x6000; public static final int SET_STATUS_LUNCH = 0x2001; // Counter variable private static int counter = 0; public synchronized static int getCounter() { counter++; return (counter); } public static String toHexString(byte[] b) { StringBuffer sb = new StringBuffer(b.length * 2); for (int i = 0; i < b.length; i++) { // look up high nibble char sb.append(hexChar[(b[i] & 0xf0) >>> 4]); // look up low nibble char sb.append(hexChar[b[i] & 0x0f]); sb.append(" "); if ((i != 0) && ((i % 15) == 0)) sb.append("\n"); } return sb.toString(); } // table to convert a nibble to a hex char. private static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; // Extracts the byte from the buffer (buf) at position off public static int getByte(byte[] buf, int off) { int val; val = ((int) buf[off]) & 0x000000FF; return (val); } // Puts the specified byte (val) into the buffer (buf) at position off public static void putByte(byte[] buf, int off, int val) { buf[off] = (byte) (val & 0x000000FF); } // Extracts the word from the buffer (buf) at position off using the specified byte ordering (bigEndian) public static int getWord(byte[] buf, int off, boolean bigEndian) { int val; if (bigEndian) { val = (((int) buf[off]) << 8) & 0x0000FF00; val |= (((int) buf[++off])) & 0x000000FF; } else // Little endian { val = (((int) buf[off])) & 0x000000FF; val |= (((int) buf[++off]) << 8) & 0x0000FF00; } return (val); } static public DataInputStream getDataInputStream(byte[] array, int offset) { return new DataInputStream(new ByteArrayInputStream(array, offset, array.length - offset)); } static public int getWord(DataInputStream stream, boolean bigEndian) throws IOException { return bigEndian ? stream.readUnsignedShort() : ((int) stream .readByte() & 0x00FF) | (((int) stream.readByte() << 8) & 0xFF00); } static public String readAsciiz(DataInputStream stream) throws IOException { int len = Util.getWord(stream, false); if (len == 0) return new String(); byte[] buffer = new byte[len]; stream.readFully(buffer); return Util.byteArrayToString(buffer); } static public void writeWord(ByteArrayOutputStream stream, int value, boolean bigEndian) { if (bigEndian) { stream.write(((value & 0xFF00) >> 8) & 0xFF); stream.write(value & 0xFF); } else { stream.write(value & 0xFF); stream.write(((value & 0xFF00) >> 8) & 0xFF); } } static public void writeByteArray(ByteArrayOutputStream stream, byte[] array) { try { stream.write(array); } catch (Exception e) { e.printStackTrace(); } } static public void writeDWord(ByteArrayOutputStream stream, int value, boolean bigEndian) { if (bigEndian) { stream.write(((value & 0xFF000000) >> 24) & 0xFF); stream.write(((value & 0xFF0000) >> 16) & 0xFF); stream.write(((value & 0xFF00) >> 8) & 0xFF); stream.write(value & 0xFF); } else { stream.write(value & 0xFF); stream.write(((value & 0xFF00) >> 8) & 0xFF); stream.write(((value & 0xFF0000) >> 16) & 0xFF); stream.write(((value & 0xFF000000) >> 24) & 0xFF); } } public static int getDWord(ByteArrayInputStream stream, boolean bigEndian) { try { int byte1 = stream.read()&0xFF; int byte2 = stream.read()&0xFF; int byte3 = stream.read()&0xFF; int byte4 = stream.read()&0xFF; return bigEndian ? (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | (byte4) : (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | (byte1); } catch (Exception e) { } return -1; } public static long getQWord(ByteArrayInputStream stream, boolean bigEndian) { try { long result = 0; long value; for (int i = 0; i < 8; i++) { if (bigEndian) result <<= 8; else result >>= 8; value = stream.read()&0xFF; if (bigEndian) result |= value; else result |= (value << 56); } return result; } catch (Exception e) { } return -1; } public static int getWord(ByteArrayInputStream stream, boolean bigEndian) { try { int byte1 = stream.read()&0xFF; int byte2 = stream.read()&0xFF; return bigEndian ? (byte1 << 8) | (byte2) : (byte2 << 8) | (byte1); } catch (Exception e) {} return -1; } public static String getLenAndString(ByteArrayInputStream stream, int lenLen) { int len = (lenLen == 2) ? getWord(stream, true) : stream.read(); if (len == 0) return new String(); byte[] data = new byte[len]; try {stream.read(data);} catch (Exception e) { return new String(); } return byteArrayToString(data); } static public void writeByte(ByteArrayOutputStream stream, int value) { stream.write(value); } static public void writeLenAndString(ByteArrayOutputStream stream, String value, boolean utf8) { byte[] raw = Util.stringToByteArray(value, utf8); writeWord(stream, raw.length, true); stream.write(raw, 0, raw.length); } static public void writeLenLEAndStringAsciiz(ByteArrayOutputStream stream, String value) { byte[] raw = Util.stringToByteArray(value, false); writeWord(stream, raw.length + 1, false); writeByteArray(stream, raw); writeByte(stream, 0); } static public void writeAsciizTLV(int type, ByteArrayOutputStream stream, String value, boolean bigEndian) { writeWord(stream, type, bigEndian); byte[] raw = Util.stringToByteArray(value); writeWord(stream, raw.length + 3, false); writeWord(stream, raw.length + 1, false); stream.write(raw, 0, raw.length); stream.write(0); } static public void writeAsciizTLV(int type, ByteArrayOutputStream stream, String value) { writeAsciizTLV(type, stream, value, true); } static public void writeTLV(ByteArrayOutputStream stream, int type, byte[] data, boolean bigEndian) { writeWord(stream, type, bigEndian); if (data != null) { writeWord(stream, data.length, bigEndian); stream.write(data, 0, data.length); } else writeWord(stream, 0, bigEndian); } // Extracts the word from the buffer (buf) at position off using big endian byte ordering public static int getWord(byte[] buf, int off) { return (Util.getWord(buf, off, true)); } // Puts the specified word (val) into the buffer (buf) at position off using the specified byte ordering (bigEndian) public static void putWord(byte[] buf, int off, int val, boolean bigEndian) { if (bigEndian) { buf[off] = (byte) ((val >> 8) & 0x000000FF); buf[++off] = (byte) ((val) & 0x000000FF); } else // Little endian { buf[off] = (byte) ((val) & 0x000000FF); buf[++off] = (byte) ((val >> 8) & 0x000000FF); } } // Puts the specified word (val) into the buffer (buf) at position off using big endian byte ordering public static void putWord(byte[] buf, int off, int val) { Util.putWord(buf, off, val, true); } // Extracts the double from the buffer (buf) at position off using the specified byte ordering (bigEndian) public static long getDWord(byte[] buf, int off, boolean bigEndian) { long val; if (bigEndian) { val = (((long) buf[off]) << 24) & 0xFF000000; val |= (((long) buf[++off]) << 16) & 0x00FF0000; val |= (((long) buf[++off]) << 8) & 0x0000FF00; val |= (((long) buf[++off])) & 0x000000FF; } else // Little endian { val = (((long) buf[off])) & 0x000000FF; val |= (((long) buf[++off]) << 8) & 0x0000FF00; val |= (((long) buf[++off]) << 16) & 0x00FF0000; val |= (((long) buf[++off]) << 24) & 0xFF000000; } return (val); } // Extracts the double from the buffer (buf) at position off using big endian byte ordering public static long getDWord(byte[] buf, int off) { return (Util.getDWord(buf, off, true)); } // Puts the specified double (val) into the buffer (buf) at position off using the specified byte ordering (bigEndian) public static void putDWord(byte[] buf, int off, long val, boolean bigEndian) { if (bigEndian) { buf[off] = (byte) ((val >> 24) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 16) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 8) & 0x00000000000000FF); buf[++off] = (byte) ((val) & 0x00000000000000FF); } else // Little endian { buf[off] = (byte) ((val) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 8) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 16) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 24) & 0x00000000000000FF); } } // Puts the specified double (val) into the buffer (buf) at position off using big endian byte ordering public static void putDWord(byte[] buf, int off, long val) { Util.putDWord(buf, off, val, true); } // Puts the specified double (val) into the buffer (buf) at position off using the specified byte ordering (bigEndian) public static void putQWord(byte[] buf, int off, long val, boolean bigEndian) { if (bigEndian) { buf[off] = (byte) ((val >> 56) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 48) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 40) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 32) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 24) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 16) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 8) & 0x00000000000000FF); buf[++off] = (byte) ((val) & 0x00000000000000FF); } else // Little endian { buf[off] = (byte) ((val) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 8) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 16) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 24) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 32) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 40) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 48) & 0x00000000000000FF); buf[++off] = (byte) ((val >> 56) & 0x00000000000000FF); } } // Puts the specified double (val) into the buffer (buf) at position off using big endian byte ordering
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -