Here you can find the source of arrayToRLEString(int[] a)
static public final String arrayToRLEString(int[] a)
//package com.java2s; public class Main { /**//w w w. jav a 2 s .c o m * The ESCAPE character is used during run-length encoding. It signals * a run of identical chars. */ private static final char ESCAPE = '\uA5A5'; /** * The ESCAPE_BYTE character is used during run-length encoding. It signals * a run of identical bytes. */ static final byte ESCAPE_BYTE = (byte) 0xA5; /** * Construct a string representing an int array. Use run-length encoding. * A character represents itself, unless it is the ESCAPE character. Then * the following notations are possible: * ESCAPE ESCAPE ESCAPE literal * ESCAPE n c n instances of character c * Since an encoded run occupies 3 characters, we only encode runs of 4 or * more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. * If we encounter a run where n == ESCAPE, we represent this as: * c ESCAPE n-1 c * The ESCAPE value is chosen so as not to collide with commonly * seen values. */ ///CLOVER:OFF static public final String arrayToRLEString(int[] a) { StringBuffer buffer = new StringBuffer(); appendInt(buffer, a.length); int runValue = a[0]; int runLength = 1; for (int i = 1; i < a.length; ++i) { int s = a[i]; if (s == runValue && runLength < 0xFFFF) { ++runLength; } else { encodeRun(buffer, runValue, runLength); runValue = s; runLength = 1; } } encodeRun(buffer, runValue, runLength); return buffer.toString(); } /** * Construct a string representing a short array. Use run-length encoding. * A character represents itself, unless it is the ESCAPE character. Then * the following notations are possible: * ESCAPE ESCAPE ESCAPE literal * ESCAPE n c n instances of character c * Since an encoded run occupies 3 characters, we only encode runs of 4 or * more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. * If we encounter a run where n == ESCAPE, we represent this as: * c ESCAPE n-1 c * The ESCAPE value is chosen so as not to collide with commonly * seen values. */ ///CLOVER:OFF static public final String arrayToRLEString(short[] a) { StringBuffer buffer = new StringBuffer(); // for (int i=0; i<a.length; ++i) buffer.append((char) a[i]); buffer.append((char) (a.length >> 16)); buffer.append((char) a.length); short runValue = a[0]; int runLength = 1; for (int i = 1; i < a.length; ++i) { short s = a[i]; if (s == runValue && runLength < 0xFFFF) ++runLength; else { encodeRun(buffer, runValue, runLength); runValue = s; runLength = 1; } } encodeRun(buffer, runValue, runLength); return buffer.toString(); } /** * Construct a string representing a char array. Use run-length encoding. * A character represents itself, unless it is the ESCAPE character. Then * the following notations are possible: * ESCAPE ESCAPE ESCAPE literal * ESCAPE n c n instances of character c * Since an encoded run occupies 3 characters, we only encode runs of 4 or * more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. * If we encounter a run where n == ESCAPE, we represent this as: * c ESCAPE n-1 c * The ESCAPE value is chosen so as not to collide with commonly * seen values. */ static public final String arrayToRLEString(char[] a) { StringBuffer buffer = new StringBuffer(); buffer.append((char) (a.length >> 16)); buffer.append((char) a.length); char runValue = a[0]; int runLength = 1; for (int i = 1; i < a.length; ++i) { char s = a[i]; if (s == runValue && runLength < 0xFFFF) ++runLength; else { encodeRun(buffer, (short) runValue, runLength); runValue = s; runLength = 1; } } encodeRun(buffer, (short) runValue, runLength); return buffer.toString(); } /** * Construct a string representing a byte array. Use run-length encoding. * Two bytes are packed into a single char, with a single extra zero byte at * the end if needed. A byte represents itself, unless it is the * ESCAPE_BYTE. Then the following notations are possible: * ESCAPE_BYTE ESCAPE_BYTE ESCAPE_BYTE literal * ESCAPE_BYTE n b n instances of byte b * Since an encoded run occupies 3 bytes, we only encode runs of 4 or * more bytes. Thus we have n > 0 and n != ESCAPE_BYTE and n <= 0xFF. * If we encounter a run where n == ESCAPE_BYTE, we represent this as: * b ESCAPE_BYTE n-1 b * The ESCAPE_BYTE value is chosen so as not to collide with commonly * seen values. */ static public final String arrayToRLEString(byte[] a) { StringBuffer buffer = new StringBuffer(); buffer.append((char) (a.length >> 16)); buffer.append((char) a.length); byte runValue = a[0]; int runLength = 1; byte[] state = new byte[2]; for (int i = 1; i < a.length; ++i) { byte b = a[i]; if (b == runValue && runLength < 0xFF) ++runLength; else { encodeRun(buffer, runValue, runLength, state); runValue = b; runLength = 1; } } encodeRun(buffer, runValue, runLength, state); // We must save the final byte, if there is one, by padding // an extra zero. if (state[0] != 0) appendEncodedByte(buffer, (byte) 0, state); return buffer.toString(); } private static final void appendInt(StringBuffer buffer, int value) { buffer.append((char) (value >>> 16)); buffer.append((char) (value & 0xFFFF)); } /** * Encode a run, possibly a degenerate run (of < 4 values). * @param length The length of the run; must be > 0 && <= 0xFFFF. */ ///CLOVER:OFF 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 } } /** * 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, short value, int length) { if (length < 4) { for (int j = 0; j < length; ++j) { if (value == (int) ESCAPE) buffer.append(ESCAPE); buffer.append((char) value); } } else { if (length == (int) ESCAPE) { if (value == (int) ESCAPE) buffer.append(ESCAPE); buffer.append((char) value); --length; } buffer.append(ESCAPE); buffer.append((char) length); buffer.append((char) value); // 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 && <= 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 } } /** * 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; } } }