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.jaspersoft.jasperserver.ws.axis2.scheduling.ReportJobBeanTraslator.java

private Object convertToParameter(Class paramType, Object value) {
    Object parameterValue = null;

    try {/*  w w  w.jav a 2 s  . c  o  m*/
        if (String.class.equals(paramType)) {
            parameterValue = value.toString();
        } else if (Boolean.class.equals(paramType)) {
            if (value instanceof String) {
                parameterValue = Boolean.valueOf((String) value);
            } else if (value instanceof Number) {
                //0 = false
                parameterValue = Boolean.valueOf(((Number) value).intValue() != 0);
            }
        } else if (Date.class.equals(paramType)) {
            if (value instanceof Calendar) {
                parameterValue = ((Calendar) value).getTime();
            } else if (value instanceof Number) {
                // the number is interpreted as milliseconds
                parameterValue = new Date(((Number) value).longValue());
            } else if (value instanceof String) {
                // the string is interpreted as milliseconds
                parameterValue = new Date(Long.parseLong((String) value));
            } else if (value instanceof XMLGregorianCalendar) {
                parameterValue = ((XMLGregorianCalendar) value).toGregorianCalendar().getTime();
            }
        } else if (java.sql.Date.class.equals(paramType)) {
            if (value instanceof Calendar) {
                parameterValue = ((Calendar) value).getTime();
            } else if (value instanceof Number) {
                // the number is interpreted as milliseconds
                parameterValue = new java.sql.Date(((Number) value).longValue());
            } else if (value instanceof String) {
                // the string is interpreted as milliseconds
                parameterValue = new java.sql.Date(Long.parseLong((String) value));
            } else if (value instanceof XMLGregorianCalendar) {
                parameterValue = new java.sql.Date(
                        ((XMLGregorianCalendar) value).toGregorianCalendar().getTimeInMillis());
            }
        } else if (Timestamp.class.equals(paramType)) {
            if (value instanceof Date) {
                parameterValue = new Timestamp(((Date) value).getTime());
            } else if (value instanceof Calendar) {
                parameterValue = new Timestamp(((Calendar) value).getTimeInMillis());
            } else if (value instanceof Number) {
                // the number is interpreted as milliseconds
                parameterValue = new Timestamp(((Number) value).longValue());
            } else if (value instanceof String) {
                // the string is interpreted as milliseconds
                parameterValue = new Timestamp(Long.parseLong((String) value));
            } else if (value instanceof XMLGregorianCalendar) {
                parameterValue = new Timestamp(
                        ((XMLGregorianCalendar) value).toGregorianCalendar().getTimeInMillis());
            }
        } else if (Byte.class.equals(paramType)) {
            if (value instanceof Number) {
                parameterValue = new Byte(((Number) value).byteValue());
            } else if (value instanceof String) {
                parameterValue = new Byte((String) value);
            }
        } else if (Short.class.equals(paramType)) {
            if (value instanceof Number) {
                parameterValue = new Short(((Number) value).shortValue());
            } else if (value instanceof String) {
                parameterValue = new Short((String) value);
            }
        } else if (Integer.class.equals(paramType)) {
            if (value instanceof Number) {
                parameterValue = new Integer(((Number) value).intValue());
            } else if (value instanceof String) {
                parameterValue = new Integer((String) value);
            }
        } else if (Long.class.equals(paramType)) {
            if (value instanceof Number) {
                parameterValue = new Long(((Number) value).longValue());
            } else if (value instanceof String) {
                parameterValue = new Long((String) value);
            }
        } else if (Float.class.equals(paramType)) {
            if (value instanceof Number) {
                parameterValue = new Float(((Number) value).floatValue());
            } else if (value instanceof String) {
                parameterValue = new Float((String) value);
            }
        } else if (Double.class.equals(paramType)) {
            if (value instanceof Number) {
                parameterValue = new Double(((Number) value).doubleValue());
            } else if (value instanceof String) {
                parameterValue = new Double((String) value);
            }
        } else if (BigInteger.class.equals(paramType)) {
            if (value instanceof BigDecimal) {
                parameterValue = ((BigDecimal) value).toBigInteger();
            } else if (value instanceof Number) {
                parameterValue = BigDecimal.valueOf(((Number) value).longValue());
            } else if (value instanceof String) {
                parameterValue = new BigDecimal((String) value);
            }
        } else if (BigDecimal.class.equals(paramType)) {
            if (value instanceof BigInteger) {
                parameterValue = new BigDecimal((BigInteger) value);
            } else if (value instanceof Number) {
                parameterValue = new BigDecimal(Double.toString(((Number) value).doubleValue()));
            } else if (value instanceof String) {
                parameterValue = new BigDecimal((String) value);
            }
        }
    } catch (NumberFormatException e) {
        // ignore, exception will be thrown bellow
    }

    if (parameterValue == null) {
        throw new JSException("report.scheduling.ws.value.conversion.not.supported",
                new Object[] { value, value.getClass().getName(), paramType.getName() });
    }

    return parameterValue;
}

From source file:com.netspective.axiom.sql.StoredProcedureParameter.java

/**
 * Extract the OUT parameter values from the callable statment and
 * assign them to the value of the parameter.
 *///  ww w.jav  a2s. com
public void extract(ConnectionContext cc, CallableStatement stmt) throws SQLException {
    if (getType().getValueIndex() == StoredProcedureParameter.Type.IN)
        return;

    int index = this.getIndex();
    QueryParameterType paramType = getSqlType();
    int jdbcType = paramType.getJdbcType();
    String identifier = paramType.getIdentifier();

    // result sets are special
    if (identifier.equals(QueryParameterType.RESULTSET_IDENTIFIER)) {
        ResultSet rs = (ResultSet) stmt.getObject(index);
        QueryResultSet qrs = new QueryResultSet(getParent().getProcedure(), cc, rs);
        value.getValue(cc).setValue(qrs);
        return;
    }

    switch (jdbcType) {
    case Types.VARCHAR:
        value.getValue(cc).setTextValue(stmt.getString(index));
        break;
    case Types.INTEGER:
        value.getValue(cc).setValue(new Integer(stmt.getInt(index)));
        break;
    case Types.DOUBLE:
        value.getValue(cc).setValue(new Double(stmt.getDouble(index)));
        break;
    case Types.CLOB:
        Clob clob = stmt.getClob(index);
        value.getValue(cc).setTextValue(clob.getSubString(1, (int) clob.length()));
        break;
    case java.sql.Types.ARRAY:
        Array array = stmt.getArray(index);
        value.getValue(cc).setValue(array);
        break;
    case java.sql.Types.BIGINT:
        long bigint = stmt.getLong(index);
        value.getValue(cc).setValue(new Long(bigint));
        break;
    case java.sql.Types.BINARY:
        value.getValue(cc).setTextValue(new String(stmt.getBytes(index)));
        break;
    case java.sql.Types.BIT:
        boolean bit = stmt.getBoolean(index);
        value.getValue(cc).setValue(new Boolean(bit));
    case java.sql.Types.BLOB:
        value.getValue(cc).setValue(stmt.getBlob(index));
        break;
    case java.sql.Types.CHAR:
        value.getValue(cc).setTextValue(stmt.getString(index));
        break;
    case java.sql.Types.DATE:
        value.getValue(cc).setValue(stmt.getDate(index));
        break;
    case java.sql.Types.DECIMAL:
        value.getValue(cc).setValue(stmt.getBigDecimal(index));
        break;
    case java.sql.Types.DISTINCT:
        value.getValue(cc).setValue(stmt.getObject(index));
        break;
    case java.sql.Types.FLOAT:
        value.getValue(cc).setValue(new Float(stmt.getFloat(index)));
        break;
    case java.sql.Types.JAVA_OBJECT:
        value.getValue(cc).setValue(stmt.getObject(index));
        break;
    case java.sql.Types.LONGVARBINARY:
        value.getValue(cc).setTextValue(new String(stmt.getBytes(index)));
        break;
    case java.sql.Types.LONGVARCHAR:
        value.getValue(cc).setTextValue(stmt.getString(index));
        break;
    //case java.sql.Types.NULL:
    //    value.getValue(cc).setValue(null);
    //    break;
    case java.sql.Types.NUMERIC:
        value.getValue(cc).setValue(stmt.getBigDecimal(index));
        break;
    case java.sql.Types.OTHER:
        value.getValue(cc).setValue(stmt.getObject(index));
        break;
    case java.sql.Types.REAL:
        value.getValue(cc).setValue(new Float(stmt.getFloat(index)));
        break;
    //case java.sql.Types.REF:
    //    Ref ref = stmt.getRef(index);
    //    break;
    case java.sql.Types.SMALLINT:
        short sh = stmt.getShort(index);
        value.getValue(cc).setValue(new Short(sh));
        break;
    case java.sql.Types.STRUCT:
        value.getValue(cc).setValue(stmt.getObject(index));
        break;
    case java.sql.Types.TIME:
        value.getValue(cc).setValue(stmt.getTime(index));
        break;
    case java.sql.Types.TIMESTAMP:
        value.getValue(cc).setValue(stmt.getTimestamp(index));
        break;
    case java.sql.Types.TINYINT:
        byte b = stmt.getByte(index);
        value.getValue(cc).setValue(new Byte(b));
        break;
    case java.sql.Types.VARBINARY:
        value.getValue(cc).setValue(stmt.getBytes(index));
        break;
    default:
        throw new RuntimeException(
                "Unknown JDBC Type set for stored procedure parameter '" + this.getName() + "'.");
    }
}

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

