in src/main/java/org/apache/commons/compress/harmony/pack200/CpBands.java [532:599]
private void writeCpUtf8(final OutputStream out) throws IOException, Pack200Exception {
PackingUtils.log("Writing " + cp_Utf8.size() + " UTF8 entries...");
final int[] cpUtf8Prefix = new int[cp_Utf8.size() - 2];
final int[] cpUtf8Suffix = new int[cp_Utf8.size() - 1];
final List<Character> chars = new ArrayList<>();
final List<Integer> bigSuffix = new ArrayList<>();
final List<Character> bigChars = new ArrayList<>();
final Object[] cpUtf8Array = cp_Utf8.toArray();
final String first = ((CPUTF8) cpUtf8Array[1]).getUnderlyingString();
cpUtf8Suffix[0] = first.length();
addCharacters(chars, first.toCharArray());
for (int i = 2; i < cpUtf8Array.length; i++) {
final char[] previous = ((CPUTF8) cpUtf8Array[i - 1]).getUnderlyingString().toCharArray();
String currentStr = ((CPUTF8) cpUtf8Array[i]).getUnderlyingString();
final char[] current = currentStr.toCharArray();
int prefix = 0;
for (int j = 0; j < previous.length; j++) {
if (previous[j] != current[j]) {
break;
}
prefix++;
}
cpUtf8Prefix[i - 2] = prefix;
currentStr = currentStr.substring(prefix);
final char[] suffix = currentStr.toCharArray();
if (suffix.length > 1000) { // big suffix (1000 is arbitrary - can we
// do better?)
cpUtf8Suffix[i - 1] = 0;
bigSuffix.add(Integer.valueOf(suffix.length));
addCharacters(bigChars, suffix);
} else {
cpUtf8Suffix[i - 1] = suffix.length;
addCharacters(chars, suffix);
}
}
final int[] cpUtf8Chars = new int[chars.size()];
final int[] cpUtf8BigSuffix = new int[bigSuffix.size()];
final int[][] cpUtf8BigChars = new int[bigSuffix.size()][];
Arrays.setAll(cpUtf8Chars, i -> chars.get(i).charValue());
for (int i = 0; i < cpUtf8BigSuffix.length; i++) {
final int numBigChars = bigSuffix.get(i).intValue();
cpUtf8BigSuffix[i] = numBigChars;
cpUtf8BigChars[i] = new int[numBigChars];
Arrays.setAll(cpUtf8BigChars[i], j -> bigChars.remove(0).charValue());
}
byte[] encodedBand = encodeBandInt("cpUtf8Prefix", cpUtf8Prefix, Codec.DELTA5);
out.write(encodedBand);
PackingUtils.log("Wrote " + encodedBand.length + " bytes from cpUtf8Prefix[" + cpUtf8Prefix.length + "]");
encodedBand = encodeBandInt("cpUtf8Suffix", cpUtf8Suffix, Codec.UNSIGNED5);
out.write(encodedBand);
PackingUtils.log("Wrote " + encodedBand.length + " bytes from cpUtf8Suffix[" + cpUtf8Suffix.length + "]");
encodedBand = encodeBandInt("cpUtf8Chars", cpUtf8Chars, Codec.CHAR3);
out.write(encodedBand);
PackingUtils.log("Wrote " + encodedBand.length + " bytes from cpUtf8Chars[" + cpUtf8Chars.length + "]");
encodedBand = encodeBandInt("cpUtf8BigSuffix", cpUtf8BigSuffix, Codec.DELTA5);
out.write(encodedBand);
PackingUtils.log("Wrote " + encodedBand.length + " bytes from cpUtf8BigSuffix[" + cpUtf8BigSuffix.length + "]");
for (int i = 0; i < cpUtf8BigChars.length; i++) {
encodedBand = encodeBandInt("cpUtf8BigChars " + i, cpUtf8BigChars[i], Codec.DELTA5);
out.write(encodedBand);
PackingUtils.log("Wrote " + encodedBand.length + " bytes from cpUtf8BigChars" + i + "[" + cpUtf8BigChars[i].length + "]");
}
}