Example usage for com.google.common.primitives UnsignedBytes parseUnsignedByte

List of usage examples for com.google.common.primitives UnsignedBytes parseUnsignedByte

Introduction

In this page you can find the example usage for com.google.common.primitives UnsignedBytes parseUnsignedByte.

Prototype

@Beta
public static byte parseUnsignedByte(String string, int radix) 

Source Link

Document

Returns the unsigned byte value represented by a string with the given radix.

Usage

From source file:se.sics.caracaldb.utils.ByteArrayFormatter.java

public static byte[] fromHexString(String str) {
    if (str.equals("(null)")) {
        return null;
    }/*from  w w  w.ja v a2 s . c o m*/
    if (str.equals("")) {
        return new byte[0];
    }
    String nospacestr = str.replaceAll("\\s", ""); // take away spaces
    if (UnsignedInts.remainder(nospacestr.length(), 2) != 0) {
        throw new NumberFormatException("String should contain only pairs of [0-F] (should be even)!");
    }
    int expectedLength = nospacestr.length() / 2;
    String[] byteBlocks = nospacestr.split("(?<=\\G.{2})"); // split in parts of length 2
    if (expectedLength != byteBlocks.length) {
        System.out.println("Blocks: " + Arrays.toString(byteBlocks));
        throw new NumberFormatException("String should contain only pairs of [0-F]!");
    }
    byte[] bytes = new byte[byteBlocks.length];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = UnsignedBytes.parseUnsignedByte(byteBlocks[i], 16);
    }
    return bytes;
}

From source file:org.opendaylight.genius.mdsalutil.NWUtil.java

public static byte[] parseMacAddress(String macAddress) {
    byte cur;/*from ww  w . j a  v a  2 s. c  o m*/

    String[] addressPart = macAddress.split(NwConstants.MACADDR_SEP);
    int size = addressPart.length;

    byte[] part = new byte[size];
    for (int i = 0; i < size; i++) {
        cur = UnsignedBytes.parseUnsignedByte(addressPart[i], 16);
        part[i] = cur;
    }

    return part;
}

From source file:com.torodb.common.util.HexUtils.java

public static byte[] hex2Bytes(@Nonnull String value) {
    checkNotNull(value);/*  w w  w  .j a  v a 2s  .co  m*/
    checkArgument(value.length() % 2 == 0, "A string with a even lenght was expected");

    final int size = value.length();
    int r = 0;
    byte[] result = new byte[size / 2];
    for (int i = 0; i < size; i += 2) {
        assert r == i / 2;

        String substring = value.substring(i, i + 2);
        assert substring.length() == 2;
        result[r] = UnsignedBytes.parseUnsignedByte(substring, 16);

        r++;
    }
    return result;
}

From source file:com.torodb.common.util.OctetUtils.java

public static byte[] octet2Bytes(@Nonnull String value) {
    checkNotNull(value);/*from  ww  w.  jav  a2 s . c o m*/

    int r = 0;
    byte[] result = new byte[value.length() / 5];
    for (int i = 0; i < value.length(); i += 5) {
        if (value.charAt(i) == '\\') {
            String substring = value.substring(i + 2, i + 5);
            assert substring.length() == 3;
            result[r] = UnsignedBytes.parseUnsignedByte(substring, 8);
        } else {
            assert value.charAt(i) < 256;
            result[r] = (byte) value.charAt(i);
        }

        r++;
    }
    return result;
}

From source file:org.opendaylight.vpnservice.alivenessmonitor.internal.AlivenessMonitorUtil.java

public static byte[] parseIpAddress(String ipAddress) {
    byte cur;//from w  ww  .ja v a  2  s  . c  om

    String[] addressPart = ipAddress.split(".");
    int size = addressPart.length;

    byte[] part = new byte[size];
    for (int i = 0; i < size; i++) {
        cur = UnsignedBytes.parseUnsignedByte(addressPart[i], 16);
        part[i] = cur;
    }

    return part;
}

