Example usage for java.lang Byte Byte

List of usage examples for java.lang Byte Byte

Introduction

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

Prototype

@Deprecated(since = "9")
public Byte(String s) throws NumberFormatException 

Source Link

Document

Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter.

Usage

From source file:com.github.xbn.array.primitive.ObjectArrayFromPrimitive.java

/**
   <p>Get a {@code java.lang.Object} array from an array of {@code byte}-s.</p>
        //from www. ja  va 2s . c om
 * @return  If {@code prmtv_array} is<ul>
 <li>{@code null}: {@code null}</li>
 <li>non-{@code null} and has no elements: <code>org.apache.commons.lang3.{@link org.apache.commons.lang3.ArrayUtils ArrayUtils}.{@link org.apache.commons.lang3.ArrayUtils#EMPTY_OBJECT_ARRAY EMPTY_OBJECT_ARRAY}</code></li>
 <li>Otherwise: A new {@code Object} array containing all elements in {@code prmtv_array}.</li>
   </ul>
 * @see  #get(boolean[], NullContainer, String) get(b[])
 */
public static final Object[] get(byte[] prmtv_array, NullContainer nnull, String cntrName_forNullBad) {
    if (prmtv_array == null) {
        IndexableUtil.crashIfContainerIsNullAndThatIsBad(nnull, cntrName_forNullBad);
        return null;
    }

    if (prmtv_array.length == 0) {
        return ArrayUtils.EMPTY_OBJECT_ARRAY;
    }

    Byte[] ao = new Byte[prmtv_array.length];
    for (int i = 0; i < prmtv_array.length; i++) {
        ao[i] = new Byte(prmtv_array[i]);
    }

    return ao;
}

From source file:org.apache.cocoon.util.JDBCTypeConversions.java

/**
 * Get the Statement column so that the results are mapped correctly.
 * (this has been copied from AbstractDatabaseAction and modified slightly)
 *//*  w  ww  .  j a v  a  2 s  .co  m*/
public static Object getColumn(ResultSet set, Configuration column) throws Exception {

    Integer type = (Integer) JDBCTypeConversions.typeConstants.get(column.getAttribute("type"));
    String dbcol = column.getAttribute("name");
    Object value = null;

    switch (type.intValue()) {
    case Types.CLOB:
    case Types.CHAR:
        Clob dbClob = set.getClob(dbcol);
        int length = (int) dbClob.length();
        InputStream asciiStream = new BufferedInputStream(dbClob.getAsciiStream());
        byte[] buffer = new byte[length];
        asciiStream.read(buffer);
        String str = new String(buffer);
        asciiStream.close();
        value = str;
        break;
    case Types.BIGINT:
        value = set.getBigDecimal(dbcol);
        break;
    case Types.TINYINT:
        value = new Byte(set.getByte(dbcol));
        break;
    case Types.VARCHAR:
        value = set.getString(dbcol);
        break;
    case Types.DATE:
        value = set.getDate(dbcol);
        break;
    case Types.DOUBLE:
        value = new Double(set.getDouble(dbcol));
        break;
    case Types.FLOAT:
        value = new Float(set.getFloat(dbcol));
        break;
    case Types.INTEGER:
        value = new Integer(set.getInt(dbcol));
        break;
    case Types.NUMERIC:
        value = new Long(set.getLong(dbcol));
        break;
    case Types.SMALLINT:
        value = new Short(set.getShort(dbcol));
        break;
    case Types.TIME:
        value = set.getTime(dbcol);
        break;
    case Types.TIMESTAMP:
        value = set.getTimestamp(dbcol);
        break;
    case Types.ARRAY:
        value = set.getArray(dbcol); // new Integer(set.getInt(dbcol));
        break;
    case Types.BIT:
        value = BooleanUtils.toBooleanObject(set.getBoolean(dbcol));
        break;
    case Types.STRUCT:
        value = (Struct) set.getObject(dbcol);
        break;
    case Types.OTHER:
        value = set.getObject(dbcol);
        break;

    default:
        // The blob types have to be requested separately, via a Reader.
        value = "";
        break;
    }

    return value;
}

From source file:com.amazonaws.hal.client.ConversionUtil.java

private static Object convertFromString(Class<?> clazz, String value) {
    if (String.class.isAssignableFrom(clazz)) {
        return value;
    } else if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz)) {
        return new Integer(value);
    } else if (long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz)) {
        return new Long(value);
    } else if (short.class.isAssignableFrom(clazz) || Short.class.isAssignableFrom(clazz)) {
        return new Short(value);
    } else if (double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz)) {
        return new Double(value);
    } else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz)) {
        return new Float(value);
    } else if (boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz)) {
        return Boolean.valueOf(value);
    } else if (char.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz)) {
        return value.charAt(0);
    } else if (byte.class.isAssignableFrom(clazz) || Byte.class.isAssignableFrom(clazz)) {
        return new Byte(value);
    } else if (BigDecimal.class.isAssignableFrom(clazz)) {
        return new BigDecimal(value);
    } else if (BigInteger.class.isAssignableFrom(clazz)) {
        return new BigInteger(value);
    } else if (Date.class.isAssignableFrom(clazz)) {
        try {//from w w  w  .  j ava2  s .co m
            return new Date(Long.parseLong(value));
        } catch (NumberFormatException e) {
            try {
                return DatatypeConverter.parseDateTime(value).getTime();
            } catch (IllegalArgumentException e1) {
                throw new RuntimeException("Unexpected date format: " + value
                        + ".  We currently parse xsd:datetime and milliseconds.");
            }
        }
    } else if (clazz.isEnum()) {
        try {
            //noinspection unchecked
            return Enum.valueOf((Class<Enum>) clazz, value);
        } catch (IllegalArgumentException e) {
            log.error(String.format(
                    "'%s' is not a recognized enum value for %s.  Returning default of %s instead.", value,
                    clazz.getName(), clazz.getEnumConstants()[0]));

            return clazz.getEnumConstants()[0];
        }
    } else {
        throw new RuntimeException("Not sure how to convert " + value + " to a " + clazz.getSimpleName());
    }
}

From source file:de.odysseus.calyxo.base.util.ParseUtils.java

public void testNullPrimitive() throws Exception {
        assertEquals(Boolean.FALSE, ParseUtils.parse(boolean.class, null));
        assertEquals(new Character((char) 0), ParseUtils.parse(char.class, null));
        assertEquals(new Byte((byte) 0), ParseUtils.parse(byte.class, null));
        assertEquals(new Short((short) 0), ParseUtils.parse(short.class, null));
        assertEquals(new Integer(0), ParseUtils.parse(int.class, null));
        assertEquals(new Long(0), ParseUtils.parse(long.class, null));
        assertEquals(new Float(0), ParseUtils.parse(float.class, null));
        assertEquals(new Double(0), ParseUtils.parse(double.class, null));
    }//from w  w w  .  j av a2s. c o  m

From source file:com.jaspersoft.jasperserver.remote.utils.RepositoryHelper.java

/**
 *  This method get a string and tries to convert the string into the requested object.
 *  Valid classes are:/*from  ww w  .  j  a v a 2 s  .  c o  m*/
 *    java.util.Date (the string is supposed to represent a number of milliseconds)
 *    Number
 *    BigDecimal
 *    Byte
 *    Short
 *    Integer
 *    Long
 *    Float
 *    Double
 *    Boolean
 *    String
 *    java.util.Locale
 * 
 *  The default returned object is of type String
 * @param str the string representing the value
 * @param clazz The class of the result (not garanteed)
 * @return return an Object hopefully of the requested class (or a String)
 * 
 */
public static Object stringToValue(String str, Class clazz) {
    Object value = str;
    if (value == null) {
        return value;
    }

    if (java.util.Date.class.getName().equals(clazz.getName())) {
        value = new java.util.Date(Long.valueOf(value.toString()).longValue());
    } else if (java.sql.Date.class.getName().equals(clazz.getName())) {
        value = new java.sql.Date(Long.valueOf(value.toString()).longValue());
    } else if (java.sql.Timestamp.class.getName().equals(clazz.getName())) {
        value = new java.sql.Timestamp(Long.valueOf(value.toString()).longValue());
    } else if (Number.class.isAssignableFrom(clazz)) {
        value = new java.math.BigDecimal(value.toString());
        if (Byte.class.getName().equals(clazz.getName())) {
            value = new Byte(((Number) value).byteValue());
        } else if (Short.class.getName().equals(clazz.getName())) {
            value = new Short(((Number) value).shortValue());
        } else if (Integer.class.getName().equals(clazz.getName())) {
            value = new Integer(((Number) value).intValue());
        } else if (Long.class.getName().equals(clazz.getName())) {
            value = new Long(((Number) value).longValue());
        } else if (Float.class.getName().equals(clazz.getName())) {
            value = new Float(((Number) value).floatValue());
        } else if (Double.class.getName().equals(clazz.getName())) {
            value = new Double(((Number) value).doubleValue());
        }
    } else if (Boolean.class.getName().equals(clazz.getName())) {
        value = Boolean.valueOf(value.toString());
    } else if (Locale.class.getName().equals(clazz.getName())) {
        value = LocaleHelper.getInstance().getLocale(str);
    } else if (DateRange.class.equals(clazz)) {
        try {
            value = DateRangeFactory.getInstance(str, Date.class);
        } catch (InvalidDateRangeExpressionException e) {
            //if DateRange passed as string with milliseconds we need to parse it
            Date dateValue = (Date) stringToValue(str, Date.class);
            value = DateRangeFactory.getInstance(dateValue);
        }
    } else if (TimestampRange.class.equals(clazz)) {
        try {
            value = DateRangeFactory.getInstance(str, Timestamp.class);
        } catch (InvalidDateRangeExpressionException e) {
            //if TimestampRange passed as string with milliseconds we need to parse it
            Timestamp timestamp = (Timestamp) stringToValue(str, Timestamp.class);
            value = DateRangeFactory.getInstance(timestamp);
        }
    }
    return value;
}

From source file:com.twinsoft.convertigo.eclipse.learnproxy.http.HttpProxyWorker.java

private HttpRequest handleRequest(Socket proxySocket) throws IOException {
    HttpRequest request = new HttpRequest();
    BufferedInputStream proxyInStream = new BufferedInputStream(proxySocket.getInputStream());
    byte b = -1;//www  .ja v  a2 s .  co m
    ArrayList<Byte> list = new ArrayList<Byte>(200);
    ArrayList<Byte> listToSend = new ArrayList<Byte>(200);
    int readInt;
    String previousLine = null;
    int lastCRPos = 0;
    int lastSendCRPos = 0;
    boolean hasCompleted = false;
    int lineNo = 0;
    int length = 0;
    while (!isInterrupted && !hasCompleted && (readInt = proxyInStream.read()) != -1) {
        b = (byte) readInt;
        list.add(new Byte(b));
        listToSend.add(new Byte(b));
        if (b == 13) {
            // check for two line breaks without form feed
            if (list.size() > 1) {
                if (list.get(list.size() - 2).equals(new Byte((byte) 13))) {
                    hasCompleted = true;
                } else {
                    // try to analyze the previous line
                    byte[] bytes = new byte[list.size() - lastCRPos - 1];
                    for (int i = lastCRPos; i < list.size() - 1; i++) {
                        bytes[i - lastCRPos] = ((Byte) list.get(i)).byteValue();
                    }
                    // requests are always in ASCII
                    previousLine = new String(bytes, "ISO-8859-1");
                    //logger.debug("request: " + previousLine);
                    if (lineNo == 0) {
                        // we must have here s.th. like 
                        // GET http://server/xyz.html?param=value HTTP/1.0
                        String[] components = previousLine.split(" ");
                        String method = components[0];
                        String urlStr = components[1];
                        String httpVersion = components[2];

                        // now parse the URL
                        URL url = new URL(urlStr);
                        String host = url.getHost();
                        String path = url.getPath();
                        String query = url.getQuery();
                        String port = String.valueOf(url.getPort());
                        if ("-1".equals(port)) {
                            port = "80";
                        }
                        request.setPort(Integer.parseInt(port));
                        request.setHost(host);
                        request.setPath(path);
                        request.setQuery(query);
                        request.setMethod(method);
                        request.setVersion(httpVersion);

                        // now we can reconstruct this line...
                        if ((System.getProperty("http.proxyHost") == null)
                                || System.getProperty("http.proxyHost").trim().equals("")) {
                            listToSend = new ArrayList<Byte>();
                            StringBuffer buff = new StringBuffer(100);
                            buff.append(method);
                            buff.append(' ');
                            buff.append(path);
                            if (query != null) {
                                buff.append('?');
                                buff.append(query);
                            }
                            buff.append(' ');
                            buff.append(components[2].substring(0, components[2].length()));
                            String newLine = buff.toString();
                            byte[] newLineBytes = newLine.getBytes("ISO-8859-1");
                            for (int i = 0; i < newLineBytes.length; i++) {
                                listToSend.add(new Byte(newLineBytes[i]));
                            }
                            listToSend.add(new Byte((byte) 13));
                        }
                    }
                    if (previousLine.matches("^[Cc]ontent-[Ll]ength: .+")) {
                        String lengthStr = previousLine.substring(16, previousLine.length());
                        length = Integer.parseInt(lengthStr);
                        //logger.debug("length: " + length + ", " + lengthStr);
                    }
                    if (previousLine.matches("^[Pp]roxy.+")) {
                        if ((System.getProperty("http.proxyHost") == null)
                                || System.getProperty("http.proxyHost").trim().equals("")) {
                            //logger.debug("proxy!!! - " + previousLine);
                            // if not used behind another proxy erase proxy-related headers
                            for (int i = listToSend.size() - 1; i > lastSendCRPos - 2; i--) {
                                listToSend.remove(i);
                            }
                        }
                    }
                    // the CR should be ignored for printing any headerLine
                    lastCRPos = list.size() + 1;
                    lastSendCRPos = listToSend.size() + 1;
                    lineNo++;
                }
            }
        }
        if (b == 10) {
            // check for two line breaks with form feed
            if (list.get(list.size() - 2).equals(new Byte((byte) 13))
                    && list.get(list.size() - 3).equals(new Byte((byte) 10))
                    && list.get(list.size() - 4).equals(new Byte((byte) 13))) {
                //logger.debug("length: " + length);
                if (length == 0) {
                    hasCompleted = true;
                } else {
                    for (int i = 0; i < length; i++) {
                        readInt = proxyInStream.read();
                        b = (byte) readInt;
                        list.add(new Byte(b));
                        listToSend.add(new Byte(b));
                    }
                    list.add(new Byte((byte) '\n'));
                    listToSend.add(new Byte((byte) '\n'));
                    hasCompleted = true;
                }
            }
        }
    }
    // store original request
    byte[] byteArray = getByteArrayFromList(listToSend);
    request.setRequest(byteArray);
    //logger.debug("request: \nasText:\n" + new String(byteArray) + "as bytes:\n" + printByteArray(byteArray));
    return request;
}

From source file:org.joda.primitives.list.impl.AbstractTestByteList.java

public void testFirst_notEmpty() {
    if (isAddSupported() == false) {
        return;// www  . j av a2s  .c  o  m
    }
    resetEmpty();
    ByteList plist = (ByteList) collection;
    plist.add((byte) 0);
    plist.add((byte) 6);
    assertEquals(new Byte((byte) 0), plist.first());
}

From source file:com.edmunds.autotest.AutoTestGetterSetter.java

private static Map<Class<?>, Object> createValueMap() {
    Map<Class<?>, Object> valueMap = new HashMap<Class<?>, Object>();

    valueMap.put(byte.class, new Byte((byte) 40));
    valueMap.put(short.class, new Short((short) 41));
    valueMap.put(int.class, new Integer(42));
    valueMap.put(long.class, new Long(43));
    valueMap.put(float.class, new Float(44));
    valueMap.put(double.class, new Double(45));
    valueMap.put(boolean.class, Boolean.TRUE);
    valueMap.put(char.class, new Character((char) 46));

    valueMap.put(Byte.class, new Byte((byte) 40));
    valueMap.put(Short.class, new Short((short) 41));
    valueMap.put(Integer.class, new Integer(42));
    valueMap.put(Long.class, new Long(43));
    valueMap.put(Float.class, new Float(44));
    valueMap.put(Double.class, new Double(45));
    valueMap.put(Boolean.class, Boolean.TRUE);
    valueMap.put(Character.class, new Character((char) 46));

    return valueMap;
}

