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

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

Introduction

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

Prototype

@CheckReturnValue
public static int toInt(byte value) 

Source Link

Document

Returns the value of the given byte as an integer, when treated as unsigned.

Usage

From source file:org.opendaylight.openflowplugin.testcommon.DropTestUtils.java

private static final void appendByte(final StringBuilder sb, final byte b) {
    int v = UnsignedBytes.toInt(b);
    sb.append(HEX[v >>> 4]);// ww w . j a va 2 s  .  c  om
    sb.append(HEX[v & 15]);
}

From source file:org.anarres.dhcp.common.address.AddressUtils.java

/**
 * Performs an arbitrary-precision increment of a byte array.
 *
 * @param in The array to increment.//from   w  w w. j  a v a2 s.  co  m
 * @return The same input array.
 */
@Nonnull
public static byte[] increment(@Nonnull byte[] in) {
    for (int i = in.length - 1; i >= 0; i--) {
        if (UnsignedBytes.toInt(in[i]) < 255) {
            in[i]++;
            break;
        }
        in[i] = 0;
    }

    return in;
}

From source file:org.anarres.dhcp.common.address.AddressUtils.java

/**
 * Performs an arbitrary-precision decrement of a byte array.
 *
 * @param in The array to decrement.//from w w w  . ja va  2  s.c  om
 * @return The same input array.
 */
@Nonnull
public static byte[] decrement(@Nonnull byte[] in) {
    for (int i = in.length - 1; i >= 0; i--) {
        if (UnsignedBytes.toInt(in[i]) > 0) {
            in[i]--;
            break;
        }
        in[i] = UnsignedBytes.MAX_VALUE;
    }

    return in;
}

From source file:net.sourcecrumbs.refimpl.util.DwarfDump.java

private static void printDIE(DIE die, String indent, StringTable stringTable) {
    System.out.print(indent);/*from w ww .j a  v  a 2  s  .c o  m*/
    System.out.println(die.getTag() + "[" + Long.toHexString(die.getOffset()) + "]");
    for (AttributeValue v : die.getAttributeValues()) {
        StringBuilder builder = new StringBuilder();
        switch (v.getForm()) {
        case DW_FORM_string:
            builder.append("\"").append(v.getDataAsString()).append("\"");
            break;
        case DW_FORM_strp:
            builder.append("\"").append(v.getDataAsString(stringTable)).append("\"");
            break;
        default:
            builder.append("{ ");
            for (int i = 0; i < v.getData().length; ++i) {
                builder.append(Integer.toHexString(UnsignedBytes.toInt(v.getData()[i])));

                if (i < v.getData().length - 1) {
                    builder.append(",");
                }
            }
            builder.append(" }");
            break;
        }

        System.out.print(indent);
        System.out.printf("- %s[%s] = %s", v.getName(), v.getForm(), builder.toString());
        System.out.println();
    }

    for (DIE child : die.getChildren()) {
        printDIE(child, "||||" + indent, stringTable);
    }
}

From source file:com.google.devrel.gmscore.tools.apk.arsc.TypeSpecChunk.java

protected TypeSpecChunk(ByteBuffer buffer, @Nullable Chunk parent) {
    super(buffer, parent);
    id = UnsignedBytes.toInt(buffer.get());
    buffer.position(buffer.position() + 3); // Skip 3 bytes for packing
    int resourceCount = buffer.getInt();
    resources = new int[resourceCount];

    for (int i = 0; i < resourceCount; ++i) {
        resources[i] = buffer.getInt();//  w ww  .j av  a  2 s. c om
    }
}

From source file:org.anarres.dhcp.common.address.AddressUtils.java

private static void _add(@Nonnull byte[] in, @Nonnegative long value) {
    for (int i = in.length - 1; i >= 0; i--) {
        if (value == 0)
            break;
        value += UnsignedBytes.toInt(in[i]);
        in[i] = (byte) (value & 0xFF);
        value >>>= Byte.SIZE;
    }/*ww w  . ja  va  2  s . c om*/
    // Preconditions.checkArgument(value == 0, "Carry overflow after arithmetic.");
}

From source file:net.sourcecrumbs.refimpl.dwarf.entries.AttributeValue.java

public long getDataAsLong(ByteOrder targetOrder) {
    long output = 0;
    int length = (data.length < 8 ? data.length : 8);
    if (targetOrder == ByteOrder.LITTLE_ENDIAN) {
        for (int i = 0; i < length; ++i) {
            output |= (UnsignedBytes.toInt(data[i]) << i * 8);
        }//from   ww  w .j  a  v  a  2 s .  com
    } else {
        for (int i = length - 1; i >= 0; --i) {
            output |= (UnsignedBytes.toInt(data[i]) << i * 8);
        }
    }
    return output;
}

From source file:com.google.devrel.gmscore.tools.apk.arsc.BinaryResourceConfiguration.java