private List<Byte> getChunk(BufferedInputStream inStream, BufferedOutputStream proxyClientStream)
        throws IOException {
    ArrayList<Byte> retList = new ArrayList<Byte>(1024);
    int readInt;/*from  w w w  . jav  a 2 s  .  c o m*/
    String chunkHeader = null;
    String lengthStr = "";
    int length;
    chunkHeader = getNextLine(inStream);
    int posOfSemicolon = chunkHeader.indexOf(';');
    if (posOfSemicolon != -1) {
        lengthStr = chunkHeader.substring(0, posOfSemicolon);
    } else {
        lengthStr = chunkHeader.substring(0, chunkHeader.length() - 2);
    }
    int posOfMarks = lengthStr.indexOf('"');
    if (posOfMarks != -1) {
        lengthStr = lengthStr.substring(1, lengthStr.length() - 1);
    }
    lengthStr = lengthStr.trim();
    if ("0".equals(lengthStr)) {
        // marks the end; so simply skip to EOL and return empty list
        getNextLine(inStream);
        return retList;
    }
    byte[] appendBytes = chunkHeader.getBytes("ISO-8859-1");
    for (int i = 0; i < appendBytes.length; i++) {
        retList.add(new Byte(appendBytes[i]));
        proxyClientStream.write((int) appendBytes[i]);
    }
    length = Integer.parseInt(lengthStr, 16);
    byte b;
    for (int i = 0; i < length; i++) {
        //read bytes
        readInt = inStream.read();
        b = (byte) readInt;
        retList.add(new Byte(b));
        proxyClientStream.write(readInt);
    }
    // skip to EOL
    String chunkEndLine = getNextLine(inStream);
    appendBytes = chunkEndLine.getBytes("ISO-8859-1");
    for (int i = 0; i < appendBytes.length; i++) {
        retList.add(new Byte(appendBytes[i]));
        proxyClientStream.write((int) appendBytes[i]);
    }
    return retList;
}

From source file:com.tek42.perforce.parse.AbstractPerforceTemplate.java

/**
 * Used by calls that make use of p4.exe's python dictionary output format.
 * @param cmd//from   w w  w  .  jav a 2s .  c  om
 * @return
 * @throws PerforceException
 */

protected byte[] getRawPerforceResponseBytes(String cmd[]) throws PerforceException {
    List<Byte> bytes = new ArrayList<Byte>(1024);

    Executor p4 = depot.getExecFactory().newExecutor();
    String debugCmd = "";
    // get entire cmd to execute
    cmd = getExtraParams(cmd);

    // setup information for logging...
    for (String cm : cmd) {
        debugCmd += cm + " ";
    }

    // Perform execution and IO
    p4.exec(cmd);

    try {
        byte[] cbuf = new byte[1024];
        InputStream input = p4.getInputStream();
        p4.getWriter().close();
        int readCount = -1;
        while ((readCount = input.read(cbuf, 0, 1024)) != -1) {
            for (int i = 0; i < readCount; i++) {
                bytes.add(new Byte((byte) (cbuf[i] & 0xff)));
            }
        }
    } catch (IOException ioe) {
        //this is generally not anything to worry about.  The underlying
        //perforce process terminated and that causes java to be angry.

        // TODO Given the above comment, should we bother to log a warning?
        // See this blog for a discussion of IOException with message "Write end dead" from pipes:
        //      http://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        ioe.printStackTrace(pw);
        pw.flush();
        sw.flush();
        getLogger().warn("IOException reading from Perforce process (may just be EOF)");
        getLogger().warn(sw.toString());
    } finally {
        try {
            p4.getWriter().close();
        } catch (IOException e) {
            getLogger().warn("Write pipe failed to close.");
        }
        try {
            p4.getReader().close();
        } catch (IOException e) {
            getLogger().warn("Read pipe failed to close.");
        }
        p4.close();
    }
    byte[] byteArray = new byte[bytes.size()];
    for (int i = 0; i < bytes.size(); i++) {
        byteArray[i] = bytes.get(i).byteValue();
    }
    return byteArray;
}

From source file:io.stallion.reflection.PropertyUtils.java

/**
 * Try to transform the passed in value into the destinationClass, via a applying a boatload of
 * heuristics.//w  w  w .j  a va  2s  . c  om
 *
 * @param value
 * @param destinationClass
 * @return
 */
public static Object transform(Object value, Class destinationClass) {
    if (value == null) {
        return null;
    }
    if (value.getClass() == destinationClass)
        return value;
    if (destinationClass.isInstance(value)) {
        return value;
    }

    // If target type is Date and json was a long, convert the long to a date
    if (destinationClass == Date.class && (value.getClass() == long.class || value.getClass() == Long.class)) {
        return new Date((long) value);
    }
    // Convert integers to longs, if target type is long
    if ((destinationClass == Long.class || destinationClass == long.class)
            && (value.getClass() == int.class || value.getClass() == Integer.class)) {
        return new Long((int) value);
    }
    // Convert ints and longs to ZonedDateTime, if ZonedDateTime was a long
    if (destinationClass == ZonedDateTime.class
            && (value.getClass() == long.class || value.getClass() == Long.class
                    || value.getClass() == int.class || value.getClass() == Integer.class)) {
        if (value.getClass() == Integer.class || value.getClass() == int.class) {
            return ZonedDateTime.ofInstant(Instant.ofEpochMilli(((int) value) * 1000), GeneralUtils.UTC);
        } else {
            return ZonedDateTime.ofInstant(Instant.ofEpochMilli((long) value), GeneralUtils.UTC);
        }
    }

    if (destinationClass == ZonedDateTime.class && value instanceof Timestamp) {
        return ZonedDateTime.ofInstant(((Timestamp) value).toInstant(), UTC);
    }
    if (destinationClass == Long.class && value instanceof BigInteger) {
        return ((BigInteger) value).longValue();
    }

    if (destinationClass == ZonedDateTime.class
            && (value.getClass() == double.class || value.getClass() == Double.class)) {
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(Math.round((Double) value)), GeneralUtils.UTC);
    }

    // Convert Strings to Enums, if target type was an enum
    if (destinationClass.isEnum()) {
        return Enum.valueOf(destinationClass, value.toString());
    }

    if ((destinationClass == boolean.class || destinationClass == Boolean.class)) {
        if (value instanceof String) {
            return Boolean.valueOf((String) value);
        } else if (value instanceof Integer) {
            return (Integer) value > 0;
        } else if (value instanceof Long) {
            return (Long) value > 0;
        }
    }

    if ((destinationClass == byte.class || destinationClass == Byte.class)
            && value.getClass() == String.class) {
        return new Byte((String) value);
    }
    if ((destinationClass == short.class || destinationClass == Short.class)
            && value.getClass() == String.class) {
        return new Short((String) value);
    }
    if ((destinationClass == int.class || destinationClass == Integer.class)
            && value.getClass() == String.class) {
        return new Integer((String) value);
    }
    if ((destinationClass == long.class || destinationClass == Long.class)
            && value.getClass() == String.class) {
        return new Long((String) value);
    }
    if ((destinationClass == long.class || destinationClass == Long.class) && value instanceof Integer) {
        return new Long((Integer) value);
    }
    if ((destinationClass == float.class || destinationClass == Float.class)
            && value.getClass() == String.class) {
        return new Float((String) value);
    }
    if ((destinationClass == float.class || destinationClass == Float.class)
            && value.getClass() == Integer.class) {
        return ((Integer) value).floatValue();
    }
    if ((destinationClass == float.class || destinationClass == Float.class)
            && value.getClass() == Long.class) {
        return ((Long) value).floatValue();
    }
    if ((destinationClass == float.class || destinationClass == Float.class)
            && value.getClass() == Double.class) {
        return ((Double) value).floatValue();
    }

    if ((destinationClass == double.class || destinationClass == Double.class)
            && value.getClass() == Long.class) {
        return ((Long) value).floatValue();
    }

    if ((destinationClass == float.class || destinationClass == Float.class)
            && value.getClass() == String.class) {
        return new Float((String) value);
    }

    if ((destinationClass == double.class || destinationClass == Double.class)
            && value.getClass() == String.class) {
        return new Double((String) value);
    }

    // If the type mis-match is due to boxing, just return the value
    if (value.getClass() == boolean.class || value.getClass() == Boolean.class || value.getClass() == byte.class
            || value.getClass() == Byte.class || value.getClass() == short.class
            || value.getClass() == Short.class || value.getClass() == int.class
            || value.getClass() == Integer.class || value.getClass() == long.class
            || value.getClass() == Long.class || value.getClass() == float.class
            || value.getClass() == Float.class || value.getClass() == double.class
            || value.getClass() == Double.class)
        return value;

    throw new PropertyException("cannot convert values of type '" + value.getClass().getName() + "' into type '"
            + destinationClass + "'");
}

From source file:org.opoo.oqs.core.AbstractQuery.java

public Query setByte(int position, byte val) {
    setParameter(position, new Byte(val), Type.BYTE);
    return this;
}

From source file:ips1ap101.lib.base.util.StrUtils.java

public static Object getObjeto(String string, Class<?> clazz) {
    if (string == null || clazz == null) {
        return null;
    }/*from w  w w  .j  a v  a2  s.  c  o  m*/
    try {
        String value = StringUtils.trimToNull(string);
        if (value == null) {
            return null;
        } else if (Character.class.isAssignableFrom(clazz)) {
            return new Character(value.charAt(0));
        } else if (String.class.isAssignableFrom(clazz)) { // ALFANUMERICO
            return string;
        } else if (Boolean.class.isAssignableFrom(clazz)) {
            return BitUtils.valueOf(value);
        } else if (Byte.class.isAssignableFrom(clazz)) {
            return new Byte(new BigDecimal(value).byteValue());
        } else if (Short.class.isAssignableFrom(clazz)) {
            return new Short(new BigDecimal(value).shortValue());
        } else if (Integer.class.isAssignableFrom(clazz)) { // ENTERO
            return new Integer(new BigDecimal(value).intValue());
        } else if (Long.class.isAssignableFrom(clazz)) {
            return new Long(new BigDecimal(value).longValue());
        } else if (Float.class.isAssignableFrom(clazz)) {
            return new Float(new BigDecimal(value).floatValue());
        } else if (Double.class.isAssignableFrom(clazz)) {
            return new Double(new BigDecimal(value).doubleValue());
        } else if (BigInteger.class.isAssignableFrom(clazz)) { // ENTERO_GRANDE
            return new Long(new BigDecimal(value).longValue());
        } else if (BigDecimal.class.isAssignableFrom(clazz)) { // NUMERICO
            return new BigDecimal(value);
        } else if (java.util.Date.class.isAssignableFrom(clazz)) { // FECHA_HORA
            java.util.Date dateTime = TimeUtils.parse(value);
            if (Timestamp.class.isAssignableFrom(clazz)) {
                return new Timestamp(dateTime.getTime());
            } else if (Time.class.isAssignableFrom(clazz)) {
                return new Time(dateTime.getTime());
            } else if (Date.class.isAssignableFrom(clazz)) {
                return new Date(dateTime.getTime());
            } else {
                return dateTime;
            }
        }
        //      } catch (NumberFormatException e) {
        //          return null;
    } catch (RuntimeException e) {
        return null;
    }
    return null;
}

From source file:org.openehr.build.RMObjectBuilder.java

private Object defaultValue(Class type) {
    if (type == boolean.class) {
        return Boolean.FALSE;
    } else if (type == double.class) {
        return new Double(0);
    } else if (type == float.class) {
        return new Float(0);
    } else if (type == int.class) {
        return new Integer(0);
    } else if (type == short.class) {
        return new Short((short) 0);
    } else if (type == long.class) {
        return new Long(0);
    } else if (type == char.class) {
        return new Character((char) 0);
    } else if (type == byte.class) {
        return new Byte((byte) 0);
    }//www.  j  a v a 2  s . c  o  m
    return null;
}

From source file:org.latticesoft.util.common.NumeralUtil.java

public static byte[] convertHexStringToByteArray(String s) {
    byte[] retVal = null;
    if (s == null || s.length() == 0) {
        return null;
    }/*from  w  w  w.j av a 2 s  .c  om*/
    if (s.length() % 2 != 0) {
        return null;
    }
    s = s.toUpperCase();
    int index = 0;
    //int len = s.length();

    List l = new ArrayList();
    StringBuffer sb = new StringBuffer();
    while (index < s.length()) {
        sb.setLength(0);
        sb.append(s.charAt(index++));
        sb.append(s.charAt(index++));
        byte b = convertHexStringToByte(sb.toString());
        l.add(new Byte(b));
    }
    retVal = new byte[l.size()];
    for (int i = 0; i < l.size(); i++) {
        Byte bb = (Byte) l.get(i);
        retVal[i] = bb.byteValue();
    }
    return retVal;
}

From source file:com.jaspersoft.jasperserver.war.action.ReportJobEditAction.java

public Event setTriggerRecurrenceCalendar(RequestContext context) throws Exception {
    ReportJob job = getReportJob(context);
    ReportJobCalendarTrigger trigger = new ReportJobCalendarTrigger();
    copyCommonTriggerAttributes(trigger, job.getTrigger());
    trigger.setMinutes("0");
    trigger.setHours("0");
    trigger.setDaysType(ReportJobCalendarTrigger.DAYS_TYPE_ALL);

    TreeSet selectedMonths = new TreeSet();
    for (Iterator it = months.iterator(); it.hasNext();) {
        ByteEnum month = (ByteEnum) it.next();
        selectedMonths.add(new Byte(month.getCode()));
    }//from w  ww.  ja  va  2  s  .com
    trigger.setMonths(selectedMonths);

    job.setTrigger(trigger);
    return success();
}