From source file:com.eryansky.common.utils.SysUtils.java

/**
  * /* w  w  w  . j a v a 2 s  . co  m*/
  * 
  * @param in_str
  *            
  * @return ?
  */
 public static byte[] zip_Str(String in_str) {
     byte[] input = new byte[0];
     try {
         input = in_str.getBytes("UTF-8");
     } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
     }
     ArrayList<Byte> al = new ArrayList<Byte>();

     byte[] output;
     Deflater compresser = new Deflater();
     compresser.setInput(input);
     compresser.finish();
     for (; !compresser.finished();) {
         output = new byte[100];
         compresser.deflate(output);
         for (int i = 0; i < output.length; i++) {
             al.add(new Byte(output[i]));
         }
     }
     output = new byte[al.size()];
     for (int i = 0; i < al.size(); i++) {
         output[i] = (al.get(i)).byteValue();
     }
     return output;
 }

From source file:HexFormat.java

public static void main(String[] args) {
    String result;/*from  w w w  .ja  v  a 2s. c  o  m*/
    HexFormat format = new HexFormat();

    // byte
    byte bNumber = 0x33;
    result = format.format(bNumber);
    if (result.equals("33")) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Byte: " + bNumber + " (0x" + result + ")");

    bNumber = (byte) 0x85;
    result = format.format(bNumber);
    if (result.equals("85")) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Byte: " + bNumber + " (0x" + result + ")");

    bNumber = (byte) 0x0f;
    result = format.format(bNumber);
    if (result.equals("0f")) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Byte: " + bNumber + " (0x" + result + ")");

    short sNumber = (short) 0xa2b6;
    result = format.format(sNumber);
    if (result.equals("a2b6")) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Byte: " + bNumber + " (0x" + result + ")");

    format.setUpperCase(true);

    int iNumber = (int) 0x4321fedc;
    result = format.format(iNumber);
    if (result.equals("4321FEDC")) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Byte: " + bNumber + " (0x" + result + ")");

    long lNumber = (long) 0x4321fedc4321fedcL;
    result = format.format(lNumber);
    if (result.equals("4321FEDC4321FEDC")) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Byte: " + bNumber + " (0x" + result + ")");

    String num = "0xfe";
    Number number = null;
    Byte bExpect = new Byte((byte) 0xfe);
    try {
        number = format.parse(num);
    } catch (ParseException e) {
        System.out.println(e);
        e.printStackTrace();
    }
    if ((number instanceof Byte) && (number.equals(bExpect))) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Byte: " + bExpect + " result: " + number);

    num = "0xf";
    number = null;
    bExpect = new Byte((byte) 0xf);
    try {
        number = format.parse(num);
    } catch (ParseException e) {
        System.out.println(e);
        e.printStackTrace();
    }
    if ((number instanceof Byte) && (number.equals(bExpect))) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Byte: " + bExpect + " result: " + number);

    num = "0xf0f0";
    number = null;
    Short sExpect = new Short((short) 0xf0f0);
    try {
        number = format.parse(num);
    } catch (ParseException e) {
        System.out.println(e);
        e.printStackTrace();
    }
    if ((number instanceof Short) && (number.equals(sExpect))) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Short: " + sExpect + " result: " + number);

    num = "0xdEAdbEEf";
    number = null;
    Integer iExpect = new Integer((int) 0xdEAdbEEf);
    try {
        number = format.parse(num);
    } catch (ParseException e) {
        System.out.println(e);
        e.printStackTrace();
    }
    if ((number instanceof Integer) && (number.equals(iExpect))) {
        System.out.print("Success => ");
    } else {
        System.out.print("FAILURE => ");
    }
    System.out.println("Integer: " + iExpect + " result: " + number);
}