⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formatters.java

📁 卡耐基.梅隆大学的机器人仿真软件(Redhat linux 9下安装)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      if (!arrayObject.getClass().isArray()) {	throw new Exception(invalidArrayFormat(dataStruct, dStart));      } else {	arrayTransferToBuffer(arrayObject, buffer, 2, 			      formatFormatArrayMax(formatArray)-1,			      isSimpleType(nextFormat),			      formatArray, nextFormat, null);      }      break;    }    case VarArrayFMT: {      int formatArray = formatFormatArray(format);      int nextFormat = formatFormatArrayItem(formatArray, 1);      Object arrayObject = (isTopLevelStruct ? dataStruct :			    primFmttrs.getObjectField(dataStruct, dStart));      /* For the size of the array */      primFmttrs.formatPutInt(buffer, varArrayNumElements(formatArray,							  dataStruct));            if (!arrayObject.getClass().isArray()) {	throw new Exception(invalidArrayFormat(dataStruct, dStart));      } else {	arrayTransferToBuffer(arrayObject, buffer, 2, 			      formatFormatArrayMax(formatArray)-1,			      isSimpleType(nextFormat),			      formatArray, nextFormat, dataStruct);      }      break;    }    case NamedFMT:      transferToBuffer(findNamedFormat(format), dataStruct, dStart, buffer,		       parentFormat, isTopLevelStruct);      break;    case EnumFMT:      primFmttrs.Encode(primFmttrs.INT_FMT, dataStruct, dStart, buffer);      break;    }  }  private static void transferToDataStructure (int format, Object dataStruct, 					       int dStart, int buffer,					       int parentFormat, 					       boolean isTopLevelStruct)     throws Exception {    Object struct;    switch (formatType(format)) {    case LengthFMT:      throw new Exception("JAVA version of IPC can only use explicit formats");    case PrimitiveFMT:      primFmttrs.Decode(formatPrimitiveProc(format), dataStruct, dStart,			buffer);      break;    case PointerFMT: {      char c = primFmttrs.formatGetChar(buffer);      if (c == '\0') {	primFmttrs.setObjectField(dataStruct, dStart, null);      } else {	transferToDataStructure(formatChoosePtrFormat(format, parentFormat),				dataStruct, dStart, buffer, 0,				isTopLevelStruct);      }      break;    }    case StructFMT: {      int formatArray = formatFormatArray(format);      int i, structStart = 0, n = formatFormatArrayMax(formatArray);      if (isTopLevelStruct) {	struct = dataStruct;      } else {	Object struct1 = primFmttrs.getObjectField(dataStruct, dStart);	struct = validateObject(struct1, dataStruct, dStart);	if (struct != struct1)	  primFmttrs.setObjectField(dataStruct, dStart, struct);      }      if (struct.getClass().getFields().length < n-1) {	throw new Exception(invalidStructFormat(struct, n));      }      for (i=1; i<n; i++) {	transferToDataStructure(formatFormatArrayItem(formatArray, i),				struct, structStart, buffer, format, false);	structStart++;      }      break;    }    case FixedArrayFMT: {      int formatArray = formatFormatArray(format);      int nextFormat = formatFormatArrayItem(formatArray, 1);      int size = formatFormatArrayItem(formatArray, 2);      Object arrayObject;      if (isTopLevelStruct) {	arrayObject = dataStruct;      } else {	arrayObject = primFmttrs.getObjectField(dataStruct, dStart);	arrayObject = validateArrayObject(arrayObject, size,					  dataStruct, dStart);	primFmttrs.setObjectField(dataStruct, dStart, arrayObject);      }      if (!arrayObject.getClass().isArray()) {	throw new Exception(invalidArrayFormat(dataStruct, dStart));      } else {	arrayTransferToDataStructure(arrayObject, buffer, 2, 				     formatFormatArrayMax(formatArray)-1,				     size, isSimpleType(nextFormat),				     formatArray, nextFormat, null);      }      break;    }    case VarArrayFMT: {      int formatArray = formatFormatArray(format);      int nextFormat = formatFormatArrayItem(formatArray, 1);      /* The total size of the array is the stored first */      int size = primFmttrs.formatGetInt(buffer);      int numDims = formatFormatArrayMax(formatArray)-2;      Object arrayObject;      if (numDims > 1) size = varArrayDimSize(2, formatArray, dataStruct);      if (!feasibleToDecodeVarArray(size, formatArray, dStart)) {	throw new Exception("JAVA version of IPC cannot decode "+			    "multi-dimensional variable length arrays unless "+			    "the size variables appear BEFORE the array "+			    "in the enclosing structure");      } else if (isTopLevelStruct) {	  arrayObject = dataStruct;      } else {	arrayObject = primFmttrs.getObjectField(dataStruct, dStart);	arrayObject = validateArrayObject(arrayObject, size,					  dataStruct, dStart);	primFmttrs.setObjectField(dataStruct, dStart, arrayObject);      }      if (!arrayObject.getClass().isArray()) {	throw new Exception(invalidArrayFormat(dataStruct, dStart));      } else {	arrayTransferToDataStructure(arrayObject, buffer, 2,				     numDims+1, size, isSimpleType(nextFormat),				     formatArray, nextFormat, dataStruct);      }      break;    }    case NamedFMT:      transferToDataStructure(findNamedFormat(format), dataStruct, dStart,			      buffer, parentFormat, isTopLevelStruct);      break;    case EnumFMT:      primFmttrs.Decode(primFmttrs.INT_FMT, dataStruct, dStart, buffer);      break;    }  }  private static void encodeData (int formatter, Object object, int buffer)      throws Exception {    transferToBuffer(formatter, object, 0, buffer, 0, true);  }  private static void decodeData (int formatter, int buffer, Object object)     throws Exception {    transferToDataStructure(formatter, object, 0, buffer, 0, true);  }  public static void checkDataClass (int format, Class oclass)      throws Exception {   switch (formatType(format)) {    case LengthFMT:      throw new Exception("JAVA version of IPC can only use explicit formats");    case PrimitiveFMT:      boolean matches = true;      String neededType = null;      switch (formatPrimitiveProc(format)) {      case primFmttrs.INT_FMT:      case primFmttrs.UINT_FMT:	neededType = "int";	matches = (oclass == int.class || oclass == Integer.class); break;      case primFmttrs.BOOLEAN_FMT:	neededType = "boolean";	matches = (oclass == boolean.class || oclass == Boolean.class); break;      case primFmttrs.FLOAT_FMT:	neededType = "float";	matches = (oclass == float.class || oclass == Float.class); break;      case primFmttrs.DOUBLE_FMT:	neededType = "double";	matches = (oclass == double.class || oclass == Double.class); break;      case primFmttrs.BYTE_FMT:      case primFmttrs.UBYTE_FMT:	neededType = "byte";	matches = (oclass == byte.class || oclass == Byte.class); break;      case primFmttrs.STR_FMT:	neededType = "String";	matches = (oclass == String.class); break;      case primFmttrs.CHAR_FMT:	neededType = "char";	matches = (oclass == char.class); break;      case primFmttrs.SHORT_FMT:      case primFmttrs.USHORT_FMT:	neededType = "short";	matches = (oclass == short.class || oclass == Short.class); break;      case primFmttrs.LONG_FMT:      case primFmttrs.ULONG_FMT:	neededType = "long";	matches = (oclass == long.class || oclass == Long.class); break;      }      if (!matches) {	throw new Exception("\""+ oclass.getName()			    +"\" does not match format -- needs to be "+			    neededType);      }      break;    case PointerFMT: {      checkDataClass(formatChoosePtrFormat(format, 0), oclass);      break;    }    case StructFMT: {      int n = formatFormatArrayMax(formatFormatArray(format));      if (oclass.getFields().length < n-1) {	throw new Exception(invalidStructFormat(oclass, n));      }      break;    }    case VarArrayFMT:    case FixedArrayFMT: {      if (!oclass.isArray()) {	throw new Exception("Data structure \""+ oclass.getName()			    +"\" does not match format -- needs to be array");      }      break;    }    case NamedFMT:      checkDataClass(findNamedFormat(format), oclass);      break;    case EnumFMT:      if (!(oclass == int.class || oclass == Integer.class)) {	throw new Exception("\""+ oclass.getName() +"\" does not match format"			    +" -- needs to be int (enum)");      }      break;    }  }  /* Marshalls the object into a byte array.     Fills in the VARCONTENT structure with the length and byteArray.     "formatter" is a C pointer. Returns any error conditions. */  public static int marshall (int formatter, Object object, 			      VARCONTENT varcontent) throws Exception {    if (!checkMarshallStatus(formatter)) {      return IPC.IPC_Error;    } else {      varcontent.length = bufferSize(formatter, object, true);      varcontent.byteArray = 0;      if (varcontent.length > 0) {	varcontent.byteArray = createByteArray(varcontent.length);	int buffer = createBuffer(varcontent.byteArray);	encodeData(formatter, object, buffer);	if (bufferLength(buffer) != varcontent.length)	  throw new Exception("Mismatch between buffer size ("+			      varcontent.length+") and encoded data ("+			      bufferLength(buffer)+")");	freeBuffer(buffer);      }      return IPC.IPC_OK;    }  }  /* Fills in the slots of the object according to the formatter.     "byteArray" and "formatter" are both C pointers.     Returns any error conditions. */  public static int unmarshall (int formatter, int byteArray, Object object)       throws Exception {    if (!checkMarshallStatus(formatter)) {      return IPC.IPC_Error;    } else {      if (formatter != 0) {	int buffer = createBuffer(byteArray);	decodeData(formatter, buffer, object);	freeBuffer(buffer);      }      return IPC.IPC_OK;    }  }  public static Object createFixedArray(Class arrayClass, int formatter) {    int size = formatFormatArrayItem(formatFormatArray(formatter), 2);    return Array.newInstance(arrayClass, size);  }  public static class IPCPrim {    public Object coerce () { return null; }    public String toString() { return "IPCPrim"; }  }  public static class IPCChar extends IPCPrim {    public IPCChar () {}    public IPCChar (char theChar) { value = theChar; }    public Object coerce () { return this; }    public String toString() { return new String() + value; }    public char value;  }  public static class IPCBoolean extends IPCPrim {    public IPCBoolean () {}    public IPCBoolean (boolean theBoolean) { value = theBoolean; }    public Object coerce () { return new Boolean(value); }    public String toString() { return (value ? "true" : "false"); }    public boolean value;  }  public static class IPCByte extends IPCPrim {    public IPCByte () {}    public IPCByte (byte theByte) { value = theByte; }    public Object coerce () { return new Byte(value); }    public String toString() { return Byte.toString(value); }    public byte value;  }  public static class IPCShort extends IPCPrim {    public IPCShort () {}    public IPCShort (short theShort) { value = theShort; }    public Object coerce () { return new Short(value); }    public String toString() { return Short.toString(value); }    public short value;  }  public static class IPCInteger extends IPCPrim {    public IPCInteger () {}    public IPCInteger (int theInt) { value = theInt; }    public Object coerce () { return new Integer(value); }    public String toString() { return Integer.toString(value); }    public int value;  }  public static class IPCLong extends IPCPrim {    public IPCLong () {}    public IPCLong (long theLong) { value = theLong; }    public Object coerce () { return new Long(value); }    public String toString() { return Long.toString(value); }    public long value;  }  public static class IPCFloat extends IPCPrim {    public IPCFloat () {}    public IPCFloat (float theFloat) { value = theFloat; }    public Object coerce () { return new Float(value); }    public String toString() { return Float.toString(value); }    public float value;  }  public static class IPCDouble extends IPCPrim {    public IPCDouble () {}    public IPCDouble (double theDouble) { value = theDouble; }    public Object coerce () { return new Double(value); }    public String toString() { return Double.toString(value); }    public double value;  }  public static class IPCString extends IPCPrim {    public IPCString () {}    public IPCString (String theString) { value = theString; }    public Object coerce () { return value; }    public String toString() { return value; }    public String value;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -