Example usage for java.lang Byte MIN_VALUE

List of usage examples for java.lang Byte MIN_VALUE

Introduction

In this page you can find the example usage for java.lang Byte MIN_VALUE.

Prototype

byte MIN_VALUE

To view the source code for java.lang Byte MIN_VALUE.

Click Source Link

Document

A constant holding the minimum value a byte can have, -27.

Usage

From source file:com.hovans.netty.tcpsample.network.ChannelDecoder.java

private void printPacket(byte[] packet) {
    StringBuffer sb = new StringBuffer();
    sb.append("received = ");
    for (byte bt : packet) {
        sb.append(byteToHex[bt - Byte.MIN_VALUE]);
        sb.append(" ");
    }//from ww w . j ava2s.  c  o m

    LogByCodeLab.w(sb.toString());
}

From source file:com.jeeframework.util.validate.GenericTypeValidator.java

/**
 *  Checks if the value can safely be converted to a byte primitive.
 *
 *@param  value   The value validation is being performed on.
 *@param  locale  The locale to use to parse the number (system default if
 *      null)//w  ww . j  av  a2s . co m
 *@return the converted Byte value.
 */
public static Byte formatByte(String value, Locale locale) {
    Byte result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error      and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) {
                result = new Byte(num.byteValue());
            }
        }
    }

    return result;
}

From source file:org.tranche.server.logs.LogUtil.java

/**
 * Return bytes for an IPv4 address string.
 * @param ip// w  w w  . j  a  v  a  2 s  . c om
 * @return
 */
public static byte[] getIPv4Bytes(String ip) {
    // Format: 0.0.0.0 .. 255.255.255.255
    String[] octets = ip.split("[.]");

    byte[] bytes = new byte[4];

    for (int i = 0; i < bytes.length; i++) {
        // Parse int since bytes are unsigned, 128 and above would
        // throw NumberFormatException
        int next = Integer.parseInt(octets[i]);
        // Adjust to correct byte range (not absolute)
        bytes[i] = (byte) (Byte.MIN_VALUE + next);
    }

    return bytes;
}

From source file:StringUtils.java

/**
 * DOCUMENT ME!//from  w w w .  j a  v  a2  s  .  co m
 * 
 * @param buffer DOCUMENT ME!
 * @param startPos DOCUMENT ME!
 * @param length DOCUMENT ME!
 * @return DOCUMENT ME! 
 */
public static final String toAsciiString(byte[] buffer, int startPos, int length) {

    StringBuffer result = new StringBuffer();
    int endPoint = startPos + length;

    for (int i = startPos; i < endPoint; i++) {
        result.append(byteToChars[(int) buffer[i] - Byte.MIN_VALUE]);
    }

    return result.toString();
}

From source file:org.wte4j.impl.WordTemplateTest.java

@Test
public void writeTest() throws IOException {
    PersistentTemplate template = new PersistentTemplate();
    byte[] content = new byte[1];
    Arrays.fill(content, Byte.MIN_VALUE);
    template.setContent(content);/*w w  w  . j  a  v  a2 s  .  c  o m*/

    WordTemplate<?> wordTemplate = new WordTemplate<Object>(template, null, null);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    wordTemplate.write(out);
    assertTrue("Content is not equals", Arrays.equals(content, out.toByteArray()));
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a byte primitive.
 * //ww w  .j a v a  2s  .  co m
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Byte value.
 */
public static Byte formatByte(String value, Locale locale) {
    Byte result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) {
                result = new Byte(num.byteValue());
            }
        }
    }

    return result;
}

From source file:org.crazydog.util.spring.NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 *
 * @param number      the number to convert
 * @param targetClass the target class to convert to
 * @return the converted number/*from   ww  w.j a v a 2s  .c  o  m*/
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see Byte
 * @see Short
 * @see Integer
 * @see Long
 * @see BigInteger
 * @see Float
 * @see Double
 * @see BigDecimal
 */
@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
        throws IllegalArgumentException {

    org.springframework.util.Assert.notNull(number, "Number must not be null");
    org.springframework.util.Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return (T) number;
    } else if (Byte.class == targetClass) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Byte(number.byteValue());
    } else if (Short.class == targetClass) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Short(number.shortValue());
    } else if (Integer.class == targetClass) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Integer(number.intValue());
    } else if (Long.class == targetClass) {
        BigInteger bigInt = null;
        if (number instanceof BigInteger) {
            bigInt = (BigInteger) number;
        } else if (number instanceof BigDecimal) {
            bigInt = ((BigDecimal) number).toBigInteger();
        }
        // Effectively analogous to JDK 8's BigInteger.longValueExact()
        if (bigInt != null && (bigInt.compareTo(LONG_MIN) < 0 || bigInt.compareTo(LONG_MAX) > 0)) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Long(number.longValue());
    } else if (BigInteger.class == targetClass) {
        if (number instanceof BigDecimal) {
            // do not lose precision - use BigDecimal's own conversion
            return (T) ((BigDecimal) number).toBigInteger();
        } else {
            // original value is not a Big* number - use standard long conversion
            return (T) BigInteger.valueOf(number.longValue());
        }
    } else if (Float.class == targetClass) {
        return (T) new Float(number.floatValue());
    } else if (Double.class == targetClass) {
        return (T) new Double(number.doubleValue());
    } else if (BigDecimal.class == targetClass) {
        // always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return (T) new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a byte primitive.
 * /*from www .j ava  2 s.  c o  m*/
 * @param value
 *            The value validation is being performed on.
 * @param locale
 *            The locale to use to parse the number (system default if null)
 * @return the converted Byte value.
 */
public static Byte formatByte(String value, Locale locale) {
    Byte result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) {
                result = Byte.valueOf(num.byteValue());
            }
        }
    }

    return result;
}

From source file:edu.udel.ece.infolab.btc.Utils.java

/**
 * Convert the byte array in the platform encoding
 * @param data the string buffer//from w  w  w.  j a  va2  s .c o m
 * @param length number of bytes to decode
 */
private static final void toAsciiString(final StringBuilder data, final int length) {
    for (int i = 0; i < length; i++) {
        data.append(byteToChars[(int) bbuffer.get(i) - Byte.MIN_VALUE]);
    }
}

From source file:org.joda.primitives.collection.impl.AbstractTestByteCollection.java

public Byte[] getFullNonNullElements() {
    return new Byte[] { new Byte((byte) 2), new Byte((byte) -2), new Byte((byte) 38), new Byte((byte) 0),
            new Byte((byte) 126), new Byte((byte) 202), new Byte(Byte.MIN_VALUE), new Byte(Byte.MAX_VALUE) };
}