Example usage for java.lang Byte MAX_VALUE

List of usage examples for java.lang Byte MAX_VALUE

Introduction

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

Prototype

byte MAX_VALUE

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

Click Source Link

Document

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

Usage

From source file:Main.java

public static boolean isValidBase64Encoding(String data) {
    for (int i = 0; i < data.length(); i++) {
        char ch = data.charAt(i);
        if (ch == BASE64PAD || ch < DECODETABLE.length && DECODETABLE[ch] != Byte.MAX_VALUE) {
            // do nothing
        } else if (ch == '\r' || ch == '\n') {
            // do nothing
        } else {/*from   w w  w.  ja v  a 2  s.c  o m*/
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static byte[] decode(String data) {
    char[] ibuf = new char[4];
    int ibufcnt = 0;
    byte[] obuf = new byte[data.length() / 4 * 3 + 3];
    int obufcnt = 0;
    for (int i = 0; i < data.length(); i++) {
        char ch = data.charAt(i);
        if (ch == BASE64PAD || ch < DECODETABLE.length && DECODETABLE[ch] != Byte.MAX_VALUE) {
            ibuf[ibufcnt++] = ch;/*  w  ww.  j  ava  2 s.  c  o m*/
            if (ibufcnt == ibuf.length) {
                ibufcnt = 0;
                obufcnt += _decode(ibuf, obuf, obufcnt);
            }
        }
    }
    if (obufcnt == obuf.length)
        return obuf;
    byte[] ret = new byte[obufcnt];
    System.arraycopy(obuf, 0, ret, 0, obufcnt);
    return ret;
}

From source file:Main.java

public static byte toByte(long l) {
    if (l < Byte.MIN_VALUE || l > Byte.MAX_VALUE)
        throw new ArithmeticException("Value (" + l + ") cannot fit into byte");
    return (byte) l;
}

From source file:rascal.RandomTestDataUtils.java

public static byte[] createRandomData(int size) {
    byte[] data = new byte[size];
    for (int i = 0; i < data.length; i++) {
        data[i] = (byte) (RandomUtils.nextInt(Byte.MAX_VALUE * 2) + Byte.MIN_VALUE);
    }/*from   w  w w  .j  a v a 2 s  . c  o m*/
    return data;
}

From source file:Main.java

/**
 * Encode a string so that it can be safely used as text in an element
 * for XML output./* ww w .j  av  a 2 s .com*/
 * @param text the element text body.
 * @return a string so that it can be safely used as text in an element
 *       for XML output.
 */
public static String escape(String text) {
    final StringBuffer sb = new StringBuffer();
    if (text != null) {
        char c;
        final int l = text.length();
        for (int i = 0; i < l; i++) {
            c = text.charAt(i);
            switch (c) {
            case '<':
                sb.append("&lt;");
                break;
            case '>': // only needed to avoid ]]>
                sb.append("&gt;");
                break;
            case '&':
                sb.append("&amp;");
                break;
            default:
                if (c > Byte.MAX_VALUE) {
                    sb.append("&#x");
                    sb.append(Integer.toHexString(c));
                    sb.append(';');
                } else {
                    sb.append(c);
                }
            }
        }
    }
    return sb.toString();
}

From source file:Main.java

public static byte[] createSoundDataInByteArray(int bufferSamples, final int sampleRate, final double frequency,
        double sweep) {
    final double rad = 2 * Math.PI * frequency / sampleRate;
    byte[] vai = new byte[bufferSamples];
    sweep = Math.PI * sweep / ((double) sampleRate * vai.length);
    for (int j = 0; j < vai.length; j++) {
        int unsigned = (int) (Math.sin(j * (rad + j * sweep)) * Byte.MAX_VALUE) + Byte.MAX_VALUE & 0xFF;
        vai[j] = (byte) unsigned;
    }//from   w  w  w .  ja  v  a2  s . c  o m
    return vai;
}

From source file:Main.java

/**
 * Encode a string so that it can be safely used as attribute value in
 * XML output./* w ww . ja va2  s . co  m*/
 * @param attribute the attribute value to be encoded.
 * @return a string representing the attribute value that can be safely
 *         used in XML output.
 */
public static String attributeEscape(String attribute) {
    final StringBuffer sb = new StringBuffer();
    if (attribute != null) {
        char c;
        final int l = attribute.length();
        for (int i = 0; i < l; i++) {
            c = attribute.charAt(i);
            switch (c) {
            case '<':
                sb.append("&lt;");
                break;
            case '>':
                sb.append("&gt;");
                break;
            case '\'':
                sb.append("&apos;");
                break;
            case '"':
                sb.append("&quot;");
                break;
            case '&':
                sb.append("&amp;");
                break;
            default:
                if (c > Byte.MAX_VALUE || Character.isISOControl(c)) {
                    sb.append("&#x");
                    sb.append(Integer.toHexString(c));
                    sb.append(';');
                } else {
                    sb.append(c);
                }
            }
        }
    }
    return sb.toString();
}

From source file:datamine.storage.idl.generator.RandomValueGenerator.java

public static Object getValueOf(PrimitiveType type) {

    switch (type) {
    case BOOL://from  ww  w . j av  a  2  s . c om
        return rand1.nextBoolean();
    case BYTE:
        return (byte) rand1.nextInt(Byte.MAX_VALUE);
    case INT16:
        return (short) rand1.nextInt(Short.MAX_VALUE);
    case INT32:
        return rand1.nextInt(Integer.MAX_VALUE);
    case INT64:
        return rand1.nextLong();
    case FLOAT:
        return rand1.nextFloat();
    case DOUBLE:
        return rand1.nextDouble();
    case STRING:
        return "abasf_" + System.currentTimeMillis();//rand2.random(256);
    case BINARY:
        byte[] ret = new byte[1000];
        rand1.nextBytes(ret);
        return ret;
    case UNKNOWN:
        return null;
    default:
        throw new IllegalArgumentException("Not support the type: " + type);
    }
}

From source file:com.datatorrent.lib.util.TestUtils.java

public static byte[] getByte(int val) {
    Preconditions.checkArgument(val <= Byte.MAX_VALUE);
    return new byte[] { (byte) val };
}

From source file: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  ww  .  j a  va 2  s  .com
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class targetClass)
        throws IllegalArgumentException {

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.byteValue();
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.shortValue();
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.intValue();
    } else if (targetClass.equals(Long.class)) {
        return number.longValue();
    } else if (targetClass.equals(Float.class)) {
        return number.floatValue();
    } else if (targetClass.equals(Double.class)) {
        return number.doubleValue();
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}