📄 utility.java
字号:
* Encode a run, possibly a degenerate run (of < 4 values). * @param length The length of the run; must be > 0 && <= 0xFF. */ private static final void encodeRun(StringBuffer buffer, byte value, int length, byte[] state) { if (length < 4) { for (int j=0; j<length; ++j) { if (value == ESCAPE_BYTE) appendEncodedByte(buffer, ESCAPE_BYTE, state); appendEncodedByte(buffer, value, state); } } else { if (length == ESCAPE_BYTE) { if (value == ESCAPE_BYTE) { appendEncodedByte(buffer, ESCAPE_BYTE, state); } appendEncodedByte(buffer, value, state); --length; } appendEncodedByte(buffer, ESCAPE_BYTE, state); appendEncodedByte(buffer, (byte)length, state); appendEncodedByte(buffer, value, state); // Don't need to escape this value } } /** * Encode a run, possibly a degenerate run (of < 4 values). * @param length The length of the run; must be > 0 && <= 0xFFFF. */ private static final void encodeRun(StringBuffer buffer, int value, int length) { if (length < 4) { for (int j=0; j<length; ++j) { if (value == ESCAPE) { appendInt(buffer, value); } appendInt(buffer, value); } } else { if (length == (int) ESCAPE) { if (value == (int) ESCAPE) { appendInt(buffer, ESCAPE); } appendInt(buffer, value); --length; } appendInt(buffer, ESCAPE); appendInt(buffer, length); appendInt(buffer, value); // Don't need to escape this value } } private static final void appendInt(StringBuffer buffer, int value) { buffer.append((char)(value >>> 16)); buffer.append((char)(value & 0xFFFF)); } /** * Append a byte to the given StringBuffer, packing two bytes into each * character. The state parameter maintains intermediary data between * calls. * @param state A two-element array, with state[0] == 0 if this is the * first byte of a pair, or state[0] != 0 if this is the second byte * of a pair, in which case state[1] is the first byte. */ private static final void appendEncodedByte(StringBuffer buffer, byte value, byte[] state) { if (state[0] != 0) { char c = (char) ((state[1] << 8) | (((int) value) & 0xFF)); buffer.append(c); state[0] = 0; } else { state[0] = 1; state[1] = value; } } /** * Construct an array of shorts from a run-length encoded string. */ public static final short[] RLEStringToShortArray(String s) { int length = (((int) s.charAt(0)) << 16) | ((int) s.charAt(1)); short[] array = new short[length]; int ai = 0; for (int i=2; i<s.length(); ++i) { char c = s.charAt(i); if (c == ESCAPE) { c = s.charAt(++i); if (c == ESCAPE) { array[ai++] = (short) c; } else { int runLength = (int) c; short runValue = (short) s.charAt(++i); for (int j=0; j<runLength; ++j) { array[ai++] = runValue; } } } else { array[ai++] = (short) c; } } if (ai != length) { throw new InternalError("Bad run-length encoded short array"); } return array; } /** * Construct an array of bytes from a run-length encoded string. */ public static final byte[] RLEStringToByteArray(String s) { int length = (((int) s.charAt(0)) << 16) | ((int) s.charAt(1)); byte[] array = new byte[length]; boolean nextChar = true; char c = 0; int node = 0; int runLength = 0; int i = 2; for (int ai=0; ai<length; ) { // This part of the loop places the next byte into the local // variable 'b' each time through the loop. It keeps the // current character in 'c' and uses the boolean 'nextChar' // to see if we've taken both bytes out of 'c' yet. byte b; if (nextChar) { c = s.charAt(i++); b = (byte) (c >> 8); nextChar = false; } else { b = (byte) (c & 0xFF); nextChar = true; } // This part of the loop is a tiny state machine which handles // the parsing of the run-length encoding. This would be simpler // if we could look ahead, but we can't, so we use 'node' to // move between three nodes in the state machine. switch (node) { case 0: // Normal idle node if (b == ESCAPE_BYTE) { node = 1; } else { array[ai++] = b; } break; case 1: // We have seen one ESCAPE_BYTE; we expect either a second // one, or a run length and value. if (b == ESCAPE_BYTE) { array[ai++] = ESCAPE_BYTE; node = 0; } else { runLength = b; // Interpret signed byte as unsigned if (runLength < 0) { runLength += 0x100; } node = 2; } break; case 2: // We have seen an ESCAPE_BYTE and length byte. We interpret // the next byte as the value to be repeated. for (int j=0; j<runLength; ++j) { array[ai++] = b; } node = 0; break; } } if (node != 0) { throw new InternalError("Bad run-length encoded byte array"); } if (i != s.length()) { throw new InternalError("Excess data in RLE byte array string"); } return array; } /** * Construct an array of shorts from a run-length encoded string. */ static public final char[] RLEStringToCharArray(String s) { int length = (((int) s.charAt(0)) << 16) | ((int) s.charAt(1)); char[] array = new char[length]; int ai = 0; for (int i=2; i<s.length(); ++i) { char c = s.charAt(i); if (c == ESCAPE) { c = s.charAt(++i); if (c == ESCAPE) { array[ai++] = c; } else { int runLength = (int) c; char runValue = s.charAt(++i); for (int j=0; j<runLength; ++j) array[ai++] = runValue; } } else { array[ai++] = c; } } if (ai != length) throw new InternalError("Bad run-length encoded short array"); return array; } /** * Construct an array of ints from a run-length encoded string. */ static public final int[] RLEStringToIntArray(String s) { int length = getInt(s, 0); int[] array = new int[length]; int ai = 0, i = 1; int maxI = s.length() / 2; while (ai < length && i < maxI) { int c = getInt(s, i++); if (c == ESCAPE) { c = getInt(s, i++); if (c == ESCAPE) { array[ai++] = c; } else { int runLength = c; int runValue = getInt(s, i++); for (int j=0; j<runLength; ++j) { array[ai++] = runValue; } } } else { array[ai++] = c; } } if (ai != length || i != maxI) { throw new InternalError("Bad run-length encoded int array"); } return array; } /** * Format a String for representation in a source file. This includes * breaking it into lines escaping characters using octal notation * when necessary (control characters and double quotes). */ public static final String formatForSource(String s) { StringBuffer buffer = new StringBuffer(); for (int i=0; i<s.length();) { if (i > 0) buffer.append("+\n"); buffer.append(" \""); int count = 11; while (i<s.length() && count<80) { char c = s.charAt(i++); if (c < '\u0020' || c == '"') { // Represent control characters and the double quote // using octal notation; otherwise the string we form // won't compile, since Unicode escape sequences are // processed before tokenization. buffer.append('\\'); buffer.append(HEX_DIGIT[(c & 0700) >> 6]); // HEX_DIGIT works for octal buffer.append(HEX_DIGIT[(c & 0070) >> 3]); buffer.append(HEX_DIGIT[(c & 0007)]); count += 4; } else if (c <= '\u007E') { buffer.append(c); count += 1; } else { buffer.append("\\u"); buffer.append(HEX_DIGIT[(c & 0xF000) >> 12]); buffer.append(HEX_DIGIT[(c & 0x0F00) >> 8]); buffer.append(HEX_DIGIT[(c & 0x00F0) >> 4]); buffer.append(HEX_DIGIT[(c & 0x000F)]); count += 6; } } buffer.append('"'); } return buffer.toString(); } public static final String hex(char ch) { StringBuffer buff = new StringBuffer(); return hex(ch, buff).toString(); } public static final StringBuffer hex(String src, StringBuffer buff) { if (src != null && buff != null) { int strLen = src.length(); int x = 0; hex(src.charAt(x), buff); while(x<strLen) { buff.append(','); hex(src.charAt(x++), buff); } } return buff; } public static final String hex(String str) { StringBuffer buff = new StringBuffer(); hex(str, buff); return buff.toString(); } public static final String hex(StringBuffer buff) { return hex(buff.toString()); } public static final StringBuffer hex(char ch, StringBuffer buff) { for (int shift = 12; shift >=0; shift-=4) { buff.append( HEX_DIGIT[(byte)((ch >> shift) & 0x0F)]); } return buff; } static final int getInt(String s, int i) { return (((int) s.charAt(2*i)) << 16) | (int) s.charAt(2*i+1); } static final char[] HEX_DIGIT = {'0','1','2','3','4','5','6','7', '8','9','A','B','C','D','E','F'};}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -