📄 propcodes.java
字号:
if (len != elemLen) { data = null; // should never occur break; } ofs += elemLen; } } else if (attr.isNumeric()) { int elemLen = attr.getTypeLength(), ofs = 0; data = new byte[elemLen * arrayLen]; for (int i = 0; i < arrayLen; i++) { long val = 0L; if (value[i] instanceof Number) { Number v = (Number)value[i]; val = attr.isDecimal()? Math.round(v.doubleValue() * 10L) : v.longValue(); } else { // only numeric object types are allowed data = null; break; } int len = Payload.encodeLong(data, ofs, elemLen, true, val); if (len != elemLen) { data = null; // should never occur break; } ofs += elemLen; } } /* return bytes */ return data; } // ------------------------------------------------------------------------ /* encode set-property long */ public static byte[] encodePropertyData(int key, long value) { return encodePropertyData(key, new long[] { value }); } /* encode set-property longs */ public static byte[] encodePropertyData(int key, long value[]) { /* nothing to encode? */ if ((value == null) || (value.length == 0)) { Print.logError("Null/empty value specified"); return null; } /* get property attributes */ PropCodes.Attr attr = PropCodes.getAttr(key); if (attr == null) { Print.logError("Property not found: 0x" + StringTools.toHexString(key)); return null; } /* numeric only */ if (!attr.isNumeric() && !attr.isBoolean()) { Print.logError("Property is not a numeric: 0x" + StringTools.toHexString(key)); return null; } /* proper array length? */ int arrayLen = attr.getArrayLength(); if (arrayLen > value.length) { Print.logError("Invalid array length [expected " + arrayLen + "]"); return null; } /* data buffer */ int elemLen = attr.getTypeLength(); byte data[] = new byte[elemLen * arrayLen]; int ofs = 0; /* encode numeric */ for (int i = 0; i < arrayLen; i++) { long val = 0L; if (attr.isBoolean()) { val = (value[i] != 0L)? 1L : 0L; } else if (attr.isDecimal()) { val = value[i] * 10L; } else { val = value[i]; } int n = Payload.encodeLong(data, ofs, elemLen, true, val); if (n != elemLen) { return null; // should never occur } ofs += elemLen; } /* return bytes */ return data; } // ------------------------------------------------------------------------ /* encode set-property double */ public static byte[] encodePropertyData(int key, double value) { return encodePropertyData(key, new double[] { value }); } /* encode set-property doubles */ public static byte[] encodePropertyData(int key, double value[]) { /* nothing to encode? */ if ((value == null) || (value.length == 0)) { Print.logError("Null/empty value specified"); return null; } /* get property attributes */ PropCodes.Attr attr = PropCodes.getAttr(key); if (attr == null) { Print.logError("Property not found: 0x" + StringTools.toHexString(key)); return null; } /* numeric only */ if (!attr.isNumeric() && !attr.isBoolean()) { Print.logError("Property is not numeric: 0x" + StringTools.toHexString(key)); return null; } /* proper array length? */ int arrayLen = attr.getArrayLength(); if (arrayLen > value.length) { Print.logError("Invalid array length [expected " + arrayLen + "]"); return null; } /* data buffer */ int elemLen = attr.getTypeLength(); byte data[] = new byte[elemLen * arrayLen]; int ofs = 0; /* encode numeric */ for (int i = 0; i < arrayLen; i++) { long val = 0L; if (attr.isBoolean()) { val = (Math.round(value[i]) != 0L)? 1L : 0L; } else if (attr.isDecimal()) { val = Math.round(value[i] * 10.0); } else { val = Math.round(value[i]); } int n = Payload.encodeLong(data, ofs, elemLen, true, val); if (n != elemLen) { return null; // should never occur } ofs += elemLen; } /* return bytes */ return data; } // ------------------------------------------------------------------------ /* encode set-property byte array */ public static byte[] encodePropertyData(int key, byte value[]) { /* nothing to encode? */ if (value == null) { Print.logError("Null/empty value specified"); return null; } /* get property attributes */ PropCodes.Attr attr = PropCodes.getAttr(key); if (attr == null) { Print.logError("Property not found: 0x" + StringTools.toHexString(key)); return null; } /* Binary only */ if (!attr.isBinary()) { Print.logError("Property is not a byte array: 0x" + StringTools.toHexString(key)); return null; } /* data buffer */ byte vb[] = value; int vbLen = (vb.length <= 253)? vb.length : 253; byte data[] = new byte[vbLen]; int ofs = 0; /* encode string */ System.arraycopy(vb, 0, data, ofs, vbLen); /* return bytes */ return data; } // ------------------------------------------------------------------------ /* encode set-property String */ public static byte[] encodePropertyData(int key, String value) { /* nothing to encode? */ if (value == null) { Print.logError("Null value specified"); return null; } /* get property attributes */ PropCodes.Attr attr = PropCodes.getAttr(key); if (attr == null) { Print.logError("Property not found: 0x" + StringTools.toHexString(key)); return null; } /* String only */ if (!attr.isString()) { Print.logError("Property is not a String: 0x" + StringTools.toHexString(key)); return null; } /* data buffer */ byte vs[] = StringTools.getBytes(value); int vsLen = (vs.length <= 253)? vs.length : 253; byte data[] = new byte[vsLen]; int ofs = 0; /* encode string */ System.arraycopy(vs, 0, data, ofs, vsLen); /* return bytes */ return data; } // ------------------------------------------------------------------------ /* encode set-property GeoPoint */ public static byte[] encodePropertyData(int key, GeoPoint value) { return _encodePropertyData(key, value, true); } /* encode set-property GeoPoint */ private static byte[] _encodePropertyData(int key, GeoPoint value, boolean hiRes) { /* nothing to encode? */ if (value == null) { Print.logError("Null value specified"); return null; } /* get property attributes */ PropCodes.Attr attr = PropCodes.getAttr(key); if (attr == null) { Print.logError("Property not found: 0x" + StringTools.toHexString(key)); return null; } /* GeoPoint only */ if (!attr.isGPS()) { Print.logError("Property is not a GPS point: 0x" + StringTools.toHexString(key)); return null; } /* data buffer */ byte data[] = new byte[hiRes? GeoPoint.ENCODE_HIRES_LEN : GeoPoint.ENCODE_LORES_LEN]; /* encode GeoPoint */ return GeoPoint.encodeGeoPoint(value, data, 0, data.length); } // ------------------------------------------------------------------------}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -