List of usage examples for java.lang Byte Byte
@Deprecated(since = "9") public Byte(String s) throws NumberFormatException
From source file:com.alibaba.otter.node.etl.common.db.utils.SqlUtils.java
/** * Retrieve a JDBC column value from a ResultSet, using the specified value * type./* ww w. java 2s. c om*/ * <p> * Uses the specifically typed ResultSet accessor methods, falling back to * {@link #getResultSetValue(java.sql.ResultSet, int)} for unknown types. * <p> * Note that the returned value may not be assignable to the specified * required type, in case of an unknown type. Calling code needs to deal * with this case appropriately, e.g. throwing a corresponding exception. * * @param rs is the ResultSet holding the data * @param index is the column index * @param requiredType the required value type (may be <code>null</code>) * @return the value object * @throws SQLException if thrown by the JDBC API */ private static String getResultSetValue(ResultSet rs, int index, Class<?> requiredType) throws SQLException { if (requiredType == null) { return getResultSetValue(rs, index); } Object value = null; boolean wasNullCheck = false; // Explicitly extract typed value, as far as possible. if (String.class.equals(requiredType)) { value = rs.getString(index); } else if (boolean.class.equals(requiredType) || Boolean.class.equals(requiredType)) { value = Boolean.valueOf(rs.getBoolean(index)); wasNullCheck = true; } else if (byte.class.equals(requiredType) || Byte.class.equals(requiredType)) { value = new Byte(rs.getByte(index)); wasNullCheck = true; } else if (short.class.equals(requiredType) || Short.class.equals(requiredType)) { value = new Short(rs.getShort(index)); wasNullCheck = true; } else if (int.class.equals(requiredType) || Integer.class.equals(requiredType)) { value = new Long(rs.getLong(index)); wasNullCheck = true; } else if (long.class.equals(requiredType) || Long.class.equals(requiredType)) { value = rs.getBigDecimal(index); wasNullCheck = true; } else if (float.class.equals(requiredType) || Float.class.equals(requiredType)) { value = new Float(rs.getFloat(index)); wasNullCheck = true; } else if (double.class.equals(requiredType) || Double.class.equals(requiredType) || Number.class.equals(requiredType)) { value = new Double(rs.getDouble(index)); wasNullCheck = true; } else if (java.sql.Time.class.equals(requiredType)) { // try { // value = rs.getTime(index); // } catch (SQLException e) { value = rs.getString(index);// ?string0000Time // if (value == null && !rs.wasNull()) { // value = "00:00:00"; // // mysqlzeroDateTimeBehavior=convertToNull0null // } // } } else if (java.sql.Timestamp.class.equals(requiredType) || java.sql.Date.class.equals(requiredType)) { // try { // value = convertTimestamp(rs.getTimestamp(index)); // } catch (SQLException e) { // ?string0000-00-00 00:00:00Timestamp value = rs.getString(index); // if (value == null && !rs.wasNull()) { // value = "0000:00:00 00:00:00"; // // mysqlzeroDateTimeBehavior=convertToNull0null // } // } } else if (BigDecimal.class.equals(requiredType)) { value = rs.getBigDecimal(index); } else if (BigInteger.class.equals(requiredType)) { value = rs.getBigDecimal(index); } else if (Blob.class.equals(requiredType)) { value = rs.getBlob(index); } else if (Clob.class.equals(requiredType)) { value = rs.getClob(index); } else if (byte[].class.equals(requiredType)) { try { byte[] bytes = rs.getBytes(index); if (bytes == null) { value = null; } else { value = new String(bytes, "ISO-8859-1");// binaryiso-8859-1 } } catch (UnsupportedEncodingException e) { throw new SQLException(e); } } else { // Some unknown type desired -> rely on getObject. value = getResultSetValue(rs, index); } // Perform was-null check if demanded (for results that the // JDBC driver returns as primitives). if (wasNullCheck && (value != null) && rs.wasNull()) { value = null; } return (value == null) ? null : convertUtilsBean.convert(value); }
From source file:demo.config.PropertyConverter.java
/** * Convert the specified object into a Byte. * /*from w w w.j a v a 2 s . c o m*/ * @param value * the value to convert * @return the converted value * @throws ConversionException * thrown if the value cannot be converted to a byte */ public static Byte toByte(Object value) throws ConversionException { Number n = toNumber(value, Byte.class); if (n instanceof Byte) { return (Byte) n; } else { return new Byte(n.byteValue()); } }
From source file:org.kaaproject.kaa.server.transports.http.transport.HttpTestClient.java
@Override public void run() { logger.trace("Test: " + testId + " started..."); IOException error = null;/*from w w w. j a va 2 s .co m*/ try { //connection.setChunkedStreamingMode(2048); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", objects.getContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); objects.dumbObjects(out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); error = e; } List<Byte> bodyArray = new Vector<>(); try { DataInputStream r = new DataInputStream(connection.getInputStream()); while (true) { bodyArray.add(new Byte(r.readByte())); } } catch (EOFException eof) { } catch (IOException e) { e.printStackTrace(); error = e; } byte[] body = new byte[bodyArray.size()]; for (int i = 0; i < body.length; i++) { body[i] = bodyArray.get(i); } processComplete(error, connection.getHeaderFields(), body); }
From source file:org.ff4j.commonsconf.FF4JConfigurationTest.java
@Test public void testgetByteOK() { // Given//from ww w. j av a 2 s .c o m Assert.assertTrue(ff4jConf.containsKey("propByte")); // When Assert.assertEquals("123", ff4jConf.getString("propByte")); Assert.assertEquals(new Byte("123"), (Byte) ff4jConf.getByte("propByte")); Assert.assertEquals(new Byte("123").byteValue(), ff4jConf.getByte("propByte")); Assert.assertEquals(123, ff4jConf.getByte("propByte")); }
From source file:javadz.beanutils.locale.converters.ByteLocaleConverter.java
/** * Convert the specified locale-sensitive input object into an output object of the * specified type. This method will return values of type Byte. * * @param value The input object to be converted * @param pattern The pattern is used for the convertion * @return The converted value//from w w w . j ava2 s . c om * * @exception org.apache.commons.beanutils.ConversionException if conversion cannot be performed * successfully * @throws ParseException if an error occurs parsing a String to a Number */ protected Object parse(Object value, String pattern) throws ParseException { final Number parsed = (Number) super.parse(value, pattern); if (parsed.longValue() != parsed.byteValue()) { throw new ConversionException("Supplied number is not of type Byte: " + parsed.longValue()); } // now returns property Byte return new Byte(parsed.byteValue()); }
From source file:org.voltdb.TestParameterSet.java
public void testNullInObjectArray() throws IOException { // This test passes nulls and VoltType nulls in Object[] arrays where the arrays contain // all supported datatypes (with the exception that we currently don't support Timestamp or varbinary in Object arrays). // Each Object[] type passes in "null" and the VoltType Null equivalent. Note that Object[] containing Strings will // support null and VoltType nulls as array elements. But any other Sigil type class (big decimal, timestamp, varbinary) // DO NOT support nulls or VoltType nulls in Object[] arrays. Object o_array[] = null;// w w w . j av a2 s . c o m ParameterSet p1 = null; Object first_param[] = null; boolean failed = false; // SHOULD FAIL: Object array of nulls o_array = new Object[] { null, null, null }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // SHOULD SUCCEED: Empty Object array of null o_array = new Object[] {}; p1 = ParameterSet.fromArrayNoCopy(new Object[] {}); first_param = p1.toArray(); assertEquals(first_param.length, p1.toArray().length); // SHOULD SUCCEED: Object array of Strings - pass null o_array = new Object[] { "Null", null, "not null" }; p1 = ParameterSet.fromArrayNoCopy(o_array, 1); first_param = p1.toArray(); assertEquals(first_param.length, p1.toArray().length); // SHOULD SUCCEED: Object array of Strings - pass VoltType null o_array = new Object[] { "Null", VoltType.NULL_STRING_OR_VARBINARY, "not null" }; p1 = ParameterSet.fromArrayNoCopy(o_array, 1); first_param = p1.toArray(); assertEquals(first_param.length, p1.toArray().length); // SHOULD FAIL: Object array of BigDecimal - pass both null o_array = new Object[] { BigDecimal.ONE, null, BigDecimal.TEN }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // SHOULD FAIL: Object array of BigDecimal - pass both VoltType null o_array = new Object[] { BigDecimal.ONE, VoltType.NULL_DECIMAL, BigDecimal.TEN }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // SHOULD FAIL: Object array of Byte - pass null o_array = new Object[] { new Byte((byte) 3), null, new Byte((byte) 15) }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // SHOULD SUCCEED: Object array of Byte - pass VoltType null o_array = new Object[] { new Byte((byte) 3), VoltType.NULL_TINYINT, new Byte((byte) 15) }; p1 = ParameterSet.fromArrayNoCopy(o_array, 1); first_param = p1.toArray(); assertEquals(first_param.length, p1.toArray().length); // SHOULD FAIL: Object array of Short - pass null o_array = new Object[] { new Short((short) 3), null, new Short((short) 15) }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // SHOULD SUCCEED: Object array of Short - pass VoltType null o_array = new Object[] { new Short((short) 3), VoltType.NULL_SMALLINT, new Short((short) 15) }; p1 = ParameterSet.fromArrayNoCopy(o_array, 1); first_param = p1.toArray(); assertEquals(first_param.length, p1.toArray().length); // SHOULD FAIL: Object array of Integer - pass null o_array = new Object[] { new Integer(3), null, new Integer(15) }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // SHOULD SUCCEED: Integer array, pass VoltType null o_array = new Object[] { new Integer(3), VoltType.NULL_SMALLINT, new Integer(15) }; p1 = ParameterSet.fromArrayNoCopy(o_array, 1); first_param = p1.toArray(); assertEquals(first_param.length, p1.toArray().length); // SHOULD FAIL: Object array of Long - pass null o_array = new Object[] { new Long(3), null }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // SHOULD SUCCEED: Object array of Long - pass VoltType null o_array = new Object[] { VoltType.NULL_BIGINT, new Long(15) }; p1 = ParameterSet.fromArrayNoCopy(o_array, 1); first_param = p1.toArray(); assertEquals(first_param.length, p1.toArray().length); // SHOULD FAIL: Object array of Float - pass null o_array = new Object[] { null, new Double(3.1415) }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // SHOULD SUCCEED: Object array of Float - pass VoltType null o_array = new Object[] { new Double(3.1415), VoltType.NULL_FLOAT }; p1 = ParameterSet.fromArrayNoCopy(o_array, 1); first_param = p1.toArray(); assertEquals(first_param.length, p1.toArray().length); // SHOULD FAIL: Object array of Decimal - pass null o_array = new Object[] { new Double(3.1415), null, new Double(3.1415) }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // Object array of Timestamp - pass both null and VoltType null // Currently not supported o_array = new Object[] { new org.voltdb.types.TimestampType(123432), null, new org.voltdb.types.TimestampType(1233) }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // SHOULD FAIL: not supported o_array = new Object[] { new org.voltdb.types.TimestampType(123432), VoltType.NULL_TIMESTAMP, new org.voltdb.types.TimestampType(1233) }; failed = false; try { p1 = ParameterSet.fromArrayNoCopy(o_array, 1); } catch (Exception ex) { failed = true; } assert (failed); // Object array of varbinary (byte array) - pass both null and VoltType null // Currently not supported }
From source file:com.germinus.easyconf.taglib.PropertyTag.java
private Object readProperty(ComponentProperties conf) throws JspException { Object value;/*w ww.ja v a 2s . c o m*/ if (getType().equals("java.util.List")) { value = conf.getList(property, getPropertyFilter(), EMPTY_LIST); } else if (getType().equals("java.lang.Integer")) { value = conf.getInteger(property, getPropertyFilter(), new Integer(0)); } else if (getType().equals("java.lang.String[]")) { value = conf.getStringArray(property, getPropertyFilter(), new String[0]); } else if (getType().equals("java.lang.String")) { if (defaultValue != null) { value = conf.getString(property, getPropertyFilter(), defaultValue); } else { value = conf.getString(property, getPropertyFilter()); } } else if (getType().equals("java.lang.Double")) { value = new Double(conf.getDouble(property, getPropertyFilter())); } else if (getType().equals("java.lang.Float")) { value = new Float(conf.getFloat(property, getPropertyFilter())); } else if (getType().equals("java.lang.Byte")) { value = new Byte(conf.getByte(property, getPropertyFilter())); } else if (getType().equals("java.math.BigDecimal")) { value = conf.getBigDecimal(property, getPropertyFilter()); } else if (getType().equals("java.lang.BigInteger")) { value = conf.getBigInteger(property, getPropertyFilter()); } else if (getType().equals("java.lang.Boolean")) { value = new Boolean(conf.getBoolean(property, getPropertyFilter())); } else if (getType().equals("java.lang.Short")) { value = new Short(conf.getShort(property, getPropertyFilter())); } else if (getType().equals("java.lang.Long")) { value = new Long(conf.getLong(property, getPropertyFilter())); } else { JspException e = new JspException("Unsupported type: " + type); RequestUtils.saveException(pageContext, e); throw e; } return value; }
From source file:org.openTwoFactor.clientExt.net.sf.ezmorph.object.NumberMorpher.java
private Object morphToByte(String str) { Object result = null;/*ww w.j ava2s .c o m*/ if (isUseDefault()) { if (defaultValue == null) { return (Byte) null; } else { result = new Byte(new ByteMorpher(defaultValue.byteValue()).morph(str)); } } else { result = new Byte(new ByteMorpher().morph(str)); } return result; }
From source file:org.devgateway.eudevfin.reports.core.utils.JROlapDataSourceCustom.java
/** * Convert the value of the data type of the Field * @param jrField the Field whose type has to be converted * @return value of field in the requested type * *//*from w w w .ja v a 2 s.c om*/ public Object getFieldValue(JRField jrField) throws JRException { Class<?> valueClass = jrField.getValueClass(); Object value = fieldValues.get(jrField.getName()); try { /* * Everything in the result is a string, apart from Member */ if (valueClass.equals(mondrian.olap.Member.class)) { if (!(value instanceof mondrian.olap.Member)) { throw new JRException("Field '" + jrField.getName() + "' is of class '" + value.getClass() + "' and can not be converted to class " + valueClass.getName()); } return value; } /* * Convert the rest from String */ String fieldValue = (String) value; if (fieldValue == null) { return null; } if (Number.class.isAssignableFrom(valueClass)) { fieldValue = fieldValue.trim(); } if (fieldValue.length() == 0) { fieldValue = "0"; } if (valueClass.equals(String.class)) { return fieldValue; } else if (valueClass.equals(Boolean.class)) { return fieldValue.equalsIgnoreCase("true") ? Boolean.TRUE : Boolean.FALSE; } else if (valueClass.equals(Byte.class)) { return new Byte(fieldValue); } else if (valueClass.equals(Integer.class)) { return Integer.valueOf(fieldValue); } else if (valueClass.equals(Long.class)) { return new Long(fieldValue); } else if (valueClass.equals(Short.class)) { return new Short(fieldValue); } else if (valueClass.equals(Double.class)) { return new Double(fieldValue); } else if (valueClass.equals(Float.class)) { return new Float(fieldValue); } else if (valueClass.equals(java.math.BigDecimal.class)) { return new java.math.BigDecimal(fieldValue); } else if (valueClass.equals(java.util.Date.class)) { return dateFormat.parse(fieldValue); } else if (valueClass.equals(java.sql.Timestamp.class)) { return new java.sql.Timestamp(dateFormat.parse(fieldValue).getTime()); } else if (valueClass.equals(java.sql.Time.class)) { return new java.sql.Time(dateFormat.parse(fieldValue).getTime()); } else if (valueClass.equals(java.lang.Number.class)) { return new Double(fieldValue); } else { throw new JRException("Field '" + jrField.getName() + "', string value '" + fieldValue + "' is of class '" + fieldValues.get(jrField.getName()).getClass() + "' and can not be converted to class " + valueClass.getName()); } } catch (Exception e) { throw new JRException("Unable to get value for field '" + jrField.getName() + "' of class '" + valueClass.getName() + "'", e); } }
From source file:pe.com.mmh.sisgap.utils.BasicDynaBean.java
/** * Return the value of a simple property with the specified name. * * @param name Name of the property whose value is to be retrieved * * @exception IllegalArgumentException if there is no property * of the specified name//from w w w. j av a 2 s . co m */ public Object get(String name) { // Return any non-null value for the specified property Object value = values.get(name); if (value != null) { return (value); } // Return a null value for a non-primitive property Class type = getDynaProperty(name).getType(); if (!type.isPrimitive()) { return (value); } // Manufacture default values for primitive properties if (type == Boolean.TYPE) { return (Boolean.FALSE); } else if (type == Byte.TYPE) { return (new Byte((byte) 0)); } else if (type == Character.TYPE) { return (new Character((char) 0)); } else if (type == Double.TYPE) { return (new Double((double) 0.0)); } else if (type == Float.TYPE) { return (new Float((float) 0.0)); } else if (type == Integer.TYPE) { return (new Integer((int) 0)); } else if (type == Long.TYPE) { return (new Long((int) 0)); } else if (type == Short.TYPE) { return (new Short((short) 0)); } else { return (null); } }