📄 util.java
字号:
public static void putQWord(byte[] buf, int off, long val) { Util.putDWord(buf, off, val, true); } // Extracts the double from the buffer (buf) at position off using the specified byte ordering (bigEndian) public static long getQWord(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); } // getTlv(byte[] buf, int off) => byte[] public static byte[] getTlv(byte[] buf, int off) { if (off + 4 > buf.length) return (null); // Length check (#1) int length = Util.getWord(buf, off + 2); if (off + 4 + length > buf.length) return (null); // Length check (#2) byte[] value = new byte[length]; System.arraycopy(buf, off + 4, value, 0, length); return (value); } // Extracts a string from the buffer (buf) starting at position off, ending at position off+len public static String byteArrayToString(byte[] buf, int off, int len, boolean utf8) { // Length check if (buf.length < off + len) { return (null); } // Remove \0's at the end while ((len > 0) && (buf[off + len - 1] == 0x00)) { len--; } // Read string in UTF-8 format if (utf8) { try { byte[] buf2 = new byte[len + 2]; Util.putWord(buf2, 0, len); System.arraycopy(buf, off, buf2, 2, len); ByteArrayInputStream bais = new ByteArrayInputStream(buf2); DataInputStream dis = new DataInputStream(bais); return (dis.readUTF()); } catch (Exception e) { // do nothing } } // CP1251 or default character encoding? if (Options.getBoolean(Options.OPTION_CP1251_HACK)) { return (byteArray1251ToString(buf, off, len)); } else { return (new String(buf, off, len)); } } // Extracts a string from the buffer (buf) starting at position off, ending at position off+len public static String byteArrayToString(byte[] buf, int off, int len) { return (Util.byteArrayToString(buf, off, len, false)); } // Converts the specified buffer (buf) to a string public static String byteArrayToString(byte[] buf, boolean utf8) { return (Util.byteArrayToString(buf, 0, buf.length, utf8)); } // Converts the specified buffer (buf) to a string public static String byteArrayToString(byte[] buf) { return (Util.byteArrayToString(buf, 0, buf.length, false)); } // Converts the specific 4 byte max buffer to an unsigned long public static long byteArrayToLong(byte[] b) { long l = 0; l |= b[0] & 0xFF; l <<= 8; l |= b[1] & 0xFF; l <<= 8; if (b.length > 3) { l |= b[2] & 0xFF; l <<= 8; l |= b[3] & 0xFF; } return l; } // Converts a byte array to a hex string public static String byteArrayToHexString(byte[] buf) { StringBuffer hexString = new StringBuffer(buf.length); String hex; for (int i = 0; i < buf.length; i++) { hex = Integer.toHexString(0x0100 + (buf[i] & 0x00FF)).substring(1); hexString.append((hex.length() < 2 ? "0" : "") + hex); } return hexString.toString(); } // Converts the specified string (val) to a byte array public static byte[] stringToByteArray(String val, boolean utf8) { // Write string in UTF-8 format if (utf8) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeUTF(val); byte[] raw = baos.toByteArray(); byte[] result = new byte[raw.length - 2]; System.arraycopy(raw, 2, result, 0, raw.length - 2); return result; } catch (Exception e) { // Do nothing } } // CP1251 or default character encoding? if (Options.getBoolean(Options.OPTION_CP1251_HACK)) { return (stringToByteArray1251(val)); } else { return (val.getBytes()); } } // Converts the specified string (val) to a byte array public static byte[] stringToByteArray(String val) { return (Util.stringToByteArray(val, false)); } // Converts the specified string to UCS-2BE public static byte[] stringToUcs2beByteArray(String val) { byte[] ucs2be = new byte[val.length() * 2]; for (int i = 0; i < val.length(); i++) { Util.putWord(ucs2be, i * 2, (int) val.charAt(i)); } return (ucs2be); } // Extract a UCS-2BE string from the specified buffer (buf) starting at position off, ending at position off+len public static String ucs2beByteArrayToString(byte[] buf, int off, int len) { // Length check if ((off + len > buf.length) || (len % 2 != 0)) { return (null); } // Convert StringBuffer sb = new StringBuffer(); for (int i = off; i < off + len; i += 2) { sb.append((char) Util.getWord(buf, i)); } return (sb.toString()); } // Extracts a UCS-2BE string from the specified buffer (buf) public static String ucs2beByteArrayToString(byte[] buf) { return (Util.ucs2beByteArrayToString(buf, 0, buf.length)); } public static void showBytes(byte[] data) { StringBuffer buffer1 = new StringBuffer(), buffer2 = new StringBuffer(); int charaster; String hex; for (int i = 0; i < data.length; i++) { charaster = ((int) data[i]) & 0xFF; buffer1.append(charaster < ' ' || charaster >= 128 ? '.' : (char) charaster); hex = Integer.toHexString(((int) data[i]) & 0xFF); buffer2.append(hex.length() == 1 ? "0" + hex : hex); buffer2.append(" "); if (((i % 16) == 15) || (i == (data.length - 1))) { while (buffer2.length() < 16 * 3) buffer2.append(' '); System.out.print(buffer2.toString()); System.out.println(buffer1.toString()); buffer1.setLength(0); buffer2.setLength(0); } } System.out.println(); } // Removes all CR occurences public static String removeCr(String val) { StringBuffer result = new StringBuffer(); for (int i = 0; i < val.length(); i++) { char chr = val.charAt(i); if ((chr == 0) || (chr == '\r')) continue; result.append(chr); } return result.toString(); } // Restores CRLF sequense from LF public static String restoreCrLf(String val) { StringBuffer result = new StringBuffer(); int size = val.length(); char chr; for (int i = 0; i < size; i++) { chr = val.charAt(i); if (chr == '\r') continue; if (chr == '\n') result.append("\r\n"); else result.append(chr); } return result.toString(); } public static String removeClRfAndTabs(String val) { int len = val.length(); char[] dst = new char[len]; char chr; for (int i = 0; i < len; i++) { chr = val.charAt(i); if ((chr == '\n') || (chr == '\r') || (chr == '\t')) chr = ' '; dst[i] = chr; } return new String(dst, 0, len); } // Compare to byte arrays (return true if equals, false otherwise) public static boolean byteArrayEquals(byte[] buf1, int off1, byte[] buf2, int off2, int len) { // Length check if ((off1 + len > buf1.length) || (off2 + len > buf2.length)) { return (false); } // Compare bytes, stop at first mismatch for (int i = 0; i < len; i++) { if (buf1[off1 + i] != buf2[off2 + i]) { return (false); } } // Return true if this point is reached return (true); } public static boolean byteArrayIsEmpty(byte[] buf, int len) { if (buf == null) return true; // Length check if (len > buf.length) { len = buf.length; } // Compare bytes, stop at first mismatch for (int i = 0; i < len; i++) { if (buf[i] != 0) { return (false); } } // Return true if this point is reached return (true); } // DeScramble password public static byte[] decipherPassword(byte[] buf) { byte[] ret = new byte[buf.length]; for (int i = 0; i < buf.length; i++) { ret[i] = (byte) (buf[i] ^ Util.PASSENC_KEY[i % 16]); } return (ret); } // translateStatus(long status) => void public static int translateStatusReceived(int status) { if (status == ContactList.STATUS_OFFLINE) return (ContactList.STATUS_OFFLINE); if ((status & ContactList.STATUS_DND) != 0) return (ContactList.STATUS_DND); if ((status & ContactList.STATUS_INVISIBLE) != 0) return (ContactList.STATUS_INVISIBLE); if ((status & ContactList.STATUS_OCCUPIED) != 0) return (ContactList.STATUS_OCCUPIED); if ((status & ContactList.STATUS_NA) != 0) return (ContactList.STATUS_NA); if ((status & ContactList.STATUS_CHAT) != 0) return (ContactList.STATUS_CHAT); if ((status & ContactList.STATUS_LUNCH) == ContactList.STATUS_LUNCH) return (ContactList.STATUS_LUNCH); if ((status & ContactList.STATUS_EVIL) == ContactList.STATUS_EVIL) return (ContactList.STATUS_EVIL); if ((status & ContactList.STATUS_HOME) == ContactList.STATUS_HOME) return (ContactList.STATUS_HOME); if ((status & ContactList.STATUS_WORK) == ContactList.STATUS_WORK) return (ContactList.STATUS_WORK); if ((status & ContactList.STATUS_AWAY) != 0) return (ContactList.STATUS_AWAY); if ((status & ContactList.STATUS_DEPRESSION) == ContactList.STATUS_DEPRESSION) return (ContactList.STATUS_DEPRESSION); return (ContactList.STATUS_ONLINE); } // Get online status set value public static int translateStatusSend(int status) { switch (status) { case ContactList.STATUS_AWAY: return (Util.SET_STATUS_AWAY); case ContactList.STATUS_CHAT: return (Util.SET_STATUS_CHAT); case ContactList.STATUS_DND: return (Util.SET_STATUS_DND); case ContactList.STATUS_INVISIBLE: return (Util.SET_STATUS_INVISIBLE); case ContactList.STATUS_INVIS_ALL: return (Util.SET_STATUS_INVISIBLE); case ContactList.STATUS_NA: return (Util.SET_STATUS_NA); case ContactList.STATUS_OCCUPIED: return (Util.SET_STATUS_OCCUPIED); case ContactList.STATUS_EVIL: return (Util.SET_STATUS_EVIL); case ContactList.STATUS_DEPRESSION: return (Util.SET_STATUS_DEPRESSION); case ContactList.STATUS_HOME: return (Util.SET_STATUS_HOME); case ContactList.STATUS_WORK: return (Util.SET_STATUS_WORK); case ContactList.STATUS_LUNCH: return (Util.SET_STATUS_LUNCH); default: return (Util.SET_STATUS_ONLINE); } } // If the numer has only one digit add a 0 public static String makeTwo(int number) { if (number < 10) { return ("0" + String.valueOf(number)); } else { return (String.valueOf(number)); } } // Byte array IP to String public static String ipToString(byte[] ip) { if (ip == null) return null; StringBuffer strIP = new StringBuffer(); for (int i = 0; i < 4; i++) { int tmp = (int) ip[i] & 0xFF; if (strIP.length() != 0) strIP.append('.'); strIP.append(tmp); } return strIP.toString(); } // String IP to byte array public static byte[] ipToByteArray(String ip) { byte[] arrIP = explodeToBytes(ip, '.', 10); return ((arrIP == null) || (arrIP.length != 4)) ? null : arrIP; } //#sijapp cond.if modules_PROXY is "true"# // Try to parse string IP public static boolean isIP(String ip) { boolean isTrueIp = false; try { isTrueIp = (ipToByteArray(ip) != null); } catch (NumberFormatException e) { return false; } return isTrueIp; } //#sijapp cond.end # // Check is data array utf-8 string public static boolean isDataUTF8(byte[] array, int start, int length) { if (length == 0) return false; if (array.length < (start + length)) return false; int seqLen; byte bt; for (int i = start, len = length; len > 0;) { seqLen = 0; bt = array[i++]; len--; if ((bt & 0xE0) == 0xC0) seqLen = 1; else if ((bt & 0xF0) == 0xE0) seqLen = 2; else if ((bt & 0xF8) == 0xF0) seqLen = 3; else if ((bt & 0xFC) == 0xF8) seqLen = 4; else if ((bt & 0xFE) == 0xFC) seqLen = 5; if (seqLen == 0) { if ((bt & 0x80) == 0x80) return false; else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -