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

📄 formatters.java

📁 卡耐基.梅隆大学的机器人仿真软件(Redhat linux 9下安装)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************** *       $Id: formatters.java,v 1.1 2005/08/31 20:33:47 nickr Exp $ * $Revision: 1.1 $ *     $Date: 2005/08/31 20:33:47 $ *   $Author: nickr $ *    $State: Exp $ *   $Locker:  $ * * PROJECT:	NM-DS1 * * FILE:	formatters.java * * DESCRIPTION: JAVA class for marshalling and unmarshalling IPC byte arrays *              into JAVA objects.. * * $Log: formatters.java,v $ * Revision 1.1  2005/08/31 20:33:47  nickr * *** empty log message *** * * Revision 1.1  2005/08/31 20:25:31  nickr * Initial check-in of java class libraries * * Revision 1.3  2002/01/11 22:35:23  reids * Improved error handling for Java version * * Revision 1.2  2002/01/02 21:10:03  reids * Fixed implementation of decoding enumerated types. * Added debugging function (printByteArray). * * Revision 1.1  2002/01/02 17:40:17  reids * Initial (and more, or less, complete) release of Java version of IPC. * * *****************************************************************************/package IPC;import java.lang.reflect.Field;import java.lang.reflect.Array;public class formatters {  public static class VARCONTENT {    public int length;    public int byteArray;  }  public static final int PrimitiveFMT  = 0;  public static final int LengthFMT     = 1;  public static final int StructFMT     = 2;  public static final int PointerFMT    = 3;  public static final int FixedArrayFMT = 4;  public static final int VarArrayFMT   = 5;  public static final int BadFormatFMT  = 6;  public static final int NamedFMT      = 7;  public static final int EnumFMT       = 8;  private native static int formatType(int formatter);  private native static int formatPrimitiveProc(int formatter);  private native static int formatChoosePtrFormat(int formatter, 						  int parentFormat);  private native static int formatFormatArray(int formatter);  private native static int formatFormatArrayMax(int formatArray);  private native static int formatFormatArrayItem(int formatArray, int n);  private native static int findNamedFormat(int format);  private native static boolean checkMarshallStatus(int formatter);  private native static int createBuffer(int byteArray);  private native static void freeBuffer(int buffer);  private native static int bufferLength(int buffer);  private native static int createByteArray(int length);  // For debugging purposes  private native static void rewindBuffer(int buffer);  private native static void printBuffer(int buffer);  private native static void printByteArray(int byteArray, int length);  private native static int parseFormat(String format);  private static boolean isSimpleType (int format) throws Exception {    return (formatType(format) == PrimitiveFMT &&	    primFmttrs.SimpleType(formatPrimitiveProc(format)));  }  private static int fixedArrayNumElements (int formatArray) {    int i, numElements = 1;    int n = formatFormatArrayMax(formatArray);    for (i=2; i<n; i++) numElements *= formatFormatArrayItem(formatArray, i);    return numElements;  }  private static int varArrayDimSize (int dim, int formatArray,				      Object dataStruct) throws Exception {    int sizePlace = formatFormatArrayItem(formatArray, dim);    int size = primFmttrs.getIntField(dataStruct, sizePlace-1);    return size;  }  private static int varArrayNumElements (int formatArray, Object dataStruct)    throws Exception {    int i, numElements = 1;    int n = formatFormatArrayMax(formatArray);    for (i=2; i<n; i++)      numElements *= varArrayDimSize(i, formatArray, dataStruct);    return numElements;  }  /* dataStruct is non-null if this is a variable length array */  private static int arrayBufferSize (Object[] array, int dim, int max,				      int formatArray, int arrayFormat,				      Object dataStruct)     throws Exception {    int bufSize = 0;    int len = (dataStruct == null ? formatFormatArrayItem(formatArray, dim)	       : varArrayDimSize(dim, formatArray, dataStruct));    for (int i=0; i<len; i++) {      bufSize += (dim == max ? bufferSize1(arrayFormat, array[i], 0, 0, true)		  : arrayBufferSize((Object[])array[i], dim+1, max, 				    formatArray, arrayFormat, dataStruct));    }    return bufSize;  }  /* dataStruct is non-null if this is a variable length array */  private static void arrayTransferToBuffer (Object array, int buffer,					     int dim, int max, 					     boolean isSimple,					     int formatArray, int arrayFormat,					     Object dataStruct)     throws Exception {    int i, len = (dataStruct == null ? formatFormatArrayItem(formatArray, dim)		  : varArrayDimSize(dim, formatArray, dataStruct));    for (i=0; i<len; i++) {      if (dim != max) {	arrayTransferToBuffer(((Object[])array)[i], buffer, dim+1, max,			      isSimple, formatArray, arrayFormat, dataStruct);      } else if (isSimple) {	primFmttrs.EncodeElement(formatPrimitiveProc(arrayFormat), 				 array, i, buffer);      } else {	transferToBuffer(arrayFormat, ((Object[])array)[i], 0, buffer, 0, 			 true);      }    }  }  /* dataStruct is non-null if this is a variable length array */  private static void  arrayTransferToDataStructure (Object array, int buffer, int dim, int max, 				int len, boolean isSimple, int formatArray,				int arrayFormat, Object dataStruct)     throws Exception {    int i, nextLen=0;    if (dim != max)       nextLen = (dataStruct == null ? formatFormatArrayItem(formatArray, dim+1)		 : varArrayDimSize(dim+1, formatArray, dataStruct));    for (i=0; i<len; i++) {      if (dim != max) {	Object[] arrayObject = ((Object[])array);	Array.set(arrayObject, i,		  validateArrayObject(arrayObject[i], nextLen, array, -1));	arrayTransferToDataStructure(arrayObject[i], buffer, dim+1, max,				     nextLen, isSimple, formatArray,				     arrayFormat, dataStruct);      } else if (isSimple) {	primFmttrs.DecodeElement(formatPrimitiveProc(arrayFormat), 				 array, i, buffer);      } else {	Array.set(array, i, validateObject(((Object[])array)[i], array, -1));	transferToDataStructure(arrayFormat, ((Object[])array)[i], 0,				buffer, 0, true);      }    }  }  private static Object validateObject (Object object, Object parentObject,					int index) throws Exception {    if (object == null) {      Class pclass = parentObject.getClass();      Class oclass = (index < 0 ? pclass.getComponentType()		      : pclass.getFields()[index].getType());      object = oclass.newInstance();    }    return object;  }  private static String invalidStructFormat(Class oclass, int n) {    return ("Data structure \""+ oclass.getName()	    +"\" does not match format -- have "+	    oclass.getFields().length +" public fields; need " + (n-1));  }  private static String invalidStructFormat(Object object, int n) {    return invalidStructFormat(object.getClass(), n);  }  private static String invalidArrayFormat(Class oclass, int index) {    return ("Data structure \""+ oclass.getName()	    +"\" does not match format -- field \""+	    oclass.getFields()[index].getName() +"\" needs to be an array");  }  private static String invalidArrayFormat(Object object, int index) {    return invalidArrayFormat(object.getClass(), index);  }  private static Object validateArrayObject (Object arrayObject, int dim,					     Object object, int index)     throws Exception {      if (arrayObject != null && !arrayObject.getClass().isArray()) {	throw new Exception(invalidArrayFormat(object, index));      } else if (arrayObject == null || Array.getLength(arrayObject) != dim) {      Class oclass = object.getClass();      Class aclass = (index < 0 ? oclass.getComponentType()		      : oclass.getFields()[index].getType());      arrayObject = Array.newInstance(aclass.getComponentType(), dim);    }    return arrayObject;  }  private static boolean feasibleToDecodeVarArray(int size, int formatArray,						  int dStart) {    int max = formatFormatArrayMax(formatArray);    if (max > 3) { // Number of dimensions is max-2      for (int i=2; i<max; i++) {	if (formatFormatArrayItem(formatArray, i) > dStart) {	  return false;	}      }    }    return true;  }  private static int bufferSize1 (int format, Object dataStruct, 				  int dStart, int parentFormat, 				  boolean isTopLevelStruct)     throws Exception {    int bufSize = 0;    Object struct;    switch (formatType(format)) {    case LengthFMT:      throw new Exception("JAVA version of IPC can only use explicit formats");    case PrimitiveFMT:      int primType = formatPrimitiveProc(format);      bufSize += primFmttrs.ELength(primType, dataStruct, dStart);      break;    case PointerFMT:      bufSize += 1;      if (primFmttrs.getObjectField(dataStruct, dStart) != null)	bufSize += bufferSize1(formatChoosePtrFormat(format, parentFormat),			       dataStruct, dStart, 0, isTopLevelStruct);      break;    case StructFMT: {      int formatArray = formatFormatArray(format);      int i, structStart = 0, n = formatFormatArrayMax(formatArray);      struct = (isTopLevelStruct ? dataStruct		: primFmttrs.getObjectField(dataStruct, dStart));      if (struct.getClass().getFields().length < n-1) {	throw new Exception(invalidStructFormat(struct, n));      }      for (i=1; i<n; i++) {	bufSize += bufferSize1(formatFormatArrayItem(formatArray, i),			       struct, structStart, format, false);	structStart++;      }      break;    }    case FixedArrayFMT: {      int formatArray = formatFormatArray(format);      int nextFormat = formatFormatArrayItem(formatArray, 1);      Object arrayObject = (isTopLevelStruct ? dataStruct :			    primFmttrs.getObjectField(dataStruct, dStart));      if (!arrayObject.getClass().isArray()) {	throw new Exception(invalidArrayFormat(dataStruct, dStart));      } else if (isSimpleType(nextFormat)) {	bufSize += (bufferSize1(nextFormat, arrayObject, 0, 0, false) *		    fixedArrayNumElements(formatArray));      } else {	bufSize += arrayBufferSize((Object[])arrayObject, 2, 				   formatFormatArrayMax(formatArray)-1,				   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 */      bufSize += primFmttrs.ELength(primFmttrs.INT_FMT, null, 0);            if (!arrayObject.getClass().isArray()) {	throw new Exception(invalidArrayFormat(dataStruct, dStart));      } else if (isSimpleType(nextFormat)) {	bufSize += (bufferSize1(nextFormat, arrayObject, 0, 0, false) *		    varArrayNumElements(formatArray, dataStruct));      } else {	bufSize += arrayBufferSize((Object[])arrayObject, 2, 				   formatFormatArrayMax(formatArray)-1,				   formatArray, nextFormat, dataStruct);      }      break;    }    case NamedFMT:      bufSize += bufferSize1(findNamedFormat(format), dataStruct, dStart,			     parentFormat, isTopLevelStruct);      break;    case EnumFMT:      bufSize += primFmttrs.ELength(primFmttrs.INT_FMT, null, 0);      break;    }    return bufSize;  }  private static int bufferSize (int formatter, Object object,				 boolean isTopLevelStruct)       throws Exception {    int val = (formatter == 0 ? 0 : bufferSize1(formatter, object, 0, 0,						isTopLevelStruct));    return val;  }  private static void transferToBuffer (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.Encode(formatPrimitiveProc(format), dataStruct, dStart,			buffer);      break;    case PointerFMT:      Object object = primFmttrs.getObjectField(dataStruct, dStart);      /* 'Z' means data, 0 means NIL */      primFmttrs.formatPutChar(buffer, (object != null ? 'Z' : '\0'));      if (object != null)	transferToBuffer(formatChoosePtrFormat(format, parentFormat),			 dataStruct, dStart, buffer, 0, isTopLevelStruct);      break;    case StructFMT: {      int formatArray = formatFormatArray(format);      int i, structStart = 0, n = formatFormatArrayMax(formatArray);      struct = (isTopLevelStruct ? dataStruct		: primFmttrs.getObjectField(dataStruct, dStart));      if (struct.getClass().getFields().length < n-1) {	throw new Exception(invalidStructFormat(struct, n));      }      for (i=1; i<n; i++) {	transferToBuffer(formatFormatArrayItem(formatArray, i),			 struct, structStart, buffer, format, false);	structStart++;      }      break;    }    case FixedArrayFMT: {      int formatArray = formatFormatArray(format);      int nextFormat = formatFormatArrayItem(formatArray, 1);      Object arrayObject = (isTopLevelStruct ? dataStruct :			    primFmttrs.getObjectField(dataStruct, dStart));

⌨️ 快捷键说明

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