List of usage examples for java.lang Byte parseByte
public static byte parseByte(String s) throws NumberFormatException
From source file:org.sonar.api.batch.rule.Checks.java
private static void configureField(Object check, Field field, String value) { try {/*from www .ja v a2 s .c o m*/ field.setAccessible(true); if (field.getType().equals(String.class)) { field.set(check, value); } else if (int.class == field.getType()) { field.setInt(check, Integer.parseInt(value)); } else if (short.class == field.getType()) { field.setShort(check, Short.parseShort(value)); } else if (long.class == field.getType()) { field.setLong(check, Long.parseLong(value)); } else if (double.class == field.getType()) { field.setDouble(check, Double.parseDouble(value)); } else if (boolean.class == field.getType()) { field.setBoolean(check, Boolean.parseBoolean(value)); } else if (byte.class == field.getType()) { field.setByte(check, Byte.parseByte(value)); } else if (Integer.class == field.getType()) { field.set(check, Integer.parseInt(value)); } else if (Long.class == field.getType()) { field.set(check, Long.parseLong(value)); } else if (Double.class == field.getType()) { field.set(check, Double.parseDouble(value)); } else if (Boolean.class == field.getType()) { field.set(check, Boolean.parseBoolean(value)); } else { throw new SonarException( "The type of the field " + field + " is not supported: " + field.getType()); } } catch (IllegalAccessException e) { throw new SonarException( "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(), e); } }
From source file:com.espertech.esper.regression.expr.TestPerRowFunc.java
public void testCoalesceDouble() { EPStatement selectTestView = setupCoalesce( "coalesce(null, byteBoxed, shortBoxed, intBoxed, longBoxed, floatBoxed, doubleBoxed)"); assertEquals(Double.class, selectTestView.getEventType().getPropertyType("result")); sendEventWithDouble(null, null, null, null, null, null); assertEquals(null, testListener.assertOneGetNewAndReset().get("result")); sendEventWithDouble(null, Short.parseShort("2"), null, null, null, 1d); assertEquals(2d, testListener.assertOneGetNewAndReset().get("result")); sendEventWithDouble(null, null, null, null, null, 100d); assertEquals(100d, testListener.assertOneGetNewAndReset().get("result")); sendEventWithDouble(null, null, null, null, 10f, 100d); assertEquals(10d, testListener.assertOneGetNewAndReset().get("result")); sendEventWithDouble(null, null, 1, 5l, 10f, 100d); assertEquals(1d, testListener.assertOneGetNewAndReset().get("result")); sendEventWithDouble(Byte.parseByte("3"), null, null, null, null, null); assertEquals(3d, testListener.assertOneGetNewAndReset().get("result")); sendEventWithDouble(null, null, null, 5l, 10f, 100d); assertEquals(5d, testListener.assertOneGetNewAndReset().get("result")); }
From source file:cern.c2mon.shared.common.datatag.address.impl.HardwareAddressImpl.java
/** * Create a HardwareAddress object from its XML representation. * * @param pElement DOM element containing the XML representation of a HardwareAddress object, as created by the * toConfigXML() method. * @throws RuntimeException if unable to instantiate the Hardware address * @see cern.c2mon.shared.common.datatag.address.HardwareAddress#toConfigXML() *///from ww w.j a va2 s .co m public final synchronized HardwareAddress fromConfigXML(Element pElement) { Class hwAddressClass = null; HardwareAddressImpl hwAddress = null; try { hwAddressClass = Class.forName(pElement.getAttribute("class")); hwAddress = (HardwareAddressImpl) hwAddressClass.newInstance(); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); throw new RuntimeException("Exception caught when instantiating a hardware address from XML", cnfe); } catch (IllegalAccessException iae) { iae.printStackTrace(); throw new RuntimeException("Exception caught when instantiating a hardware address from XML", iae); } catch (InstantiationException ie) { ie.printStackTrace(); throw new RuntimeException("Exception caught when instantiating a hardware address from XML", ie); } NodeList fields = pElement.getChildNodes(); Node fieldNode = null; int fieldsCount = fields.getLength(); String fieldName; String fieldValueString; String fieldTypeName = ""; for (int i = 0; i < fieldsCount; i++) { fieldNode = fields.item(i); if (fieldNode.getNodeType() == Node.ELEMENT_NODE) { fieldName = fieldNode.getNodeName(); if (fieldNode.getFirstChild() != null) { fieldValueString = fieldNode.getFirstChild().getNodeValue(); } else { fieldValueString = ""; } try { Field field = hwAddressClass.getDeclaredField(decodeFieldName(fieldName)); fieldTypeName = field.getType().getName(); if (fieldTypeName.equals("short")) { field.setShort(hwAddress, Short.parseShort(fieldValueString)); } else if (fieldTypeName.equals("java.lang.Short")) { field.set(hwAddress, new Integer(Integer.parseInt(fieldValueString))); } else if (fieldTypeName.equals("int")) { field.setInt(hwAddress, Integer.parseInt(fieldValueString)); } else if (fieldTypeName.equals("java.lang.Integer")) { field.set(hwAddress, new Integer(Integer.parseInt(fieldValueString))); } else if (fieldTypeName.equals("float")) { field.setFloat(hwAddress, Float.parseFloat(fieldValueString)); } else if (fieldTypeName.equals("java.lang.Float")) { field.set(hwAddress, new Float(Float.parseFloat(fieldValueString))); } else if (fieldTypeName.equals("double")) { field.setDouble(hwAddress, Double.parseDouble(fieldValueString)); } else if (fieldTypeName.equals("java.lang.Double")) { field.set(hwAddress, new Double(Double.parseDouble(fieldValueString))); } else if (fieldTypeName.equals("long")) { field.setLong(hwAddress, Long.parseLong(fieldValueString)); } else if (fieldTypeName.equals("java.lang.Long")) { field.set(hwAddress, new Long(Long.parseLong(fieldValueString))); } else if (fieldTypeName.equals("byte")) { field.setByte(hwAddress, Byte.parseByte(fieldValueString)); } else if (fieldTypeName.equals("java.lang.Byte")) { field.set(hwAddress, new Byte(Byte.parseByte(fieldValueString))); } else if (fieldTypeName.equals("char")) { field.setChar(hwAddress, fieldValueString.charAt(0)); } else if (fieldTypeName.equals("java.lang.Character")) { field.set(hwAddress, new Character(fieldValueString.charAt(0))); } else if (fieldTypeName.equals("boolean")) { field.setBoolean(hwAddress, Boolean.getBoolean(fieldValueString)); } else if (fieldTypeName.equals("java.lang.Boolean")) { field.set(hwAddress, new Boolean(Boolean.getBoolean(fieldValueString))); } else if (fieldTypeName.equals("java.util.HashMap")) { field.set(hwAddress, SimpleXMLParser.domNodeToMap(fieldNode)); } else if (field.getType().isEnum()) { Object[] enumConstants = field.getType().getEnumConstants(); for (Object enumConstant : enumConstants) { if (enumConstant.toString().equals(fieldValueString)) { field.set(hwAddress, enumConstant); } } } else { field.set(hwAddress, fieldValueString); } } catch (NoSuchFieldException nsfe) { String errorMsg = "fromConfigXML(...) - Error occured while parsing XML <HardwareAddress> tag. " + "The following variable does not exist in " + hwAddressClass.toString() + ": \"" + decodeFieldName(fieldName) + "\""; log.error(errorMsg); throw new IllegalArgumentException(errorMsg); } catch (IllegalAccessException iae) { iae.printStackTrace(); throw new RuntimeException(iae); } catch (NumberFormatException npe) { String errorMsg = "fromConfigXML(...) - Error occured while parsing XML <HardwareAddress> tag. Field \"" + fieldName + "\" shall not be empty since we expect a \"" + fieldTypeName + "\" value. Please correct the XML configuration for " + hwAddressClass.toString(); log.error(errorMsg); throw new IllegalArgumentException(errorMsg); } } } return hwAddress; }
From source file:com.codebutler.farebot.card.felica.FelicaCard.java
public static FelicaCard fromXml(byte[] tagId, Date scannedAt, Element element) { Element systemsElement = (Element) element.getElementsByTagName("systems").item(0); NodeList systemElements = systemsElement.getElementsByTagName("system"); FeliCaLib.IDm idm = new FeliCaLib.IDm( Base64.decode(element.getElementsByTagName("idm").item(0).getTextContent(), Base64.DEFAULT)); FeliCaLib.PMm pmm = new FeliCaLib.PMm( Base64.decode(element.getElementsByTagName("pmm").item(0).getTextContent(), Base64.DEFAULT)); FelicaSystem[] systems = new FelicaSystem[systemElements.getLength()]; for (int x = 0; x < systemElements.getLength(); x++) { Element systemElement = (Element) systemElements.item(x); int systemCode = Integer.parseInt(systemElement.getAttribute("code")); Element servicesElement = (Element) systemElement.getElementsByTagName("services").item(0); NodeList serviceElements = servicesElement.getElementsByTagName("service"); FelicaService[] services = new FelicaService[serviceElements.getLength()]; for (int y = 0; y < serviceElements.getLength(); y++) { Element serviceElement = (Element) serviceElements.item(y); int serviceCode = Integer.parseInt(serviceElement.getAttribute("code")); Element blocksElement = (Element) serviceElement.getElementsByTagName("blocks").item(0); NodeList blockElements = blocksElement.getElementsByTagName("block"); FelicaBlock[] blocks = new FelicaBlock[blockElements.getLength()]; for (int z = 0; z < blockElements.getLength(); z++) { Element blockElement = (Element) blockElements.item(z); byte address = Byte.parseByte(blockElement.getAttribute("address")); byte[] data = Base64.decode(blockElement.getTextContent(), Base64.DEFAULT); blocks[z] = new FelicaBlock(address, data); }/*from w ww . jav a 2s . c o m*/ services[y] = new FelicaService(serviceCode, blocks); } systems[x] = new FelicaSystem(systemCode, services); } return new FelicaCard(tagId, scannedAt, idm, pmm, systems); }
From source file:cn.ac.ncic.mastiff.io.coding.DeltaBinaryArrayZigZarByteReader.java
public byte[] CompressensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 0, inBuf.getLength()); FlexibleEncoding.Parquet.DeltaByteArrayReader reader = new FlexibleEncoding.Parquet.DeltaByteArrayReader(); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip();//from ww w . j av a 2s . co m reader.initFromPage(numPairs, byteBuf.array(), 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { byte tmp = Byte.parseByte(reader.readBytes().toStringUsingUTF8()); decoding.writeByte(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:org.lambdamatic.internal.elasticsearch.codec.DocumentCodec.java
/** * Converts the given {@code value} into a value of type {@code targetType}. * <p>// w w w. j a va 2 s. co m * <strong>Note:</strong>The conversion relies on the existence of a static * {@code valueOf(String)} method in the given {@code targetType} to convert the value. * </p> * * @param value the input value * @param targetType the target type of the value to return * @return the converted value (or the value itself if no conversion was necessary) */ protected static Object convertValue(final Object value, final Class<?> targetType) { if (targetType.isAssignableFrom(value.getClass())) { return value; } else if (targetType.isPrimitive()) { if (targetType == boolean.class) { return Boolean.parseBoolean(value.toString()); } else if (targetType == byte.class) { return Byte.parseByte(value.toString()); } else if (targetType == short.class) { return Short.parseShort(value.toString()); } else if (targetType == int.class) { return Integer.parseInt(value.toString()); } else if (targetType == long.class) { return Long.parseLong(value.toString()); } else if (targetType == double.class) { return Double.parseDouble(value.toString()); } else if (targetType == float.class) { return Float.parseFloat(value.toString()); } throw new CodecException("Failed to convert value '" + value.toString() + "' (" + value.getClass().getName() + ") into a " + targetType.getName() + ": no object to primitive conversion available."); } try { final Method convertMethod = getConvertMethod(targetType); if (convertMethod != null) { return convertMethod.invoke(null, value.toString()); } throw new CodecException( "Failed to convert value '" + value.toString() + "' (" + value.getClass().getName() + ") into a " + targetType.getName() + ": no conversion method available."); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { throw new CodecException("Failed to convert value '" + value.toString() + "' (" + value.getClass().getName() + ") into a " + targetType.getClass().getName(), e); } }
From source file:com.limegroup.gnutella.metadata.MP3DataEditor.java
/** * Actually writes the ID3 tags out to the ID3V1 section of mp3 file. *//* w ww. j av a 2 s . c om*/ private int writeID3V1DataToDisk(RandomAccessFile file) { byte[] buffer = new byte[30];//max buffer length...drop/pickup vehicle //see if there are ID3 Tags in the file String tag = ""; try { file.readFully(buffer, 0, 3); tag = new String(buffer, 0, 3); } catch (EOFException e) { return LimeXMLReplyCollection.RW_ERROR; } catch (IOException e) { return LimeXMLReplyCollection.RW_ERROR; } //We are sure this is an MP3 file.Otherwise this method would never //be called. if (!tag.equals("TAG")) { //Write the TAG try { byte[] tagBytes = "TAG".getBytes();//has to be len 3 file.seek(file.length() - 128);//reset the file-pointer file.write(tagBytes, 0, 3);//write these three bytes into the File } catch (IOException ioe) { return LimeXMLReplyCollection.BAD_ID3; } } LOG.debug("about to start writing to file"); boolean b; b = toFile(title_, 30, file, buffer); if (!b) return LimeXMLReplyCollection.FAILED_TITLE; b = toFile(artist_, 30, file, buffer); if (!b) return LimeXMLReplyCollection.FAILED_ARTIST; b = toFile(album_, 30, file, buffer); if (!b) return LimeXMLReplyCollection.FAILED_ALBUM; b = toFile(year_, 4, file, buffer); if (!b) return LimeXMLReplyCollection.FAILED_YEAR; //comment and track (a little bit tricky) b = toFile(comment_, 28, file, buffer);//28 bytes for comment if (!b) return LimeXMLReplyCollection.FAILED_COMMENT; byte trackByte = (byte) -1;//initialize try { if (track_ == null || track_.equals("")) trackByte = (byte) 0; else trackByte = Byte.parseByte(track_); } catch (NumberFormatException nfe) { return LimeXMLReplyCollection.FAILED_TRACK; } try { file.write(0);//separator b/w comment and track(track is optional) file.write(trackByte); } catch (IOException e) { return LimeXMLReplyCollection.FAILED_TRACK; } //genre byte genreByte = getGenreByte(); try { file.write(genreByte); } catch (IOException e) { return LimeXMLReplyCollection.FAILED_GENRE; } //come this far means we are OK. return LimeXMLReplyCollection.NORMAL; }
From source file:com.tamnd.core.util.ZConfig.java
public byte getByte(String key) throws NotExistException, InvalidParamException { return Byte.parseByte(_getProperty(key)); }
From source file:org.apache.uima.ruta.action.MarkTableAction.java
private void fillFeatures(TOP structure, Map<String, Integer> map, AnnotationFS annotationFS, RuleElement element, List<String> row, RutaStream stream) { List<?> featuresList = structure.getType().getFeatures(); for (int i = 0; i < featuresList.size(); i++) { Feature targetFeature = (Feature) featuresList.get(i); String name = targetFeature.getName(); String shortFName = name.substring(name.indexOf(":") + 1, name.length()); Integer entryIndex = map.get(shortFName); Type range = targetFeature.getRange(); if (entryIndex != null && row.size() >= entryIndex) { String value = row.get(entryIndex - 1); if (range.getName().equals(UIMAConstants.TYPE_STRING)) { structure.setStringValue(targetFeature, value); } else if (range.getName().equals(UIMAConstants.TYPE_INTEGER)) { Integer integer = Integer.parseInt(value); structure.setIntValue(targetFeature, integer); } else if (range.getName().equals(UIMAConstants.TYPE_DOUBLE)) { Double d = Double.parseDouble(value); structure.setDoubleValue(targetFeature, d); } else if (range.getName().equals(UIMAConstants.TYPE_FLOAT)) { Float d = Float.parseFloat(value); structure.setFloatValue(targetFeature, d); } else if (range.getName().equals(UIMAConstants.TYPE_BYTE)) { Byte d = Byte.parseByte(value); structure.setByteValue(targetFeature, d); } else if (range.getName().equals(UIMAConstants.TYPE_SHORT)) { Short d = Short.parseShort(value); structure.setShortValue(targetFeature, d); } else if (range.getName().equals(UIMAConstants.TYPE_LONG)) { Long d = Long.parseLong(value); structure.setLongValue(targetFeature, d); } else if (range.getName().equals(UIMAConstants.TYPE_BOOLEAN)) { Boolean b = Boolean.parseBoolean(value); structure.setBooleanValue(targetFeature, b); } else { }/*from w ww . ja va 2 s . c om*/ } } }
From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEByteReader.java
public byte[] CompressensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 0, inBuf.getLength()); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip();// w w w .j a v a 2 s.c o m DataInputBuffer dib = new DataInputBuffer(); dib.reset(byteBuf.array(), 0, byteBuf.array().length); int dictionarySize = dib.readInt(); int OnlydictionarySize = dib.readInt(); dib.reset(byteBuf.array(), 8, dictionarySize - 4); byte[] dictionaryBuffer = dib.getData(); dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4)); byte[] dictionaryId = dib.getData(); dib.close(); DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer, PrimitiveType.PrimitiveTypeName.BINARY); cr.initFromPage(numPairs, dictionaryId, 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { byte tmp = Byte.parseByte(cr.readBytes().toStringUsingUTF8()); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }