    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017
    018package org.apache.bcel.classfile;
    019
    020import java.io.ByteArrayInputStream;
    021import java.io.ByteArrayOutputStream;
    022import java.io.CharArrayReader;
    023import java.io.CharArrayWriter;
    024import java.io.FilterReader;
    025import java.io.FilterWriter;
    026import java.io.IOException;
    027import java.io.PrintStream;
    028import java.io.PrintWriter;
    029import java.io.Reader;
    030import java.io.Writer;
    031import java.util.ArrayList;
    032import java.util.List;
    033import java.util.Locale;
    034import java.util.zip.GZIPInputStream;
    035import java.util.zip.GZIPOutputStream;
    036
    037import org.apache.bcel.Const;
    038import org.apache.bcel.util.ByteSequence;
    039
    040/**
    041 * Utility functions that do not really belong to any class in particular.
    042 *
    043 */
    044// @since 6.0 methods are no longer final
    045public abstract class Utility {
    046
    047    private static int unwrap( final ThreadLocal<Integer> tl ) {
    048        return tl.get().intValue();
    049    }
    050
    051    private static void wrap( final ThreadLocal<Integer> tl, final int value ) {
    052        tl.set(Integer.valueOf(value));
    053    }
    054
    055    /* How many chars have been consumed
    056     * during parsing in typeSignatureToString().
    057     * Read by methodSignatureToString().
    058     * Set by side effect, but only internally.
    059     */
    060    private static ThreadLocal<Integer> consumed_chars = new ThreadLocal<Integer>() {
    061        @Override
    062        protected Integer initialValue() {
    063            return Integer.valueOf(0);
    064        }
    065    };
    066
    067    /* The `WIDE' instruction is used in the
    068     * byte code to allow 16-bit wide indices
    069     * for local variables. This opcode
    070     * precedes an `ILOAD', e.g.. The opcode
    071     * immediately following takes an extra
    072     * byte which is combined with the
    073     * following byte to form a
    074     * 16-bit value.
    075     */
    076    private static boolean wide = false;
    077
    078
    079    /**
    080     * Convert bit field of flags into string such as `static final'.
    081     *
    082     * @param  access_flags Access flags
    083     * @return String representation of flags
    084     */
    085    public static String accessToString( final int access_flags ) {
    086        return accessToString(access_flags, false);
    087    }
    088
    089
    090    /**
    091     * Convert bit field of flags into string such as `static final'.
    092     *
    093     * Special case: Classes compiled with new compilers and with the
    094     * `ACC_SUPER' flag would be said to be "synchronized". This is
    095     * because SUN used the same value for the flags `ACC_SUPER' and
    096     * `ACC_SYNCHRONIZED'.
    097     *
    098     * @param  access_flags Access flags
    099     * @param  for_class access flags are for class qualifiers ?
    100     * @return String representation of flags
    101     */
    102    public static String accessToString( final int access_flags, final boolean for_class ) {
    103        final StringBuilder buf = new StringBuilder();
    104        int p = 0;
    105        for (int i = 0; p < Const.MAX_ACC_FLAG_I; i++) { // Loop through known flags
    106            p = pow2(i);
    107            if ((access_flags & p) != 0) {
    108                /* Special case: Classes compiled with new compilers and with the
    109                 * `ACC_SUPER' flag would be said to be "synchronized". This is
    110                 * because SUN used the same value for the flags `ACC_SUPER' and
    111                 * `ACC_SYNCHRONIZED'.
    112                 */
    113                if (for_class && ((p == Const.ACC_SUPER) || (p == Const.ACC_INTERFACE))) {
    114                    continue;
    115                }
    116                buf.append(Const.getAccessName(i)).append(" ");
    117            }
    118        }
    119        return buf.toString().trim();
    120    }
    121
    122
    123    /**
    124     * @param access_flags the class flags
    125     *
    126     * @return "class" or "interface", depending on the ACC_INTERFACE flag
    127     */
    128    public static String classOrInterface( final int access_flags ) {
    129        return ((access_flags & Const.ACC_INTERFACE) != 0) ? "interface" : "class";
    130    }
    131
    132
    133    /**
    134     * Disassemble a byte array of JVM byte codes starting from code line
    135     * `index' and return the disassembled string representation. Decode only
    136     * `num' opcodes (including their operands), use -1 if you want to
    137     * decompile everything.
    138     *
    139     * @param  code byte code array
    140     * @param  constant_pool Array of constants
    141     * @param  index offset in `code' array
    142     * <EM>(number of opcodes, not bytes!)</EM>
    143     * @param  length number of opcodes to decompile, -1 for all
    144     * @param  verbose be verbose, e.g. print constant pool index
    145     * @return String representation of byte codes
    146     */
    147    public static String codeToString( final byte[] code, final ConstantPool constant_pool, final int index,
    148            final int length, final boolean verbose ) {
    149        final StringBuilder buf = new StringBuilder(code.length * 20); // Should be sufficient // CHECKSTYLE IGNORE MagicNumber
    150        try (ByteSequence stream = new ByteSequence(code)) {
    151            for (int i = 0; i < index; i++) {
    152                codeToString(stream, constant_pool, verbose);
    153            }
    154            for (int i = 0; stream.available() > 0; i++) {
    155                if ((length < 0) || (i < length)) {
    156                    final String indices = fillup(stream.getIndex() + ":", 6, true, ' ');
    157                    buf.append(indices).append(codeToString(stream, constant_pool, verbose)).append('\n');
    158                }
    159            }
    160        } catch (final IOException e) {
    161            throw new ClassFormatException("Byte code error: " + buf.toString(), e);
    162        }
    163        return buf.toString();
    164    }
    165
    166
    167    public static String codeToString( final byte[] code, final ConstantPool constant_pool, final int index, final int length ) {
    168        return codeToString(code, constant_pool, index, length, true);
    169    }
    170
    171
    172    /**
    173     * Disassemble a stream of byte codes and return the
    174     * string representation.
    175     *
    176     * @param  bytes stream of bytes
    177     * @param  constant_pool Array of constants
    178     * @param  verbose be verbose, e.g. print constant pool index
    179     * @return String representation of byte code
    180     *
    181     * @throws IOException if a failure from reading from the bytes argument occurs
    182     */
    183    public static String codeToString( final ByteSequence bytes, final ConstantPool constant_pool,
    184            final boolean verbose ) throws IOException {
    185        final short opcode = (short) bytes.readUnsignedByte();
    186        int default_offset = 0;
    187        int low;
    188        int high;
    189        int npairs;
    190        int index;
    191        int vindex;
    192        int constant;
    193        int[] match;
    194        int[] jump_table;
    195        int no_pad_bytes = 0;
    196        int offset;
    197        final StringBuilder buf = new StringBuilder(Const.getOpcodeName(opcode));
    198        /* Special case: Skip (0-3) padding bytes, i.e., the
    199         * following bytes are 4-byte-aligned
    200         */
    201        if ((opcode == Const.TABLESWITCH) || (opcode == Const.LOOKUPSWITCH)) {
    202            final int remainder = bytes.getIndex() % 4;
    203            no_pad_bytes = (remainder == 0) ? 0 : 4 - remainder;
    204            for (int i = 0; i < no_pad_bytes; i++) {
    205                byte b;
    206                if ((b = bytes.readByte()) != 0) {
    207                    System.err.println("Warning: Padding byte != 0 in "
    208                            + Const.getOpcodeName(opcode) + ":" + b);
    209                }
    210            }
    211            // Both cases have a field default_offset in common
    212            default_offset = bytes.readInt();
    213        }
    214        switch (opcode) {
    215            /* Table switch has variable length arguments.
    216             */
    217            case Const.TABLESWITCH:
    218                low = bytes.readInt();
    219                high = bytes.readInt();
    220                offset = bytes.getIndex() - 12 - no_pad_bytes - 1;
    221                default_offset += offset;
    222                buf.append("\tdefault = ").append(default_offset).append(", low = ").append(low)
    223                        .append(", high = ").append(high).append("(");
    224                jump_table = new int[high - low + 1];
    225                for (int i = 0; i < jump_table.length; i++) {
    226                    jump_table[i] = offset + bytes.readInt();
    227                    buf.append(jump_table[i]);
    228                    if (i < jump_table.length - 1) {
    229                        buf.append(", ");
    230                    }
    231                }
    232                buf.append(")");
    233                break;
    234            /* Lookup switch has variable length arguments.
    235             */
    236            case Const.LOOKUPSWITCH: {
    237                npairs = bytes.readInt();
    238                offset = bytes.getIndex() - 8 - no_pad_bytes - 1;
    239                match = new int[npairs];
    240                jump_table = new int[npairs];
    241                default_offset += offset;
    242                buf.append("\tdefault = ").append(default_offset).append(", npairs = ").append(
    243                        npairs).append(" (");
    244                for (int i = 0; i < npairs; i++) {
    245                    match[i] = bytes.readInt();
    246                    jump_table[i] = offset + bytes.readInt();
    247                    buf.append("(").append(match[i]).append(", ").append(jump_table[i]).append(")");
    248                    if (i < npairs - 1) {
    249                        buf.append(", ");
    250                    }
    251                }
    252                buf.append(")");
    253            }
    254                break;
    255            /* Two address bytes + offset from start of byte stream form the
    256             * jump target
    257             */
    258            case Const.GOTO:
    259            case Const.IFEQ:
    260            case Const.IFGE:
    261            case Const.IFGT:
    262            case Const.IFLE:
    263            case Const.IFLT:
    264            case Const.JSR:
    265            case Const.IFNE:
    266            case Const.IFNONNULL:
    267            case Const.IFNULL:
    268            case Const.IF_ACMPEQ:
    269            case Const.IF_ACMPNE:
    270            case Const.IF_ICMPEQ:
    271            case Const.IF_ICMPGE:
    272            case Const.IF_ICMPGT:
    273            case Const.IF_ICMPLE:
    274            case Const.IF_ICMPLT:
    275            case Const.IF_ICMPNE:
    276                buf.append("\t\t#").append((bytes.getIndex() - 1) + bytes.readShort());
    277                break;
    278            /* 32-bit wide jumps
    279             */
    280            case Const.GOTO_W:
    281            case Const.JSR_W:
    282                buf.append("\t\t#").append((bytes.getIndex() - 1) + bytes.readInt());
    283                break;
    284            /* Index byte references local variable (register)
    285             */
    286            case Const.ALOAD:
    287            case Const.ASTORE:
    288            case Const.DLOAD:
    289            case Const.DSTORE:
    290            case Const.FLOAD:
    291            case Const.FSTORE:
    292            case Const.ILOAD:
    293            case Const.ISTORE:
    294            case Const.LLOAD:
    295            case Const.LSTORE:
    296            case Const.RET:
    297                if (wide) {
    298                    vindex = bytes.readUnsignedShort();
    299                    wide = false; // Clear flag
    300                } else {
    301                    vindex = bytes.readUnsignedByte();
    302                }
    303                buf.append("\t\t%").append(vindex);
    304                break;
    305            /*
    306             * Remember wide byte which is used to form a 16-bit address in the
    307             * following instruction. Relies on that the method is called again with
    308             * the following opcode.
    309             */
    310            case Const.WIDE:
    311                wide = true;
    312                buf.append("\t(wide)");
    313                break;
    314            /* Array of basic type.
    315             */
    316            case Const.NEWARRAY:
    317                buf.append("\t\t<").append(Const.getTypeName(bytes.readByte())).append(">");
    318                break;
    319            /* Access object/class fields.
    320             */
    321            case Const.GETFIELD:
    322            case Const.GETSTATIC:
    323            case Const.PUTFIELD:
    324            case Const.PUTSTATIC:
    325                index = bytes.readUnsignedShort();
    326                buf.append("\t\t").append(
    327                        constant_pool.constantToString(index, Const.CONSTANT_Fieldref)).append(
    328                        verbose ? " (" + index + ")" : "");
    329                break;
    330            /* Operands are references to classes in constant pool
    331             */
    332            case Const.NEW:
    333            case Const.CHECKCAST:
    334                buf.append("\t");
    335                //$FALL-THROUGH$
    336            case Const.INSTANCEOF:
    337                index = bytes.readUnsignedShort();
    338                buf.append("\t<").append(
    339                        constant_pool.constantToString(index, Const.CONSTANT_Class))
    340                        .append(">").append(verbose ? " (" + index + ")" : "");
    341                break;
    342            /* Operands are references to methods in constant pool
    343             */
    344            case Const.INVOKESPECIAL:
    345            case Const.INVOKESTATIC:
    346                index = bytes.readUnsignedShort();
    347                final Constant c = constant_pool.getConstant(index);
    348                // With Java8 operand may be either a CONSTANT_Methodref
    349                // or a CONSTANT_InterfaceMethodref.   (markro)
    350                buf.append("\t").append(
    351                        constant_pool.constantToString(index, c.getTag()))
    352                        .append(verbose ? " (" + index + ")" : "");
    353                break;
    354            case Const.INVOKEVIRTUAL:
    355                index = bytes.readUnsignedShort();
    356                buf.append("\t").append(
    357                        constant_pool.constantToString(index, Const.CONSTANT_Methodref))
    358                        .append(verbose ? " (" + index + ")" : "");
    359                break;
    360            case Const.INVOKEINTERFACE:
    361                index = bytes.readUnsignedShort();
    362                final int nargs = bytes.readUnsignedByte(); // historical, redundant
    363                buf.append("\t").append(
    364                        constant_pool
    365                                .constantToString(index, Const.CONSTANT_InterfaceMethodref))
    366                        .append(verbose ? " (" + index + ")\t" : "").append(nargs).append("\t")
    367                        .append(bytes.readUnsignedByte()); // Last byte is a reserved space
    368                break;
    369            case Const.INVOKEDYNAMIC:
    370                index = bytes.readUnsignedShort();
    371                buf.append("\t").append(
    372                        constant_pool
    373                                .constantToString(index, Const.CONSTANT_InvokeDynamic))
    374                        .append(verbose ? " (" + index + ")\t" : "")
    375                        .append(bytes.readUnsignedByte())  // Thrid byte is a reserved space
    376                        .append(bytes.readUnsignedByte()); // Last byte is a reserved space
    377                break;
    378            /* Operands are references to items in constant pool
    379             */
    380            case Const.LDC_W:
    381            case Const.LDC2_W:
    382                index = bytes.readUnsignedShort();
    383                buf.append("\t\t").append(
    384                        constant_pool.constantToString(index, constant_pool.getConstant(index)
    385                                .getTag())).append(verbose ? " (" + index + ")" : "");
    386                break;
    387            case Const.LDC:
    388                index = bytes.readUnsignedByte();
    389                buf.append("\t\t").append(
    390                        constant_pool.constantToString(index, constant_pool.getConstant(index)
    391                                .getTag())).append(verbose ? " (" + index + ")" : "");
    392                break;
    393            /* Array of references.
    394             */
    395            case Const.ANEWARRAY:
    396                index = bytes.readUnsignedShort();
    397                buf.append("\t\t<").append(
    398                        compactClassName(constant_pool.getConstantString(index,
    399                                Const.CONSTANT_Class), false)).append(">").append(
    400                        verbose ? " (" + index + ")" : "");
    401                break;
    402            /* Multidimensional array of references.
    403             */
    404            case Const.MULTIANEWARRAY: {
    405                index = bytes.readUnsignedShort();
    406                final int dimensions = bytes.readUnsignedByte();
    407                buf.append("\t<").append(
    408                        compactClassName(constant_pool.getConstantString(index,
    409                                Const.CONSTANT_Class), false)).append(">\t").append(dimensions)
    410                        .append(verbose ? " (" + index + ")" : "");
    411            }
    412                break;
    413            /* Increment local variable.
    414             */
    415            case Const.IINC:
    416                if (wide) {
    417                    vindex = bytes.readUnsignedShort();
    418                    constant = bytes.readShort();
    419                    wide = false;
    420                } else {
    421                    vindex = bytes.readUnsignedByte();
    422                    constant = bytes.readByte();
    423                }
    424                buf.append("\t\t%").append(vindex).append("\t").append(constant);
    425                break;
    426            default:
    427                if (Const.getNoOfOperands(opcode) > 0) {
    428                    for (int i = 0; i < Const.getOperandTypeCount(opcode); i++) {
    429                        buf.append("\t\t");
    430                        switch (Const.getOperandType(opcode, i)) {
    431                            case Const.T_BYTE:
    432                                buf.append(bytes.readByte());
    433                                break;
    434                            case Const.T_SHORT:
    435                                buf.append(bytes.readShort());
    436                                break;
    437                            case Const.T_INT:
    438                                buf.append(bytes.readInt());
    439                                break;
    440                            default: // Never reached
    441                                throw new IllegalStateException("Unreachable default case reached!");
    442                        }
    443                    }
    444                }
    445        }
    446        return buf.toString();
    447    }
    448
    449
    450    public static String codeToString( final ByteSequence bytes, final ConstantPool constant_pool )
    451            throws IOException {
    452        return codeToString(bytes, constant_pool, true);
    453    }
    454
    455
    456    /**
    457     * Shorten long class names, <em>java/lang/String</em> becomes
    458     * <em>String</em>.
    459     *
    460     * @param str The long class name
    461     * @return Compacted class name
    462     */
    463    public static String compactClassName( final String str ) {
    464        return compactClassName(str, true);
    465    }
    466
    467
    468    /**
    469     * Shorten long class names, <em>java/lang/String</em> becomes
    470     * <em>java.lang.String</em>,
    471     * e.g.. If <em>chopit</em> is <em>true</em> the prefix <em>java.lang</em>
    472     * is also removed.
    473     *
    474     * @param str The long class name
    475     * @param chopit flag that determines whether chopping is executed or not
    476     * @return Compacted class name
    477     */
    478    public static String compactClassName( final String str, final boolean chopit ) {
    479        return compactClassName(str, "java.lang.", chopit);
    480    }
    481
    482
    483    /**
    484     * Shorten long class name <em>str</em>, i.e., chop off the <em>prefix</em>,
    485     * if the
    486     * class name starts with this string and the flag <em>chopit</em> is true.
    487     * Slashes <em>/</em> are converted to dots <em>.</em>.
    488     *
    489     * @param str The long class name
    490     * @param prefix The prefix the get rid off
    491     * @param chopit flag that determines whether chopping is executed or not
    492     * @return Compacted class name
    493     */
    494    public static String compactClassName( String str, final String prefix, final boolean chopit ) {
    495        final int len = prefix.length();
    496        str = str.replace('/', '.'); // Is `/' on all systems, even DOS
    497        if (chopit) {
    498            // If string starts with `prefix' and contains no further dots
    499            if (str.startsWith(prefix) && (str.substring(len).indexOf('.') == -1)) {
    500                str = str.substring(len);
    501            }
    502        }
    503        return str;
    504    }
    505
    506
    507    /**
    508     * @return `flag' with bit `i' set to 1
    509     */
    510    public static int setBit( final int flag, final int i ) {
    511        return flag | pow2(i);
    512    }
    513
    514
    515    /**
    516     * @return `flag' with bit `i' set to 0
    517     */
    518    public static int clearBit( final int flag, final int i ) {
    519        final int bit = pow2(i);
    520        return (flag & bit) == 0 ? flag : flag ^ bit;
    521    }
    522
    523
    524    /**
    525     * @return true, if bit `i' in `flag' is set
    526     */
    527    public static boolean isSet( final int flag, final int i ) {
    528        return (flag & pow2(i)) != 0;
    529    }
    530
    531
    532    /**
    533     * Converts string containing the method return and argument types
    534     * to a byte code method signature.
    535     *
    536     * @param  ret Return type of method
    537     * @param  argv Types of method arguments
    538     * @return Byte code representation of method signature
    539     *
    540     * @throws ClassFormatException if the signature is for Void
    541     */
    542    public static String methodTypeToSignature( final String ret, final String[] argv )
    543            throws ClassFormatException {
    544        final StringBuilder buf = new StringBuilder("(");
    545        String str;
    546        if (argv != null) {
    547            for (final String element : argv) {
    548                str = getSignature(element);
    549                if (str.endsWith("V")) {
    550                    throw new ClassFormatException("Invalid type: " + element);
    551                }
    552                buf.append(str);
    553            }
    554        }
    555        str = getSignature(ret);
    556        buf.append(")").append(str);
    557        return buf.toString();
    558    }
    559
    560
    561    /**
    562     * Converts argument list portion of method signature to string with all class names compacted.
    563     *
    564     * @param  signature    Method signature
    565     * @return String Array of argument types
    566     * @throws ClassFormatException
    567     */
    568    public static String[] methodSignatureArgumentTypes( final String signature )
    569            throws ClassFormatException {
    570        return methodSignatureArgumentTypes(signature, true);
    571    }
    572
    573
    574    /**
    575     * Converts argument list portion of method signature to string.
    576     *
    577     * @param  signature    Method signature
    578     * @param  chopit flag that determines whether chopping is executed or not
    579     * @return String Array of argument types
    580     * @throws ClassFormatException
    581     */
    582    public static String[] methodSignatureArgumentTypes( final String signature, final boolean chopit )
    583            throws ClassFormatException {
    584        final List<String> vec = new ArrayList<>();
    585        int index;
    586        try {
    587            // Skip any type arguments to read argument declarations between `(' and `)'
    588            index = signature.indexOf('(') + 1;
    589            if (index <= 0) {
    590                throw new ClassFormatException("Invalid method signature: " + signature);
    591            }
    592            while (signature.charAt(index) != ')') {
    593                vec.add(typeSignatureToString(signature.substring(index), chopit));
    594                //corrected concurrent private static field acess
    595                index += unwrap(consumed_chars); // update position
    596            }
    597        } catch (final StringIndexOutOfBoundsException e) { // Should never occur
    598            throw new ClassFormatException("Invalid method signature: " + signature, e);
    599        }
    600        return vec.toArray(new String[vec.size()]);
    601    }
    602
    603
    604    /**
    605     * Converts return type portion of method signature to string with all class names compacted.
    606     *
    607     * @param  signature    Method signature
    608     * @return String representation of method return type
    609     * @throws ClassFormatException
    610     */
    611    public static String methodSignatureReturnType( final String signature ) throws ClassFormatException {
    612        return methodSignatureReturnType(signature, true);
    613    }
    614
    615
    616    /**
    617     * Converts return type portion of method signature to string.
    618     *
    619     * @param  signature    Method signature
    620     * @param  chopit flag that determines whether chopping is executed or not
    621     * @return String representation of method return type
    622     * @throws ClassFormatException
    623     */
    624    public static String methodSignatureReturnType( final String signature, final boolean chopit ) throws ClassFormatException {
    625        int index;
    626        String type;
    627        try {
    628            // Read return type after `)'
    629            index = signature.lastIndexOf(')') + 1;
    630            if (index <= 0) {
    631                throw new ClassFormatException("Invalid method signature: " + signature);
    632            }
    633            type = typeSignatureToString(signature.substring(index), chopit);
    634        } catch (final StringIndexOutOfBoundsException e) { // Should never occur
    635            throw new ClassFormatException("Invalid method signature: " + signature, e);
    636        }
    637        return type;
    638    }
    639
    640
    641    /**
    642     * Converts method signature to string with all class names compacted.
    643     *
    644     * @param  signature to convert
    645     * @param  name of method
    646     * @param  access flags of method
    647     * @return Human readable signature
    648     */
    649    public static String methodSignatureToString( final String signature, final String name, final String access ) {
    650        return methodSignatureToString(signature, name, access, true);
    651    }
    652
    653
    654    /**
    655     * Converts method signature to string.
    656     *
    657     * @param  signature to convert
    658     * @param  name of method
    659     * @param  access flags of method
    660     * @param  chopit flag that determines whether chopping is executed or not
    661     * @return Human readable signature
    662     */
    663    public static String methodSignatureToString( final String signature, final String name, final String access, final boolean chopit ) {
    664        return methodSignatureToString(signature, name, access, chopit, null);
    665    }
    666
    667
    668    /**
    669     * This method converts a method signature string into a Java type declaration like
    670     * `void main(String[])' and throws a `ClassFormatException' when the parsed
    671     * type is invalid.
    672     *
    673     * @param  signature    Method signature
    674     * @param  name         Method name
    675     * @param  access       Method access rights
    676     * @param  chopit flag that determines whether chopping is executed or not
    677     * @param  vars the LocalVariableTable for the method
    678     * @return Java type declaration
    679     * @throws ClassFormatException
    680     */
    681    public static String methodSignatureToString( final String signature, final String name,
    682            final String access, final boolean chopit, final LocalVariableTable vars ) throws ClassFormatException {
    683        final StringBuilder buf = new StringBuilder("(");
    684        String type;
    685        int index;
    686        int var_index = access.contains("static") ? 0 : 1;
    687        try {
    688            // Skip any type arguments to read argument declarations between `(' and `)'
    689            index = signature.indexOf('(') + 1;
    690            if (index <= 0) {
    691                throw new ClassFormatException("Invalid method signature: " + signature);
    692            }
    693            while (signature.charAt(index) != ')') {
    694                final String param_type = typeSignatureToString(signature.substring(index), chopit);
    695                buf.append(param_type);
    696                if (vars != null) {
    697                    final LocalVariable l = vars.getLocalVariable(var_index, 0);
    698                    if (l != null) {
    699                        buf.append(" ").append(l.getName());
    700                    }
    701                } else {
    702                    buf.append(" arg").append(var_index);
    703                }
    704                if ("double".equals(param_type) || "long".equals(param_type)) {
    705                    var_index += 2;
    706                } else {
    707                    var_index++;
    708                }
    709                buf.append(", ");
    710                //corrected concurrent private static field acess
    711                index += unwrap(consumed_chars); // update position
    712            }
    713            index++; // update position
    714            // Read return type after `)'
    715            type = typeSignatureToString(signature.substring(index), chopit);
    716        } catch (final StringIndexOutOfBoundsException e) { // Should never occur
    717            throw new ClassFormatException("Invalid method signature: " + signature, e);
    718        }
    719        // ignore any throws information in the signature
    720        if (buf.length() > 1) {
    721            buf.setLength(buf.length() - 2);
    722        }
    723        buf.append(")");
    724        return access + ((access.length() > 0) ? " " : "") + // May be an empty string
    725                type + " " + name + buf.toString();
    726    }
    727
    728
    729    private static int pow2( final int n ) {
    730        return 1 << n;
    731    }
    732
    733
    734    /**
    735     * Replace all occurrences of <em>old</em> in <em>str</em> with <em>new</em>.
    736     *
    737     * @param str String to permute
    738     * @param old String to be replaced
    739     * @param new_ Replacement string
    740     * @return new String object
    741     */
    742    public static String replace( String str, final String old, final String new_ ) {
    743        int index;
    744        int old_index;
    745        try {
    746            if (str.contains(old)) { // `old' found in str
    747                final StringBuilder buf = new StringBuilder();
    748                old_index = 0; // String start offset
    749                // While we have something to replace
    750                while ((index = str.indexOf(old, old_index)) != -1) {
    751                    buf.append(str.substring(old_index, index)); // append prefix
    752                    buf.append(new_); // append replacement
    753                    old_index = index + old.length(); // Skip `old'.length chars
    754                }
    755                buf.append(str.substring(old_index)); // append rest of string
    756                str = buf.toString();
    757            }
    758        } catch (final StringIndexOutOfBoundsException e) { // Should not occur
    759            System.err.println(e);
    760        }
    761        return str;
    762    }
    763
    764
    765    /**
    766     * WARNING:
    767     *
    768     * There is some nomenclature confusion through much of the BCEL code base with
    769     * respect to the terms Descriptor and Signature.  For the offical definitions see:
    770     *
    771     * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.3">
    772     * Descriptors in The Java Virtual Machine Specification</a>
    773     *
    774     * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.9.1">
    775     * Signatures in The Java Virtual Machine Specification</a>
    776     *
    777     * In brief, a descriptor is a string representing the type of a field or method.
    778     * Signatures are similar, but more complex.  Signatures are used to encode declarations
    779     * written in the Java programming language that use types outside the type system of the
    780     * Java Virtual Machine.  They are used to describe the type of any class, interface,
    781     * constructor, method or field whose declaration uses type variables or parameterized types.
    782     *
    783     * To parse a descriptor, call typeSignatureToString.
    784     * To parse a signature, call signatureToString.
    785     *
    786     * Note that if the signature string is a single, non-generic item, the call to
    787     * signatureToString reduces to a call to typeSignatureToString.
    788     * Also note, that if you only wish to parse the first item in a longer signature
    789     * string, you should call typeSignatureToString directly.
    790     */
    791
    792
    793    /**
    794     * Converts a signature to a string with all class names compacted.
    795     * Class, Method and Type signatures are supported.
    796     * Enum and Interface signatures are not supported.
    797     *
    798     * @param  signature signature to convert
    799     * @return String containg human readable signature
    800     */
    801    public static String signatureToString( final String signature ) {
    802        return signatureToString(signature, true);
    803    }
    804
    805
    806    /**
    807     * Converts a signature to a string.
    808     * Class, Method and Type signatures are supported.
    809     * Enum and Interface signatures are not supported.
    810     *
    811     * @param  signature signature to convert
    812     * @param  chopit flag that determines whether chopping is executed or not
    813     * @return String containg human readable signature
    814     */
    815    public static String signatureToString( final String signature, final boolean chopit ) {
    816        String type = "";
    817        String typeParams = "";
    818        int index = 0;
    819        if (signature.charAt(0) == '<') {
    820            // we have type paramters
    821            typeParams = typeParamTypesToString(signature, chopit);
    822            index += unwrap(consumed_chars); // update position
    823        }
    824        if (signature.charAt(index) == '(') {
    825            // We have a Method signature.
    826            // add types of arguments
    827            type = typeParams + typeSignaturesToString(signature.substring(index), chopit, ')');
    828            index += unwrap(consumed_chars); // update position
    829            // add return type
    830            type = type + typeSignatureToString(signature.substring(index), chopit);
    831            index += unwrap(consumed_chars); // update position
    832            // ignore any throws information in the signature
    833            return type;
    834        }
    835        // Could be Class or Type...
    836        type = typeSignatureToString(signature.substring(index), chopit);
    837        index += unwrap(consumed_chars); // update position
    838        if ((typeParams.length() == 0) && (index == signature.length())) {
    839            // We have a Type signature.
    840            return type;
    841        }
    842        // We have a Class signature.
    843        final StringBuilder typeClass = new StringBuilder(typeParams);
    844        typeClass.append(" extends ");
    845        typeClass.append(type);
    846        if (index < signature.length()) {
    847            typeClass.append(" implements ");
    848            typeClass.append(typeSignatureToString(signature.substring(index), chopit));
    849            index += unwrap(consumed_chars); // update position
    850        }
    851        while (index < signature.length()) {
    852            typeClass.append(", ");
    853            typeClass.append(typeSignatureToString(signature.substring(index), chopit));
    854            index += unwrap(consumed_chars); // update position
    855        }
    856        return typeClass.toString();
    857    }
    858
    859
    860    /**
    861     * Converts a type parameter list signature to a string.
    862     *
    863     * @param  signature signature to convert
    864     * @param  chopit flag that determines whether chopping is executed or not
    865     * @return String containg human readable signature
    866     */
    867    private static String typeParamTypesToString( final String signature, final boolean chopit ) {
    868        // The first character is guranteed to be '<'
    869        final StringBuilder typeParams = new StringBuilder("<");
    870        int index = 1;  // skip the '<'
    871        // get the first TypeParameter
    872        typeParams.append(typeParamTypeToString(signature.substring(index), chopit));
    873        index += unwrap(consumed_chars); // update position
    874        // are there more TypeParameters?
    875        while (signature.charAt(index) != '>') {
    876            typeParams.append(", ");
    877            typeParams.append(typeParamTypeToString(signature.substring(index), chopit));
    878            index += unwrap(consumed_chars); // update position
    879        }
    880        wrap(consumed_chars, index + 1); // account for the '>' char
    881        return typeParams.append(">").toString();
    882    }
    883
    884
    885    /**
    886     * Converts a type parameter signature to a string.
    887     *
    888     * @param  signature signature to convert
    889     * @param  chopit flag that determines whether chopping is executed or not
    890     * @return String containg human readable signature
    891     */
    892    private static String typeParamTypeToString( final String signature, final boolean chopit ) {
    893        int index = signature.indexOf(':');
    894        if (index <= 0) {
    895            throw new ClassFormatException("Invalid type parameter signature: " + signature);
    896        }
    897        // get the TypeParameter identifier
    898        final StringBuilder typeParam = new StringBuilder(signature.substring(0, index));
    899        index++;  // account for the ':'
    900        if (signature.charAt(index) != ':') {
    901            // we have a class bound
    902            typeParam.append(" extends ");
    903            typeParam.append(typeSignatureToString(signature.substring(index), chopit));
    904            index += unwrap(consumed_chars); // update position
    905        }
    906        // look for interface bounds
    907        while (signature.charAt(index) == ':') {
    908            index++;  // skip over the ':'
    909            typeParam.append(" & ");
    910            typeParam.append(typeSignatureToString(signature.substring(index), chopit));
    911            index += unwrap(consumed_chars); // update position
    912        }
    913        wrap(consumed_chars, index);
    914        return typeParam.toString();
    915    }
    916
    917
    918    /**
    919     * Converts a list of type signatures to a string.
    920     *
    921     * @param  signature signature to convert
    922     * @param  chopit flag that determines whether chopping is executed or not
    923     * @param  term character indicating the end of the list
    924     * @return String containg human readable signature
    925     */
    926    private static String typeSignaturesToString( final String signature, final boolean chopit, final char term ) {
    927        // The first character will be an 'open' that matches the 'close' contained in term.
    928        final StringBuilder typeList = new StringBuilder(signature.substring(0, 1));
    929        int index = 1;  // skip the 'open' character
    930        // get the first Type in the list
    931        if (signature.charAt(index) != term) {
    932            typeList.append(typeSignatureToString(signature.substring(index), chopit));
    933            index += unwrap(consumed_chars); // update position
    934        }
    935        // are there more types in the list?
    936        while (signature.charAt(index) != term) {
    937            typeList.append(", ");
    938            typeList.append(typeSignatureToString(signature.substring(index), chopit));
    939            index += unwrap(consumed_chars); // update position
    940        }
    941        wrap(consumed_chars, index + 1); // account for the term char
    942        return typeList.append(term).toString();
    943    }
    944
    945
    946    /**
    947     *
    948     * This method converts a type signature string into a Java type declaration such as
    949     * `String[]' and throws a `ClassFormatException' when the parsed type is invalid.
    950     *
    951     * @param  signature type signature
    952     * @param  chopit flag that determines whether chopping is executed or not
    953     * @return string containing human readable type signature
    954     * @throws ClassFormatException
    955     * @since 6.4.0
    956     */
    957    public static String typeSignatureToString( final String signature, final boolean chopit ) throws ClassFormatException {
    958        //corrected concurrent private static field acess
    959        wrap(consumed_chars, 1); // This is the default, read just one char like `B'
    960        try {
    961            switch (signature.charAt(0)) {
    962                case 'B':
    963                    return "byte";
    964                case 'C':
    965                    return "char";
    966                case 'D':
    967                    return "double";
    968                case 'F':
    969                    return "float";
    970                case 'I':
    971                    return "int";
    972                case 'J':
    973                    return "long";
    974                case 'T': { // TypeVariableSignature
    975                    final int index = signature.indexOf(';'); // Look for closing `;'
    976                    if (index < 0) {
    977                        throw new ClassFormatException("Invalid type variable signature: " + signature);
    978                    }
    979                    //corrected concurrent private static field acess
    980                    wrap(consumed_chars, index + 1); // "Tblabla;" `T' and `;' are removed
    981                    return compactClassName(signature.substring(1, index), chopit);
    982                }
    983                case 'L': { // Full class name
    984                    // should this be a while loop? can there be more than
    985                    // one generic clause?  (markro)
    986                    int fromIndex = signature.indexOf('<'); // generic type?
    987                    if (fromIndex < 0) {
    988                        fromIndex = 0;
    989                    } else {
    990                        fromIndex = signature.indexOf('>', fromIndex);
    991                        if (fromIndex < 0) {
    992                            throw new ClassFormatException("Invalid signature: " + signature);
    993                        }
    994                    }
    995                    final int index = signature.indexOf(';', fromIndex); // Look for closing `;'
    996                    if (index < 0) {
    997                        throw new ClassFormatException("Invalid signature: " + signature);
    998                    }
    999
    1000                    // check to see if there are any TypeArguments
    1001                    final int bracketIndex = signature.substring(0, index).indexOf('<');
    1002                    if (bracketIndex < 0) {
    1003                        // just a class identifier
    1004                        wrap(consumed_chars, index + 1); // "Lblabla;" `L' and `;' are removed
    1005                        return compactClassName(signature.substring(1, index), chopit);
    1006                    }
    1007                    // but make sure we are not looking past the end of the current item
    1008                    fromIndex = signature.indexOf(';');
    1009                    if (fromIndex < 0) {
    1010                        throw new ClassFormatException("Invalid signature: " + signature);
    1011                    }
    1012                    if (fromIndex < bracketIndex) {
    1013                        // just a class identifier
    1014                        wrap(consumed_chars, fromIndex + 1); // "Lblabla;" `L' and `;' are removed
    1015                        return compactClassName(signature.substring(1, fromIndex), chopit);
    1016                    }
    1017
    1018                    // we have TypeArguments; build up partial result
    1019                    // as we recurse for each TypeArgument
    1020                    final StringBuilder type = new StringBuilder(compactClassName(signature.substring(1, bracketIndex), chopit)).append("<");
    1021                    int consumed_chars = bracketIndex + 1; // Shadows global var
    1022
    1023                    // check for wildcards
    1024                    if (signature.charAt(consumed_chars) == '+') {
    1025                        type.append("? extends ");
    1026                        consumed_chars++;
    1027                    } else if (signature.charAt(consumed_chars) == '-') {
    1028                        type.append("? super ");
    1029                        consumed_chars++;
    1030                    }
    1031
    1032                    // get the first TypeArgument
    1033                    if (signature.charAt(consumed_chars) == '*') {
    1034                        type.append("?");
    1035                        consumed_chars++;
    1036                    } else {
    1037                        type.append(typeSignatureToString(signature.substring(consumed_chars), chopit));
    1038                        // update our consumed count by the number of characters the for type argument
    1039                        consumed_chars = unwrap(Utility.consumed_chars) + consumed_chars;
    1040                        wrap(Utility.consumed_chars, consumed_chars);
    1041                    }
    1042
    1043                    // are there more TypeArguments?
    1044                    while (signature.charAt(consumed_chars) != '>') {
    1045                        type.append(", ");
    1046                        // check for wildcards
    1047                        if (signature.charAt(consumed_chars) == '+') {
    1048                            type.append("? extends ");
    1049                            consumed_chars++;
    1050                        } else if (signature.charAt(consumed_chars) == '-') {
    1051                            type.append("? super ");
    1052                            consumed_chars++;
    1053                        }
    1054                        if (signature.charAt(consumed_chars) == '*') {
    1055                            type.append("?");
    1056                            consumed_chars++;
    1057                        } else {
    1058                            type.append(typeSignatureToString(signature.substring(consumed_chars), chopit));
    1059                            // update our consumed count by the number of characters the for type argument
    1060                            consumed_chars = unwrap(Utility.consumed_chars) + consumed_chars;
    1061                            wrap(Utility.consumed_chars, consumed_chars);
    1062                        }
    1063                    }
    1064
    1065                    // process the closing ">"
    1066                    consumed_chars++;
    1067                    type.append(">");
    1068
    1069                    if (signature.charAt(consumed_chars) == '.') {
    1070                        // we have a ClassTypeSignatureSuffix
    1071                        type.append(".");
    1072                        // convert SimpleClassTypeSignature to fake ClassTypeSignature
    1073                        // and then recurse to parse it
    1074                        type.append(typeSignatureToString("L" + signature.substring(consumed_chars+1), chopit));
    1075                        // update our consumed count by the number of characters the for type argument
    1076                        // note that this count includes the "L" we added, but that is ok
    1077                        // as it accounts for the "." we didn't consume
    1078                        consumed_chars = unwrap(Utility.consumed_chars) + consumed_chars;
    1079                        wrap(Utility.consumed_chars, consumed_chars);
    1080                        return type.toString();
    1081                    }
    1082                    if (signature.charAt(consumed_chars) != ';') {
    1083                        throw new ClassFormatException("Invalid signature: " + signature);
    1084                    }
    1085                    wrap(Utility.consumed_chars, consumed_chars + 1); // remove final ";"
    1086                    return type.toString();
    1087                }
    1088                case 'S':
    1089                    return "short";
    1090                case 'Z':
    1091                    return "boolean";
    1092                case '[': { // Array declaration
    1093                    int n;
    1094                    StringBuilder brackets;
    1095                    String type;
    1096                    int consumed_chars; // Shadows global var
    1097                    brackets = new StringBuilder(); // Accumulate []'s
    1098                    // Count opening brackets and look for optional size argument
    1099                    for (n = 0; signature.charAt(n) == '['; n++) {
    1100                        brackets.append("[]");
    1101                    }
    1102                    consumed_chars = n; // Remember value
    1103                    // The rest of the string denotes a `<field_type>'
    1104                    type = typeSignatureToString(signature.substring(n), chopit);
    1105                    //corrected concurrent private static field acess
    1106                    //Utility.consumed_chars += consumed_chars; is replaced by:
    1107                    final int _temp = unwrap(Utility.consumed_chars) + consumed_chars;
    1108                    wrap(Utility.consumed_chars, _temp);
    1109                    return type + brackets.toString();
    1110                }
    1111                case 'V':
    1112                    return "void";
    1113                default:
    1114                    throw new ClassFormatException("Invalid signature: `" + signature + "'");
    1115            }
    1116        } catch (final StringIndexOutOfBoundsException e) { // Should never occur
    1117            throw new ClassFormatException("Invalid signature: " + signature, e);
    1118        }
    1119    }
    1120
    1121
    1122    /** Parse Java type such as "char", or "java.lang.String[]" and return the
    1123     * signature in byte code format, e.g. "C" or "[Ljava/lang/String;" respectively.
    1124     *
    1125     * @param  type Java type
    1126     * @return byte code signature
    1127     */
    1128    public static String getSignature( String type ) {
    1129        final StringBuilder buf = new StringBuilder();
    1130        final char[] chars = type.toCharArray();
    1131        boolean char_found = false;
    1132        boolean delim = false;
    1133        int index = -1;
    1134        loop: for (int i = 0; i < chars.length; i++) {
    1135            switch (chars[i]) {
    1136                case ' ':
    1137                case '\t':
    1138                case '\n':
    1139                case '\r':
    1140                case '\f':
    1141                    if (char_found) {
    1142                        delim = true;
    1143                    }
    1144                    break;
    1145                case '[':
    1146                    if (!char_found) {
    1147                        throw new IllegalArgumentException("Illegal type: " + type);
    1148                    }
    1149                    index = i;
    1150                    break loop;
    1151                default:
    1152                    char_found = true;
    1153                    if (!delim) {
    1154                        buf.append(chars[i]);
    1155                    }
    1156            }
    1157        }
    1158        int brackets = 0;
    1159        if (index > 0) {
    1160            brackets = countBrackets(type.substring(index));
    1161        }
    1162        type = buf.toString();
    1163        buf.setLength(0);
    1164        for (int i = 0; i < brackets; i++) {
    1165            buf.append('[');
    1166        }
    1167        boolean found = false;
    1168        for (int i = Const.T_BOOLEAN; (i <= Const.T_VOID) && !found; i++) {
    1169            if (Const.getTypeName(i).equals(type)) {
    1170                found = true;
    1171                buf.append(Const.getShortTypeName(i));
    1172            }
    1173        }
    1174        if (!found) {
    1175            buf.append('L').append(type.replace('.', '/')).append(';');
    1176        }
    1177        return buf.toString();
    1178    }
    1179
    1180
    1181    private static int countBrackets( final String brackets ) {
    1182        final char[] chars = brackets.toCharArray();
    1183        int count = 0;
    1184        boolean open = false;
    1185        for (final char c : chars) {
    1186            switch (c) {
    1187                case '[':
    1188                    if (open) {
    1189                        throw new IllegalArgumentException("Illegally nested brackets:" + brackets);
    1190                    }
    1191                    open = true;
    1192                    break;
    1193                case ']':
    1194                    if (!open) {
    1195                        throw new IllegalArgumentException("Illegally nested brackets:" + brackets);
    1196                    }
    1197                    open = false;
    1198                    count++;
    1199                    break;
    1200                default:
    1201                    // Don't care
    1202                    break;
    1203            }
    1204        }
    1205        if (open) {
    1206            throw new IllegalArgumentException("Illegally nested brackets:" + brackets);
    1207        }
    1208        return count;
    1209    }
    1210
    1211
    1212    /**
    1213     * Return type of method signature as a byte value as defined in <em>Constants</em>
    1214     *
    1215     * @param  signature in format described above
    1216     * @return type of method signature
    1217     * @see    Const
    1218     *
    1219     * @throws ClassFormatException if signature is not a method signature
    1220     */
    1221    public static byte typeOfMethodSignature( final String signature ) throws ClassFormatException {
    1222        int index;
    1223        try {
    1224            if (signature.charAt(0) != '(') {
    1225                throw new ClassFormatException("Invalid method signature: " + signature);
    1226            }
    1227            index = signature.lastIndexOf(')') + 1;
    1228            return typeOfSignature(signature.substring(index));
    1229        } catch (final StringIndexOutOfBoundsException e) {
    1230            throw new ClassFormatException("Invalid method signature: " + signature, e);
    1231        }
    1232    }
    1233
    1234
    1235    /**
    1236     * Return type of signature as a byte value as defined in <em>Constants</em>
    1237     *
    1238     * @param  signature in format described above
    1239     * @return type of signature
    1240     * @see    Const
    1241     *
    1242     * @throws ClassFormatException if signature isn't a known type
    1243     */
    1244    public static byte typeOfSignature( final String signature ) throws ClassFormatException {
    1245        try {
    1246            switch (signature.charAt(0)) {
    1247                case 'B':
    1248                    return Const.T_BYTE;
    1249                case 'C':
    1250                    return Const.T_CHAR;
    1251                case 'D':
    1252                    return Const.T_DOUBLE;
    1253                case 'F':
    1254                    return Const.T_FLOAT;
    1255                case 'I':
    1256                    return Const.T_INT;
    1257                case 'J':
    1258                    return Const.T_LONG;
    1259                case 'L':
    1260                case 'T':
    1261                    return Const.T_REFERENCE;
    1262                case '[':
    1263                    return Const.T_ARRAY;
    1264                case 'V':
    1265                    return Const.T_VOID;
    1266                case 'Z':
    1267                    return Const.T_BOOLEAN;
    1268                case 'S':
    1269                    return Const.T_SHORT;
    1270                case '!':
    1271                case '+':
    1272                case '*':
    1273                    return typeOfSignature(signature.substring(1));
    1274                default:
    1275                    throw new ClassFormatException("Invalid method signature: " + signature);
    1276            }
    1277        } catch (final StringIndexOutOfBoundsException e) {
    1278            throw new ClassFormatException("Invalid method signature: " + signature, e);
    1279        }
    1280    }
    1281
    1282
    1283    /** Map opcode names to opcode numbers. E.g., return Constants.ALOAD for "aload"
    1284     */
    1285    public static short searchOpcode( String name ) {
    1286        name = name.toLowerCase(Locale.ENGLISH);
    1287        for (short i = 0; i < Const.OPCODE_NAMES_LENGTH; i++) {
    1288            if (Const.getOpcodeName(i).equals(name)) {
    1289                return i;
    1290            }
    1291        }
    1292        return -1;
    1293    }
    1294
    1295
    1296    /**
    1297     * Convert (signed) byte to (unsigned) short value, i.e., all negative
    1298     * values become positive.
    1299     */
    1300    private static short byteToShort( final byte b ) {
    1301        return (b < 0) ? (short) (256 + b) : (short) b;
    1302    }
    1303
    1304
    1305    /** Convert bytes into hexadecimal string
    1306     *
    1307     * @param bytes an array of bytes to convert to hexadecimal
    1308     *
    1309     * @return bytes as hexadecimal string, e.g. 00 fa 12 ...
    1310     */
    1311    public static String toHexString( final byte[] bytes ) {
    1312        final StringBuilder buf = new StringBuilder();
    1313        for (int i = 0; i < bytes.length; i++) {
    1314            final short b = byteToShort(bytes[i]);
    1315            final String hex = Integer.toHexString(b);
    1316            if (b < 0x10) {
    1317                buf.append('0');
    1318            }
    1319            buf.append(hex);
    1320            if (i < bytes.length - 1) {
    1321                buf.append(' ');
    1322            }
    1323        }
    1324        return buf.toString();
    1325    }
    1326
    1327
    1328    /**
    1329     * Return a string for an integer justified left or right and filled up with
    1330     * `fill' characters if necessary.
    1331     *
    1332     * @param i integer to format
    1333     * @param length length of desired string
    1334     * @param left_justify format left or right
    1335     * @param fill fill character
    1336     * @return formatted int
    1337     */
    1338    public static String format( final int i, final int length, final boolean left_justify, final char fill ) {
    1339        return fillup(Integer.toString(i), length, left_justify, fill);
    1340    }
    1341
    1342
    1343    /**
    1344     * Fillup char with up to length characters with char `fill' and justify it left or right.
    1345     *
    1346     * @param str string to format
    1347     * @param length length of desired string
    1348     * @param left_justify format left or right
    1349     * @param fill fill character
    1350     * @return formatted string
    1351     */
    1352    public static String fillup( final String str, final int length, final boolean left_justify, final char fill ) {
    1353        final int len = length - str.length();
    1354        final char[] buf = new char[(len < 0) ? 0 : len];
    1355        for (int j = 0; j < buf.length; j++) {
    1356            buf[j] = fill;
    1357        }
    1358        if (left_justify) {
    1359            return str + new String(buf);
    1360        }
    1361        return new String(buf) + str;
    1362    }
    1363
    1364
    1365    static boolean equals( final byte[] a, final byte[] b ) {
    1366        int size;
    1367        if ((size = a.length) != b.length) {
    1368            return false;
    1369        }
    1370        for (int i = 0; i < size; i++) {
    1371            if (a[i] != b[i]) {
    1372                return false;
    1373            }
    1374        }
    1375        return true;
    1376    }
    1377
    1378
    1379    public static void printArray( final PrintStream out, final Object[] obj ) {
    1380        out.println(printArray(obj, true));
    1381    }
    1382
    1383
    1384    public static void printArray( final PrintWriter out, final Object[] obj ) {
    1385        out.println(printArray(obj, true));
    1386    }
    1387
    1388
    1389    public static String printArray( final Object[] obj ) {
    1390        return printArray(obj, true);
    1391    }
    1392
    1393
    1394    public static String printArray( final Object[] obj, final boolean braces ) {
    1395        return printArray(obj, braces, false);
    1396    }
    1397
    1398
    1399    public static String printArray( final Object[] obj, final boolean braces, final boolean quote ) {
    1400        if (obj == null) {
    1401            return null;
    1402        }
    1403        final StringBuilder buf = new StringBuilder();
    1404        if (braces) {
    1405            buf.append('{');
    1406        }
    1407        for (int i = 0; i < obj.length; i++) {
    1408            if (obj[i] != null) {
    1409                buf.append(quote ? "\"" : "").append(obj[i]).append(quote ? "\"" : "");
    1410            } else {
    1411                buf.append("null");
    1412            }
    1413            if (i < obj.length - 1) {
    1414                buf.append(", ");
    1415            }
    1416        }
    1417        if (braces) {
    1418            buf.append('}');
    1419        }
    1420        return buf.toString();
    1421    }
    1422
    1423
    1424    /**
    1425     * @param ch the character to test if it's part of an identifier
    1426     *
    1427     * @return true, if character is one of (a, ... z, A, ... Z, 0, ... 9, _)
    1428     */
    1429    public static boolean isJavaIdentifierPart( final char ch ) {
    1430        return ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))
    1431                || ((ch >= '0') && (ch <= '9')) || (ch == '_');
    1432    }
    1433
    1434
    1435    /**
    1436     * Encode byte array it into Java identifier string, i.e., a string
    1437     * that only contains the following characters: (a, ... z, A, ... Z,
    1438     * 0, ... 9, _, $).  The encoding algorithm itself is not too
    1439     * clever: if the current byte's ASCII value already is a valid Java
    1440     * identifier part, leave it as it is. Otherwise it writes the
    1441     * escape character($) followed by:
    1442     *
    1443     * <ul>
    1444     *   <li> the ASCII value as a hexadecimal string, if the value is not in the range 200..247</li>
    1445     *   <li>a Java identifier char not used in a lowercase hexadecimal string, if the value is in the range 200..247</li>
    1446     * </ul>
    1447     *
    1448     * <p>This operation inflates the original byte array by roughly 40-50%</p>
    1449     *
    1450     * @param bytes the byte array to convert
    1451     * @param compress use gzip to minimize string
    1452     *
    1453     * @throws IOException if there's a gzip exception
    1454     */
    1455    public static String encode(byte[] bytes, final boolean compress) throws IOException {
    1456        if (compress) {
    1457            try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
    1458                    GZIPOutputStream gos = new GZIPOutputStream(baos)) {
    1459                gos.write(bytes, 0, bytes.length);
    1460                bytes = baos.toByteArray();
    1461            }
    1462        }
    1463        final CharArrayWriter caw = new CharArrayWriter();
    1464        try (JavaWriter jw = new JavaWriter(caw)) {
    1465            for (final byte b : bytes) {
    1466                final int in = b & 0x000000ff; // Normalize to unsigned
    1467                jw.write(in);
    1468            }
    1469        }
    1470        return caw.toString();
    1471    }
    1472
    1473
    1474    /**
    1475     * Decode a string back to a byte array.
    1476     *
    1477     * @param s the string to convert
    1478     * @param uncompress use gzip to uncompress the stream of bytes
    1479     *
    1480     * @throws IOException if there's a gzip exception
    1481     */
    1482    public static byte[] decode(final String s, final boolean uncompress) throws IOException {
    1483        byte[] bytes;
    1484        try (JavaReader jr = new JavaReader(new CharArrayReader(s.toCharArray()));
    1485                ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    1486            int ch;
    1487            while ((ch = jr.read()) >= 0) {
    1488                bos.write(ch);
    1489            }
    1490            bytes = bos.toByteArray();
    1491        }
    1492        if (uncompress) {
    1493            final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
    1494            final byte[] tmp = new byte[bytes.length * 3]; // Rough estimate
    1495            int count = 0;
    1496            int b;
    1497            while ((b = gis.read()) >= 0) {
    1498                tmp[count++] = (byte) b;
    1499            }
    1500            bytes = new byte[count];
    1501            System.arraycopy(tmp, 0, bytes, 0, count);
    1502        }
    1503        return bytes;
    1504    }
    1505
    1506    // A-Z, g-z, _, $
    1507    private static final int FREE_CHARS = 48;
    1508    private static int[] CHAR_MAP = new int[FREE_CHARS];
    1509    private static int[] MAP_CHAR = new int[256]; // Reverse map
    1510    private static final char ESCAPE_CHAR = '$';
    1511    static {
    1512        int j = 0;
    1513        for (int i = 'A'; i <= 'Z'; i++) {
    1514            CHAR_MAP[j] = i;
    1515            MAP_CHAR[i] = j;
    1516            j++;
    1517        }
    1518        for (int i = 'g'; i <= 'z'; i++) {
    1519            CHAR_MAP[j] = i;
    1520            MAP_CHAR[i] = j;
    1521            j++;
    1522        }
    1523        CHAR_MAP[j] = '$';
    1524        MAP_CHAR['$'] = j;
    1525        j++;
    1526        CHAR_MAP[j] = '_';
    1527        MAP_CHAR['_'] = j;
    1528    }
    1529
    1530    /**
    1531     * Decode characters into bytes.
    1532     * Used by <a href="Utility.html#decode(java.lang.String, boolean)">decode()</a>
    1533     */
    1534    private static class JavaReader extends FilterReader {
    1535
    1536        public JavaReader(final Reader in) {
    1537            super(in);
    1538        }
    1539
    1540
    1541        @Override
    1542        public int read() throws IOException {
    1543            final int b = in.read();
    1544            if (b != ESCAPE_CHAR) {
    1545                return b;
    1546            }
    1547            final int i = in.read();
    1548            if (i < 0) {
    1549                return -1;
    1550            }
    1551            if (((i >= '0') && (i <= '9')) || ((i >= 'a') && (i <= 'f'))) { // Normal escape
    1552                final int j = in.read();
    1553                if (j < 0) {
    1554                    return -1;
    1555                }
    1556                final char[] tmp = {
    1557                        (char) i, (char) j
    1558                };
    1559                final int s = Integer.parseInt(new String(tmp), 16);
    1560                return s;
    1561            }
    1562            return MAP_CHAR[i];
    1563        }
    1564
    1565
    1566        @Override
    1567        public int read( final char[] cbuf, final int off, final int len ) throws IOException {
    1568            for (int i = 0; i < len; i++) {
    1569                cbuf[off + i] = (char) read();
    1570            }
    1571            return len;
    1572        }
    1573    }
    1574
    1575    /**
    1576     * Encode bytes into valid java identifier characters.
    1577     * Used by <a href="Utility.html#encode(byte[], boolean)">encode()</a>
    1578     */
    1579    private static class JavaWriter extends FilterWriter {
    1580
    1581        public JavaWriter(final Writer out) {
    1582            super(out);
    1583        }
    1584
    1585
    1586        @Override
    1587        public void write( final int b ) throws IOException {
    1588            if (isJavaIdentifierPart((char) b) && (b != ESCAPE_CHAR)) {
    1589                out.write(b);
    1590            } else {
    1591                out.write(ESCAPE_CHAR); // Escape character
    1592                // Special escape
    1593                if (b >= 0 && b < FREE_CHARS) {
    1594                    out.write(CHAR_MAP[b]);
    1595                } else { // Normal escape
    1596                    final char[] tmp = Integer.toHexString(b).toCharArray();
    1597                    if (tmp.length == 1) {
    1598                        out.write('0');
    1599                        out.write(tmp[0]);
    1600                    } else {
    1601                        out.write(tmp[0]);
    1602                        out.write(tmp[1]);
    1603                    }
    1604                }
    1605            }
    1606        }
    1607
    1608
    1609        @Override
    1610        public void write( final char[] cbuf, final int off, final int len ) throws IOException {
    1611            for (int i = 0; i < len; i++) {
    1612                write(cbuf[off + i]);
    1613            }
    1614        }
    1615
    1616
    1617        @Override
    1618        public void write( final String str, final int off, final int len ) throws IOException {
    1619            write(str.toCharArray(), off, len);
    1620        }
    1621    }
    1622
    1623
    1624    /**
    1625     * Escape all occurences of newline chars '\n', quotes \", etc.
    1626     */
    1627    public static String convertString( final String label ) {
    1628        final char[] ch = label.toCharArray();
    1629        final StringBuilder buf = new StringBuilder();
    1630        for (final char element : ch) {
    1631            switch (element) {
    1632                case '\n':
    1633                    buf.append("\\n");
    1634                    break;
    1635                case '\r':
    1636                    buf.append("\\r");
    1637                    break;
    1638                case '\"':
    1639                    buf.append("\\\"");
    1640                    break;
    1641                case '\'':
    1642                    buf.append("\\'");
    1643                    break;
    1644                case '\\':
    1645                    buf.append("\\\\");
    1646                    break;
    1647                default:
    1648                    buf.append(element);
    1649                    break;
    1650            }
    1651        }
    1652        return buf.toString();
    1653    }
    1654
    1655}