Changeset 1538cd84


Ignore:
Timestamp:
Aug 11, 2012 11:40:41 AM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
be3cf44
Parents:
f92d8ae
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/data/DataHelper.java

    rf92d8ae r1538cd84  
    7272            "cost", "host", "port",
    7373            // SSU RouterAddress options
    74             "key",
     74            "key", "mtu",
    7575            "ihost0", "iport0", "ikey0", "itag0",
    7676            "ihost1", "iport1", "ikey1", "itag1",
     
    474474    }
    475475
     476    /**
     477     *  Hex with leading zeros.
     478     *  Use toHexString(byte[]) to not get leading zeros
     479     *  @param buf may be null (returns "")
     480     *  @return String of length 2*buf.length
     481     */
    476482    public static String toString(byte buf[]) {
    477483        if (buf == null) return "";
     
    482488    private static final byte[] EMPTY_BUFFER = "".getBytes();
    483489   
     490    /**
     491     *  Hex with leading zeros.
     492     *  Use toHexString(byte[]) to not get leading zeros
     493     *  @param buf may be null
     494     *  @param len number of bytes. If greater than buf.length, additional zeros will be prepended
     495     *  @return String of length 2*len
     496     */
    484497    public static String toString(byte buf[], int len) {
    485498        if (buf == null) buf = EMPTY_BUFFER;
     
    489502                out.append("00");
    490503        }
    491         for (int i = 0; i < buf.length && i < len; i++) {
    492             StringBuilder temp = new StringBuilder(Integer.toHexString(buf[i]));
    493             while (temp.length() < 2) {
    494                 temp.insert(0, '0');
    495             }
    496             temp = new StringBuilder(temp.substring(temp.length() - 2));
    497             out.append(temp.toString());
     504        int min = Math.min(buf.length, len);
     505        for (int i = 0; i < min; i++) {
     506            int bi = buf[i] & 0xff;
     507            if (bi < 16)
     508                out.append('0');
     509            out.append(Integer.toHexString(bi));
    498510        }
    499511        return out.toString();
     
    506518    }
    507519
     520    /**
     521     *  Hex without leading zeros.
     522     *  Use toString(byte[] to get leading zeros
     523     *  @param buf may be null (returns "00")
     524     */
    508525    public final static String toHexString(byte data[]) {
    509526        if ((data == null) || (data.length <= 0)) return "00";
Note: See TracChangeset for help on using the changeset viewer.