List of usage examples for java.lang Byte MAX_VALUE
byte MAX_VALUE
To view the source code for java.lang Byte MAX_VALUE.
Click Source Link
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)// ww w. j a va 2s .c o 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:Base64Utils.java
public static byte[] base64Decode(String data) { char[] ibuf = new char[4]; int ibufcount = 0; byte[] obuf = new byte[data.length() / 4 * 3 + 3]; int obufcount = 0; for (int i = 0; i < data.length(); i++) { char ch = data.charAt(i); if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) { ibuf[ibufcount++] = ch;//from ww w.j av a 2 s. co m if (ibufcount == ibuf.length) { ibufcount = 0; obufcount += base64Decode0(ibuf, obuf, obufcount); } } } if (obufcount == obuf.length) { return obuf; } byte[] ret = new byte[obufcount]; System.arraycopy(obuf, 0, ret, 0, obufcount); return ret; }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a byte primitive. * //from w w w. j a v a2 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 = 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 w w w . j av a 2s . c om*/ * @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. * // w ww .j a v a2s . c om * @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: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) }; }
From source file:org.sakaiproject.content.impl.serialize.impl.test.ByteStorageConversionCheck.java
@Test public void testMaxMinConversion() { byte[] bin = new byte[2]; char[] cin = new char[2]; byte[] bout = new byte[2]; bin[0] = Byte.MIN_VALUE;//from w w w. jav a 2 s. c om bin[1] = Byte.MAX_VALUE; ByteStorageConversion.toChar(bin, 0, cin, 0, bin.length); ByteStorageConversion.toByte(cin, 0, bout, 0, cin.length); log.info(" Min Byte " + bin[0] + ": Stored as int[" + (int) cin[0] + "] char[" + cin[0] + "]\n"); log.info(" Max Byte " + bin[1] + ": Stored as int[" + (int) cin[1] + "] char[" + cin[1] + "]\n"); for (int i = 0; i < bin.length; i++) { if (bin[i] != bout[i]) { log.warn("Internal Byte conversion failed at " + bin[i] + "=>" + (int) cin[i] + "=>" + bout[i]); } } }
From source file:com.qwazr.externalizor.SimplePrimitive.java
public SimplePrimitive() { intValue = RandomUtils.nextInt();//www .j av a 2 s.co m intArray = new int[] { RandomUtils.nextInt(), RandomUtils.nextInt(), RandomUtils.nextInt() }; shortValue = (short) RandomUtils.nextInt(0, Short.MAX_VALUE); shortArray = new short[] { (short) RandomUtils.nextInt(0, Short.MAX_VALUE), (short) RandomUtils.nextInt(0, Short.MAX_VALUE), (short) RandomUtils.nextInt(0, Short.MAX_VALUE) }; longValue = RandomUtils.nextLong(); longArray = new long[] { RandomUtils.nextLong(), RandomUtils.nextLong(), RandomUtils.nextLong() }; floatValue = RandomUtils.nextFloat(); floatArray = new float[] { RandomUtils.nextFloat(), RandomUtils.nextFloat(), RandomUtils.nextFloat() }; doubleValue = RandomUtils.nextDouble(); doubleArray = new double[] { RandomUtils.nextDouble(), RandomUtils.nextDouble(), RandomUtils.nextDouble() }; emptyDoubleArray = new double[0]; booleanValue = RandomUtils.nextInt(0, 2) == 0; booleanArray = new boolean[RandomUtils.nextInt(0, 5)]; for (int i = 0; i < booleanArray.length; i++) booleanArray[i] = RandomUtils.nextInt(0, 2) == 1; byteValue = (byte) RandomUtils.nextInt(0, Byte.MAX_VALUE); byteArray = new byte[] { (byte) RandomUtils.nextInt(0, Byte.MAX_VALUE), (byte) RandomUtils.nextInt(0, Byte.MAX_VALUE), (byte) RandomUtils.nextInt(0, Byte.MAX_VALUE) }; charValue = (char) RandomUtils.nextInt(0, Character.MAX_VALUE); charArray = new char[] { (char) RandomUtils.nextInt(0, Character.MAX_VALUE), (char) RandomUtils.nextInt(0, Character.MAX_VALUE), (char) RandomUtils.nextInt(0, Character.MAX_VALUE) }; }
From source file:management.limbr.test.util.PojoTester.java
private Object getTestValueFor(Class<?> type) { if (type.equals(boolean.class) || type.equals(Boolean.class)) { return random.nextBoolean(); } else if (type.equals(byte.class) || type.equals(Byte.class)) { return (byte) random.nextInt(Byte.MAX_VALUE); } else if (type.equals(short.class) || type.equals(Short.class)) { return (short) random.nextInt(Short.MAX_VALUE); } else if (type.equals(int.class) || type.equals(Integer.class)) { return random.nextInt(); } else if (type.equals(long.class) || type.equals(Long.class)) { return random.nextLong(); } else if (type.equals(char.class) || type.equals(Character.class)) { return (char) random.nextInt(Character.MAX_VALUE); } else if (type.equals(float.class) || type.equals(Float.class)) { return random.nextFloat(); } else if (type.equals(double.class) || type.equals(Double.class)) { return random.nextDouble(); } else if (type.equals(String.class)) { return Long.toString(random.nextLong()); } else {// w w w . j ava2 s . c o m return createRichObject(type); } }
From source file:lineage2.gameserver.network.clientpackets.RequestExSendPost.java
/** * Method readImpl./*from w w w.j a va2 s . c o m*/ */ @Override protected void readImpl() { _recieverName = readS(35); _messageType = readD(); _topic = readS(Byte.MAX_VALUE); _body = readS(Short.MAX_VALUE); _count = readD(); if ((((_count * 12) + 4) > _buf.remaining()) || (_count > Short.MAX_VALUE) || (_count < 1)) { _count = 0; return; } _items = new int[_count]; _itemQ = new long[_count]; for (int i = 0; i < _count; i++) { _items[i] = readD(); _itemQ[i] = readQ(); if ((_itemQ[i] < 1) || (ArrayUtils.indexOf(_items, _items[i]) < i)) { _count = 0; return; } } _price = readQ(); if (_price < 0) { _count = 0; _price = 0; } }