static BinaryResourceConfiguration create(ByteBuffer buffer) {
    int startPosition = buffer.position(); // The starting buffer position to calculate bytes read.
    int size = buffer.getInt();
    int mcc = buffer.getShort() & 0xFFFF;
    int mnc = buffer.getShort() & 0xFFFF;
    byte[] language = new byte[2];
    buffer.get(language);//www. j  av a2s.  co m
    byte[] region = new byte[2];
    buffer.get(region);
    int orientation = UnsignedBytes.toInt(buffer.get());
    int touchscreen = UnsignedBytes.toInt(buffer.get());
    int density = buffer.getShort() & 0xFFFF;
    int keyboard = UnsignedBytes.toInt(buffer.get());
    int navigation = UnsignedBytes.toInt(buffer.get());
    int inputFlags = UnsignedBytes.toInt(buffer.get());
    buffer.get(); // 1 byte of padding
    int screenWidth = buffer.getShort() & 0xFFFF;
    int screenHeight = buffer.getShort() & 0xFFFF;
    int sdkVersion = buffer.getShort() & 0xFFFF;
    int minorVersion = buffer.getShort() & 0xFFFF;

    // At this point, the configuration's size needs to be taken into account as not all
    // configurations have all values.
    int screenLayout = 0;
    int uiMode = 0;
    int smallestScreenWidthDp = 0;
    int screenWidthDp = 0;
    int screenHeightDp = 0;
    byte[] localeScript = new byte[4];
    byte[] localeVariant = new byte[8];
    int screenLayout2 = 0;

    if (size >= SCREEN_CONFIG_MIN_SIZE) {
        screenLayout = UnsignedBytes.toInt(buffer.get());
        uiMode = UnsignedBytes.toInt(buffer.get());
        smallestScreenWidthDp = buffer.getShort() & 0xFFFF;
    }

    if (size >= SCREEN_DP_MIN_SIZE) {
        screenWidthDp = buffer.getShort() & 0xFFFF;
        screenHeightDp = buffer.getShort() & 0xFFFF;
    }

    if (size >= LOCALE_MIN_SIZE) {
        buffer.get(localeScript);
        buffer.get(localeVariant);
    }

    if (size >= SCREEN_CONFIG_EXTENSION_MIN_SIZE) {
        screenLayout2 = UnsignedBytes.toInt(buffer.get());
        buffer.get(); // Reserved padding
        buffer.getShort(); // More reserved padding
    }

    // After parsing everything that's known, account for anything that's unknown.
    int bytesRead = buffer.position() - startPosition;
    byte[] unknown = new byte[size - bytesRead];
    buffer.get(unknown);

    return new BinaryResourceConfiguration(size, mcc, mnc, language, region, orientation, touchscreen, density,
            keyboard, navigation, inputFlags, screenWidth, screenHeight, sdkVersion, minorVersion, screenLayout,
            uiMode, smallestScreenWidthDp, screenWidthDp, screenHeightDp, localeScript, localeVariant,
            screenLayout2, unknown);
}

From source file:cpw.mods.fml.common.network.ModIdMapPacket.java

@Override
public FMLPacket consumePacket(byte[] data) {
    ByteArrayDataInput bdi = ByteStreams.newDataInput(data);
    int chunkIdx = UnsignedBytes.toInt(bdi.readByte());
    int chunkTotal = UnsignedBytes.toInt(bdi.readByte());
    int chunkLength = bdi.readInt();
    if (partials == null) {
        partials = new byte[chunkTotal][];
    }/*from  www.j  a  va2s  .  co m*/
    partials[chunkIdx] = new byte[chunkLength];
    bdi.readFully(partials[chunkIdx]);
    for (int i = 0; i < partials.length; i++) {
        if (partials[i] == null) {
            return null;
        }
    }
    return this;
}

From source file:com.google.javascript.jscomp.FileInstrumentationData.java

/**
 * Returns a byte-wise hex string representation of the BitField from
 * MSB (Most Significant Byte) to LSB (Least Significant Byte).
 * Eg. Single byte: a setting of "0001 1111", returns "1f"
 * Eg. Multiple bytes: a setting of "0000 0010 0001 1111", returns "1f02"
 *
 * @return string representation of bits set
 */// www  .  ja  v a 2s  . com
String getInstrumentedLinesAsHexString() {
    StringBuilder builder = new StringBuilder();

    // Build the hex string.
    for (byte byteEntry : instrumentedBits.toByteArray()) {
        // Java bytes are signed, but we want the value as if it were unsigned.
        int value = UnsignedBytes.toInt(byteEntry);
        String hexString = Integer.toHexString(value);

        // Pad string to be two characters (if it isn't already).
        hexString = Strings.padStart(hexString, 2, '0');

        builder.append(hexString);
    }

    return builder.toString();
}