From source file:org.opendaylight.genius.alivenessmonitor.protocols.internal.AbstractAlivenessProtocolHandler.java

private byte[] parseMacAddress(String macAddress) {
    byte cur;//w ww . j  a v  a2s . c  o  m

    String[] addressPart = macAddress.split(":");
    int size = addressPart.length;

    byte[] part = new byte[size];
    for (int i = 0; i < size; i++) {
        cur = UnsignedBytes.parseUnsignedByte(addressPart[i], 16);
        part[i] = cur;
    }

    return part;
}

From source file:org.opendaylight.vpnservice.alivenessmonitor.internal.AlivenessMonitorUtil.java

public static byte[] parseMacAddress(String macAddress) {
    byte cur;/*  w  ww.  ja  v  a2 s . c  o  m*/

    String[] addressPart = macAddress.split(":");
    int size = addressPart.length;

    byte[] part = new byte[size];
    for (int i = 0; i < size; i++) {
        cur = UnsignedBytes.parseUnsignedByte(addressPart[i], 16);
        part[i] = cur;
    }

    return part;
}

From source file:se.sics.caracaldb.Key.java

public static Key fromHex(String hex) {
    if (hex.equals("")) {
        return Key.NULL_KEY;
    }//www.j  a  v a 2 s.c o  m
    String[] byteBlocks = hex.split("\\s");
    byte[] bytes = new byte[byteBlocks.length];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = UnsignedBytes.parseUnsignedByte(byteBlocks[i], 16);
    }
    return new Key(bytes);
}

From source file:com.wrmsr.wava.yen.parser.ModuleFactory.java

private void parseMemory(Element s) {
    hasMemory = true;/* ww  w  .  j  a v a  2 s  .co  m*/

    int max = -1;
    int initial = Integer.parseInt(s.get(1).string());
    if (s.size() == 2) {
        return;
    }
    int i = 2;
    if (s.get(i).isString()) {
        max = Integer.parseInt(s.get(i).string());
        i++;
    }
    YMemory origMemory = builder.getMemory();
    builder.setMemory(new YMemory(initial, max, origMemory.getSegments(), origMemory.getExportName()));

    while (i < s.size()) {
        Element curr = s.get(i);
        checkState(curr.get(0).string().equals(SEGMENT));

        byte[] input = curr.get(2).string().getBytes();
        int pos = 0;
        byte[] data = new byte[input.length]; // over-allocated, since escaping collapses, but whatever // lol not really cuz unicode
        int write = 0;
        while (true) {
            if (pos > input.length) {
                throw new IllegalStateException();
            }
            if (pos == input.length) {
                break;
            }
            if (input[pos + 0] == 0) {
                // break;
                throw new IllegalStateException();
            }
            if (input[pos + 0] == '\\') {
                if (input[pos + 1] == '"') {
                    data[write++] = '"';
                    pos += 2;
                    continue;
                } else if (input[pos + 1] == '\'') {
                    data[write++] = '\'';
                    pos += 2;
                    continue;
                } else if (input[pos + 1] == '\\') {
                    data[write++] = '\\';
                    pos += 2;
                    continue;
                } else if (input[pos + 1] == 'n') {
                    data[write++] = '\n';
                    pos += 2;
                    continue;
                } else if (input[pos + 1] == 't') {
                    data[write++] = '\t';
                    pos += 2;
                    continue;
                } else {
                    data[write++] = UnsignedBytes
                            .parseUnsignedByte(new String(new byte[] { input[pos + 1], input[pos + 2] }), 16);
                    pos += 3;
                    continue;
                }
            }
            data[write++] = input[pos + 0];
            pos++;
        }

        byte[] compacted = new byte[write];
        System.arraycopy(data, 0, compacted, 0, write);
        YMemory.Segment segment = new YMemory.Segment(Integer.parseInt(curr.get(1).string()), compacted, write);
        builder.setMemory(builder.getMemory().withSegment(segment));
        i++;
    }
}