📄 smppio.java
字号:
* bytes to generate). * @return An array of length len containing the byte representation of num. */ public static final byte[] longToBytes(long num, int len) { return (longToBytes(num, len, null, 0)); } /** * Convert a long to a byte array in MSB first order. * * @param num * The number to store * @param len * The length of the integer to convert (that is, the number of * bytes to generate). * @param b * the byte array to write the integer to. * @param offset * the offset in <code>b</code> to write the integer to. * @return An array of length len containing the byte representation of num. */ public static final byte[] longToBytes(long num, int len, byte[] b, int offset) { if (b == null) { b = new byte[len]; offset = 0; } long sw = ((len - 1) * 8); long mask = (0xffL << sw); for (int l = 0; l < len; l++) { b[offset + l] = (byte) ((num & mask) >>> sw); sw -= 8; mask >>>= 8; } return (b); } /** * Convert a byte array (or part thereof) into an integer. The byte array * should be in big-endian form. That is, the byte at index 'offset' should * be the MSB. * * @param b * The array containing the bytes * @param offset * The array index of the MSB * @param size * The number of bytes to convert into the integer * @return An integer value represented by the specified bytes. */ public static final int bytesToInt(byte[] b, int offset, int size) { int num = 0; int sw = 8 * (size - 1); for (int loop = 0; loop < size; loop++) { num |= ((int) b[offset + loop] & 0x00ff) << sw; sw -= 8; } return (num); } /** * Convert a byte array (or part thereof) into a long. The byte array should * be in big-endian form. That is, the byte at index 'offset' should be the * MSB. * * @param b * The array containing the bytes * @param offset * The array index of the MSB * @param size * The number of bytes to convert into the long * @return An long value represented by the specified bytes. */ public static final long bytesToLong(byte[] b, int offset, int size) { long num = 0; long sw = 8L * ((long) size - 1L); for (int loop = 0; loop < size; loop++) { num |= ((long) b[offset + loop] & 0x00ff) << sw; sw -= 8; } return (num); } /** * Write the byte representation of an integer to an OutputStream in MSB * order. * * @param x * The integer to write * @param len * The number of bytes in this integer (usually either 1 or 4) * @param out * The OutputStream to write the integer to * @throws java.io.IOException * If an I/O error occurs. * @see java.io.OutputStream */ public static void writeInt(int x, int len, OutputStream out) throws java.io.IOException { out.write(intToBytes(x, len)); } /** * Write a String to an OutputStream followed by a NUL byte * * @param s * The string to write * @param out * The output stream to write to * @throws java.io.IOException * If an I/O error occurs * @see java.io.OutputStream */ public static void writeCString(String s, OutputStream out) throws java.io.IOException { writeString(s, out); out.write((byte) 0); } /** * Write a String of specified length to an OutputStream * * @param s * The String to write * @param len * The length of the String to write. If this is longer than the * length of the String, the whole String will be sent. * @param out * The OutputStream to use * @throws java.io.IOException * If an I/O error occurs * @see java.io.OutputStream */ public static void writeString(String s, int len, OutputStream out) throws java.io.IOException { if (s == null) return; if (len > s.length()) writeString(s, out); else writeString(s.substring(0, len), out); } /** * Write a String in it's entirety to an OutputStream * * @param s * The String to write * @param out * The OutputStream to write to * @throws java.io.IOException * If an I/O error occurs * @see java.io.OutputStream */ public static void writeString(String s, OutputStream out) throws java.io.IOException { if (s == null) return; out.write(s.getBytes()); } /* * public static final void main(String[] args) { // Integer/byte conversion * tests int[] twoByte_vals = { 0x4512, 0xdead, 0xcafe, 0xfcfc }; byte[][] * twoByte_bytes = { { 0x45, 0x12 }, { (byte)0xde, (byte)0xad }, { * (byte)0xca, (byte)0xfe }, { (byte)0xfc, (byte)0xfc } }; * * int[] threeByte_vals = { 0x112233, 0x432165, 0xf88ee2 }; byte[][] * threeByte_bytes = { { 0x11, 0x22, 0x33 }, { 0x43, 0x21, 0x65 }, { * (byte)0xf8, (byte)0x8e, (byte)0xe2 } }; * * int[] fourByte_vals = { 0xdeadbeef, 0xcafefeed, 0xbeeffeed }; byte[][] * fourByte_bytes = { { (byte)0xde, (byte)0xad, (byte)0xbe, (byte)0xef }, { * (byte)0xca, (byte)0xfe, (byte)0xfe, (byte)0xed }, { (byte)0xbe, * (byte)0xef, (byte)0xfe, (byte)0xed } }; * * boolean passed = true; passed &= b2iTest(twoByte_bytes, twoByte_vals, 2); * passed &= b2iTest(threeByte_bytes, threeByte_vals, 3); passed &= * b2iTest(fourByte_bytes, fourByte_vals, 4); * // C-string test.. try { String s = "This is a CString test. ASCII chars * only, please!"; byte[] sb = s.getBytes("US-ASCII"); byte[] sb1 = new * byte[sb.length + 1]; byte[] sb2; java.io.ByteArrayOutputStream os = new * java.io.ByteArrayOutputStream(); * * System.arraycopy(sb, 0, sb1, 0, sb.length); sb1[sb.length] = (byte)0; * writeCString(s, os); sb2 = os.toByteArray(); * * String s1 = readCString(sb1, 0); passed &= (s1.equals(s)); passed &= * java.util.Arrays.equals(sb1, sb2); } catch * (java.io.UnsupportedEncodingException x) { passed = false; * System.out.println("Unsupported encoding!"); * x.printStackTrace(System.out); } catch (java.io.IOException x) { passed = * false; x.printStackTrace(System.out); } * * * if (!passed) System.out.println("Test failed."); else * System.out.println("All tests passed."); } // Run a byte array/integer * conversion test. // @param bArray array of array of bytes representing * the values. // @param vArray array of values. // @param size the number * of bytes per integer. private static final boolean b2iTest(byte[][] * bArray, int[] vArray, int size) { boolean ret = true; for (int i = 0; i < * vArray.length; i++) { int ri = bytesToInt(bArray[i], 0, size); byte[] bo = * intToBytes(vArray[i], size); * * if (ri != vArray[i]) { ret = false; System.out.println("bytesToInt failed * on " + size + " bytes, pos " + i); System.out.println("\tValue = " + * vArray[i] + ", ri = " + ri); } * * if (!java.util.Arrays.equals(bo, bArray[i])) { ret = false; * System.out.println("intToBytes failed in " + size + " bytes, pos " + i); * * System.out.print("Fixed: "); for (int j = 0; j < bo.length; j++) { * System.out.print(Integer.toHexString(new Byte( bo[j]).intValue()&0xff) + " * "); } System.out.print("Generated:"); for (int j = 0; j < * bArray[i].length; j++) { System.out.print(Integer.toHexString(new Byte( * bArray[i][j]).intValue()&0xff) + " "); } } } return (ret); } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -