List of usage examples for java.lang Byte parseByte
public static byte parseByte(String s) throws NumberFormatException
From source file:de.cebitec.readXplorer.util.GeneralUtils.java
/** * Checks if the input string is a valid byte larger than or equal to 0. * @param text input string to check/*from www .j av a 2 s . c o m*/ * @return <code>true</code> if it is a valid input string, * <code>false</code> otherwise */ public static boolean isValidByteInput(String text) { try { return Byte.parseByte(text) >= 0; } catch (NumberFormatException e) { return false; } }
From source file:quarks.freeautomaticbridg.CommunicationServer.java
/** * Is processing the html get request//from w w w. ja v a 2s . c o m * * Supportet is the following scema * <br> host:port/rest.api?<addr=(number ignored by ethernet nodss )>&node=(nodename)&port=[port]&command=[CMD_SET[..]|CMD_GET[..]] * * <br> CMD_SET : type=RGB expectet three values r=0-255 & g=... and b * <br> : type=SW_AND send a shortvalue with hint to alter the states this should be implementet as an logical or * <br> : type=SW_OR send a shortvalue with hint to reset the states completly expectet &value=[integer] * <br> a single Switche port can handle up to 16 switches using bit operation (16 bit) expectet &value=[integer] * * * * @param requestMap * @throws java.io.IOException */ public void response(HashMap<String, String> requestMap, HtmlGetRequestHandler handler) throws IOException { byte dest_addr = Byte.parseByte(requestMap.get("addr")); CommunicationSubSystem comSub = this.communicationSubSystems.get(requestMap.get("node")); if (requestMap.get("command").compareTo("CMD_SET") == 0) { String type = requestMap.get("type"); // send RGB SET if (type.compareTo("RGB") == 0) { byte port = Byte.parseByte(requestMap.get("port")); int r = Integer.parseInt(requestMap.get("r")); int g = Integer.parseInt(requestMap.get("g")); int b = Integer.parseInt(requestMap.get("b")); log.info("Recive CMD_SET: for node:" + dest_addr + ":" + port + " RGB "); PackageBuilder pack = new PackageBuilder(dest_addr, (byte) 0, port, (byte) PackageBuilder.CMD_SET, r, g, b); WorkingSet ws = new WorkingSet(handler, pack); comSub.putWorkingSet(ws); } if (type.compareTo("SW_AND") == 0) { byte port = Byte.parseByte(requestMap.get("port")); int value = Integer.parseInt(requestMap.get("value")); log.info("Recive CMD_SET: for node:" + dest_addr + ":" + port + " SW_AND "); byte command = (byte) PackageBuilder.CMD_SET + (byte) PackageBuilder.TYPE_SW_AND; PackageBuilder pack = new PackageBuilder(dest_addr, (byte) 0, port, command, (short) value, (short) 0, (short) 0, (short) 0); WorkingSet ws = new WorkingSet(handler, pack); comSub.putWorkingSet(ws); } if (type.compareTo("SW_OR") == 0) { byte port = Byte.parseByte(requestMap.get("port")); int value = Integer.parseInt(requestMap.get("value")); log.info("Recive CMD_SET: for node:" + dest_addr + ":" + port + " SW_AND "); byte command = (byte) PackageBuilder.CMD_SET + (byte) PackageBuilder.TYPE_SW_OR; PackageBuilder pack = new PackageBuilder(dest_addr, (byte) 0, port, command, (short) value, (short) 0, (short) 0, (short) 0); WorkingSet ws = new WorkingSet(handler, pack); comSub.putWorkingSet(ws); } } if (requestMap.get("command").compareTo("CMD_GET") == 0) { byte port = Byte.parseByte(requestMap.get("port")); log.info("Recive CMD_GET: for node:" + dest_addr + ":" + port); PackageBuilder pack = new PackageBuilder(dest_addr, (byte) 0, port, (byte) PackageBuilder.CMD_GET, (short) 0, (short) 0, (short) 0, (short) 0); WorkingSet ws = new WorkingSet(handler, pack); comSub.putWorkingSet(ws); } }
From source file:org.apache.tajo.storage.TextSerializerDeserializer.java
@Override public Datum deserialize(int index, byte[] bytes, int offset, int length, byte[] nullCharacters) throws IOException { Column col = schema.getColumn(index); TajoDataTypes.Type type = col.getDataType().getType(); Datum datum;//from www . ja v a 2 s . c om switch (type) { case BOOLEAN: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createBool(bytes[offset] == 't' || bytes[offset] == 'T'); break; case BIT: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createBit(Byte.parseByte(new String(bytes, offset, length))); break; case CHAR: datum = isNullText(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createChar(new String(bytes, offset, length).trim()); break; case INT1: case INT2: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createInt2((short) NumberUtil.parseInt(bytes, offset, length)); break; case INT4: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createInt4(NumberUtil.parseInt(bytes, offset, length)); break; case INT8: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createInt8(new String(bytes, offset, length)); break; case FLOAT4: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createFloat4(new String(bytes, offset, length)); break; case FLOAT8: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createFloat8(NumberUtil.parseDouble(bytes, offset, length)); break; case TEXT: { byte[] chars = new byte[length]; System.arraycopy(bytes, offset, chars, 0, length); datum = isNullText(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createText(chars); break; } case DATE: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createDate(new String(bytes, offset, length)); break; case TIME: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createTime(new String(bytes, offset, length)); break; case INTERVAL: datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get() : DatumFactory.createInterval(new String(bytes, offset, length)); break; case PROTOBUF: { if (isNull(bytes, offset, length, nullCharacters)) { datum = NullDatum.get(); } else { ProtobufDatumFactory factory = ProtobufDatumFactory.get(col.getDataType()); Message.Builder builder = factory.newBuilder(); try { byte[] protoBytes = new byte[length]; System.arraycopy(bytes, offset, protoBytes, 0, length); protobufJsonFormat.merge(protoBytes, builder); datum = factory.createDatum(builder.build()); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } break; } case BLOB: { if (isNull(bytes, offset, length, nullCharacters)) { datum = NullDatum.get(); } else { byte[] blob = new byte[length]; System.arraycopy(bytes, offset, blob, 0, length); datum = DatumFactory.createBlob(Base64.decodeBase64(blob)); } break; } default: datum = NullDatum.get(); break; } return datum; }
From source file:com.github.strawberry.guice.config.ConfigLoader.java
private static Option getFromProperties(Map properties, final Field field, final Config annotation) { Object value = null;//from w w w . j a v a 2 s . co m Class<?> fieldType = field.getType(); String pattern = annotation.value(); boolean allowNull = annotation.allowNull(); Set<String> matchingKeys = getKeys(properties, pattern); if (matchingKeys.size() == 1) { String matchingKey = Iterables.getOnlyElement(matchingKeys); if (fieldType.equals(char[].class)) { value = properties.get(matchingKey).toString().toCharArray(); } else if (fieldType.equals(Character[].class)) { value = ArrayUtils.toObject(properties.get(matchingKey).toString().toCharArray()); } else if (fieldType.equals(char.class) || fieldType.equals(Character.class)) { String toConvert = properties.get(matchingKey).toString(); if (toConvert.length() == 1) { value = properties.get(matchingKey).toString().charAt(0); } else { throw ConversionException.of(toConvert, matchingKey, fieldType); } } else if (fieldType.equals(String.class)) { value = properties.get(matchingKey); } else if (fieldType.equals(byte[].class)) { if (properties.containsKey(matchingKey)) { value = properties.get(matchingKey).toString().getBytes(); } else { value = null; } } else if (fieldType.equals(Byte[].class)) { value = ArrayUtils.toObject(properties.get(matchingKey).toString().getBytes()); } else if (fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) { String toConvert = properties.get(matchingKey).toString(); if (BOOLEAN.matcher(toConvert).matches()) { value = TRUE.matcher(toConvert).matches(); } else { throw ConversionException.of(toConvert, matchingKey, fieldType); } } else if (Map.class.isAssignableFrom(fieldType)) { value = mapOf(field, properties, matchingKey); } else if (Collection.class.isAssignableFrom(fieldType)) { value = collectionOf(field, properties, matchingKey); } else { String toConvert = properties.get(matchingKey).toString(); try { if (fieldType.equals(byte.class) || fieldType.equals(Byte.class)) { value = Byte.parseByte(properties.get(matchingKey).toString()); } else if (fieldType.equals(short.class) || fieldType.equals(Short.class)) { value = Short.parseShort(toConvert); } else if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) { value = Integer.parseInt(toConvert); } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) { value = Long.parseLong(toConvert); } else if (fieldType.equals(BigInteger.class)) { value = new BigInteger(toConvert); } else if (fieldType.equals(float.class) || fieldType.equals(Float.class)) { value = Float.parseFloat(toConvert); } else if (fieldType.equals(double.class) || fieldType.equals(Double.class)) { value = Double.parseDouble(toConvert); } else if (fieldType.equals(BigDecimal.class)) { value = new BigDecimal(toConvert); } } catch (NumberFormatException exception) { throw ConversionException.of(exception, toConvert, matchingKey, fieldType); } } } else if (matchingKeys.size() > 1) { if (Map.class.isAssignableFrom(fieldType)) { value = nestedMapOf(field, properties, matchingKeys); } else if (Collection.class.isAssignableFrom(fieldType)) { value = nestedCollectionOf(field, properties, matchingKeys); } } else { if (!allowNull) { value = nonNullValueOf(fieldType); } } return Option.fromNull(value); }
From source file:com.strider.datadefender.requirement.Parameter.java
private Object getTypeValueOf(final String type, final String value) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { if (type.equals(boolean.class.getName())) { return Boolean.parseBoolean(value); } else if (type.equals(byte.class.getName())) { return Byte.parseByte(value); } else if (type.equals(short.class.getName())) { return Short.parseShort(value); } else if (type.equals(char.class.getName())) { return value.charAt(0); } else if (type.equals(int.class.getName())) { return Integer.parseInt(value); } else if (type.equals(long.class.getName())) { return Long.parseLong(value); } else if (type.equals(float.class.getName())) { return Float.parseFloat(value); } else if (type.equals(double.class.getName())) { return Double.parseDouble(value); } else if (type.equals(String.class.getName()) || "String".equals(type)) { return value; } else {/* w w w .j a va 2 s . co m*/ final Constructor<?> constr = Class.forName(type).getConstructor(String.class); return constr.newInstance(value); } }
From source file:org.hachreak.projects.networkcodingsip2peer.behavior.PublishEncodedFileClientBehavior.java
/** * Load configuration file/*from ww w . j a va2 s .c o m*/ * * @throws IOException */ private void initFromConfigFile() throws IOException { InputStream i = new FileInputStream(getPeer().getPathConfig()); // peer configuration configFile.load(i); if (configFile.containsKey(GF_N)) { GF_n = Byte.parseByte(configFile.getProperty(GF_N)); galoisField = new GaloisField(GF_n); } if (configFile.containsKey(FRAGMENT_SIZE)) fragmentSize = Integer.parseInt(configFile.getProperty(FRAGMENT_SIZE)); if (configFile.containsKey(REDUNDANCY_RATE)) redundancyRate = Float.parseFloat(configFile.getProperty(REDUNDANCY_RATE)); }
From source file:org.opennaas.extensions.router.junos.commandsets.digester.RoutingOptionsParser.java
public void setDestinationAddress(String ipv4) { NextHopIPRoute ipRoute = (NextHopIPRoute) peek(); try {/*from w w w.j a va 2 s . c om*/ // Parsing ip/mask String shortMask = ipv4.split("/")[1]; String ip = ipv4.split("/")[0]; String maskIpv4 = IPUtilsHelper.parseShortToLongIpv4NetMask(shortMask); // adding to the model ipRoute.setDestinationAddress(ip); ipRoute.setPrefixLength(Byte.parseByte(shortMask)); ipRoute.setDestinationMask(maskIpv4); ipRoute.setIsStatic(true); } catch (Exception e) { log.error(e.getMessage()); } }
From source file:com.npower.dm.dispatch.JobNotificationSenderImpl.java
/** * Bootstrap PIN Type//from w w w.ja v a 2 s . co m * @param device * @return */ private OMACPSecurityMethod getBootstrapPinType(Device device) { String pTypeStr = device.getBootstrapPinType(); // PinType, SubscriberPIN Type if (StringUtils.isEmpty(pTypeStr)) { pTypeStr = device.getSubscriber().getBootstrapPinType(); } // DeviceSubscriberPinType, CarrierPinType if (StringUtils.isEmpty(pTypeStr)) { Carrier carrier = device.getSubscriber().getCarrier(); pTypeStr = carrier.getDefaultBootstrapPinType(); } // if (StringUtils.isEmpty(pTypeStr)) { // Return default; return OMACPSecurityMethod.USERPIN; } OMACPSecurityMethod result = OMACPSecurityMethod.value(Byte.parseByte(pTypeStr)); return result; }
From source file:net.floodlightcontroller.loadbalancer.MonitorsResource.java
protected LBMonitor jsonToMonitor(String json) throws IOException { MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp;/*from ww w. ja v a 2 s . c om*/ LBMonitor monitor = new LBMonitor(); try { jp = f.createJsonParser(json); } catch (JsonParseException e) { throw new IOException(e); } jp.nextToken(); if (jp.getCurrentToken() != JsonToken.START_OBJECT) { throw new IOException("Expected START_OBJECT"); } while (jp.nextToken() != JsonToken.END_OBJECT) { if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { throw new IOException("Expected FIELD_NAME"); } String n = jp.getCurrentName(); jp.nextToken(); if (jp.getText().equals("")) continue; else if (n.equals("monitor")) { while (jp.nextToken() != JsonToken.END_OBJECT) { String field = jp.getCurrentName(); if (field.equals("id")) { monitor.id = jp.getText(); continue; } if (field.equals("name")) { monitor.name = jp.getText(); continue; } if (field.equals("type")) { monitor.type = Short.parseShort(jp.getText()); continue; } if (field.equals("delay")) { monitor.delay = Short.parseShort(jp.getText()); continue; } if (field.equals("timeout")) { monitor.timeout = Short.parseShort(jp.getText()); continue; } if (field.equals("attempts_before_deactivation")) { monitor.attemptsBeforeDeactivation = Short.parseShort(jp.getText()); continue; } if (field.equals("network_id")) { monitor.netId = jp.getText(); continue; } if (field.equals("address")) { monitor.address = Integer.parseInt(jp.getText()); continue; } if (field.equals("protocol")) { monitor.protocol = Byte.parseByte(jp.getText()); continue; } if (field.equals("port")) { monitor.port = Short.parseShort(jp.getText()); continue; } if (field.equals("admin_state")) { monitor.adminState = Short.parseShort(jp.getText()); continue; } if (field.equals("status")) { monitor.status = Short.parseShort(jp.getText()); continue; } log.warn("Unrecognized field {} in " + "parsing Vips", jp.getText()); } } } jp.close(); return monitor; }
From source file:org.thelq.stackexchange.dbimport.DumpParser.java
public void parseNextEntry() { try {//from www . j a v a 2s . c o m if (!enabled) throw new RuntimeException("Parser is disabled"); else if (endOfFile) throw new RuntimeException("Reached end of file, cannot continue"); else if (parsedCount == 0) log.info("Starting parsing {}", dumpEntry.getName()); int eventType = xmlReader.nextTag(); String curElement = xmlReader.getName().toString(); //System.out.println("Current element: " + curElement); if (curElement.equals(getRoot())) { //Were done, shutdown this parser endOfFile = true; log.info("Done parsing {}, parsed {} enteries", dumpEntry.getName(), parsedCount); return; } else if (eventType != XMLEvent.START_ELEMENT) throw new RuntimeException("Unexpected event " + ErrorConsts.tokenTypeDesc(eventType)); //Build attributes map log.debug("Parsing entry {}", parsedCount); Map<String, Object> attributesMap = ArrayMap.create(xmlReader.getAttributeCount()); for (int i = 0; i < xmlReader.getAttributeCount(); i++) { String attributeName = xmlReader.getAttributeLocalName(i); Type attributeType = properties.get(attributeName); if (attributeType == null) throw new RuntimeException( "Unknown column " + attributeName + " at " + xmlReader.getLocation()); Class attributeTypeClass = attributeType.getReturnedClass(); String attributeValueRaw = xmlReader.getAttributeValue(i); //Attempt to convert to number if nessesary Object attributeValue; if (attributeTypeClass == Date.class) { log.debug("Converting {} to a date", attributeValueRaw); if (attributeValueRaw.length() < 11) attributeValue = dateFormatterShort.parse(attributeValueRaw); else attributeValue = dateFormatterLong.parse(attributeValueRaw); } else if (attributeTypeClass == Byte.class) { log.debug("Converting {} to a byte", attributeValueRaw); attributeValue = Byte.parseByte(attributeValueRaw); } else if (attributeTypeClass != String.class) { log.debug("Converting {} to class {}", attributeValueRaw, attributeTypeClass); if (attributeValueRaw.isEmpty()) attributeValue = 0; else attributeValue = NumberUtils.createNumber(attributeValueRaw); } else attributeValue = attributeValueRaw; attributesMap.put(attributeName, attributeValue); } //Advance to END_ELEMENT, skipping the attributes and other stuff while (xmlReader.next() != XMLEvent.END_ELEMENT) { } parsedCount++; databaseWriter.insertData(attributesMap); } catch (Exception e) { throw new RuntimeException("Cannot parse entry in " + dumpEntry.getLocation() + " #" + (parsedCount + 1) + " at " + xmlReader.getLocation(), e); } }