public packString ( int offs, string s ) : int | ||
offs | int | |
s | string | |
Результат | int |
internal int packObject(XMLElement objElem, ClassDescriptor desc, int offs, ByteBuffer buf) { ClassDescriptor.FieldDescriptor[] flds = desc.allFields; for (int i = 0, n = flds.Length; i < n; i++) { ClassDescriptor.FieldDescriptor fd = flds[i]; FieldInfo f = fd.field; String fieldName = fd.fieldName; XMLElement elem = (objElem != null) ? objElem.getSibling(fieldName) : null; switch (fd.type) { case ClassDescriptor.FieldType.tpByte: case ClassDescriptor.FieldType.tpSByte: buf.extend(offs + 1); if (elem != null) { if (elem.isIntValue()) { buf.arr[offs] = (byte)elem.IntValue; } else if (elem.isRealValue()) { buf.arr[offs] = (byte)elem.RealValue; } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 1; continue; case ClassDescriptor.FieldType.tpBoolean: buf.extend(offs + 1); if (elem != null) { if (elem.isIntValue()) { buf.arr[offs] = (byte)(elem.IntValue != 0 ? 1 : 0); } else if (elem.isRealValue()) { buf.arr[offs] = (byte)(elem.RealValue != 0.0 ? 1 : 0); } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 1; continue; case ClassDescriptor.FieldType.tpShort: case ClassDescriptor.FieldType.tpUShort: case ClassDescriptor.FieldType.tpChar: buf.extend(offs + 2); if (elem != null) { if (elem.isIntValue()) { Bytes.pack2(buf.arr, offs, (short)elem.IntValue); } else if (elem.isRealValue()) { Bytes.pack2(buf.arr, offs, (short)elem.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 2; continue; case ClassDescriptor.FieldType.tpEnum: buf.extend(offs + 4); if (elem != null) { if (elem.isIntValue()) { Bytes.pack4(buf.arr, offs, (int)elem.IntValue); } else if (elem.isRealValue()) { Bytes.pack4(buf.arr, offs, (int)elem.RealValue); } else if (elem.isStringValue()) { try { #if CF Bytes.pack4(buf.arr, offs, (int)ClassDescriptor.parseEnum(f.FieldType, elem.StringValue)); #else Bytes.pack4(buf.arr, offs, (int)Enum.Parse(f.FieldType, elem.StringValue)); #endif } catch (ArgumentException) { throwException("Invalid enum value"); } } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 4; continue; case ClassDescriptor.FieldType.tpInt: case ClassDescriptor.FieldType.tpUInt: buf.extend(offs + 4); if (elem != null) { if (elem.isIntValue()) { Bytes.pack4(buf.arr, offs, (int)elem.IntValue); } else if (elem.isRealValue()) { Bytes.pack4(buf.arr, offs, (int)elem.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 4; continue; case ClassDescriptor.FieldType.tpLong: case ClassDescriptor.FieldType.tpULong: buf.extend(offs + 8); if (elem != null) { if (elem.isIntValue()) { Bytes.pack8(buf.arr, offs, elem.IntValue); } else if (elem.isRealValue()) { Bytes.pack8(buf.arr, offs, (long)elem.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 8; continue; case ClassDescriptor.FieldType.tpFloat: buf.extend(offs + 4); if (elem != null) { if (elem.isIntValue()) { Bytes.packF4(buf.arr, offs, (float)elem.IntValue); } else if (elem.isRealValue()) { Bytes.packF4(buf.arr, offs, (float)elem.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 4; continue; case ClassDescriptor.FieldType.tpDouble: buf.extend(offs + 8); if (elem != null) { if (elem.isIntValue()) { Bytes.packF8(buf.arr, offs, (double)elem.IntValue); } else if (elem.isRealValue()) { Bytes.packF8(buf.arr, offs, elem.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 8; continue; case ClassDescriptor.FieldType.tpDecimal: buf.extend(offs + 16); if (elem != null) { decimal d = 0; if (elem.isIntValue()) { d = elem.IntValue; } else if (elem.isRealValue()) { d = (decimal)elem.RealValue; } else if (elem.isStringValue()) { try { d = Decimal.Parse(elem.StringValue); } catch (FormatException) { throwException("Invalid date"); } } else { throwException("Conversion for field " + fieldName + " is not possible"); } Bytes.packDecimal(buf.arr, offs, d); } offs += 16; continue; case ClassDescriptor.FieldType.tpGuid: buf.extend(offs + 16); if (elem != null) { if (elem.isStringValue()) { Guid guid = new Guid(elem.StringValue); byte[] bits = guid.ToByteArray(); Array.Copy(bits, 0, buf.arr, offs, 16); } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 16; continue; case ClassDescriptor.FieldType.tpDate: buf.extend(offs + 8); if (elem != null) { if (elem.isIntValue()) { Bytes.pack8(buf.arr, offs, elem.IntValue); } else if (elem.isNullValue()) { Bytes.pack8(buf.arr, offs, -1); } else if (elem.isStringValue()) { try { Bytes.packDate(buf.arr, offs, DateTime.Parse(elem.StringValue)); } catch (FormatException) { throwException("Invalid date"); } } else { throwException("Conversion for field " + fieldName + " is not possible"); } } offs += 8; continue; case ClassDescriptor.FieldType.tpString: if (elem != null) { System.String val = null; if (elem.isIntValue()) { val = System.Convert.ToString(elem.IntValue); } else if (elem.isRealValue()) { val = elem.RealValue.ToString(); } else if (elem.isStringValue()) { val = elem.StringValue; } else if (elem.isNullValue()) { val = null; } else { throwException("Conversion for field " + fieldName + " is not possible"); } offs = buf.packString(offs, val); continue; } buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; continue; case ClassDescriptor.FieldType.tpOid: case ClassDescriptor.FieldType.tpObject: { int oid = 0; if (elem != null) { XMLElement refElem = elem.getSibling("ref"); if (refElem == null) { throwException("<ref> element expected"); } oid = mapId(getIntAttribute(refElem, "id")); } buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, oid); offs += 4; continue; } case ClassDescriptor.FieldType.tpValue: offs = packObject(elem, fd.valueDesc, offs, buf); continue; case ClassDescriptor.FieldType.tpRaw: case ClassDescriptor.FieldType.tpArrayOfByte: case ClassDescriptor.FieldType.tpArrayOfSByte: offs = importBinary(elem, offs, buf, fieldName); continue; case ClassDescriptor.FieldType.tpArrayOfBoolean: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isIntValue()) { buf.arr[offs] = (byte)(item.IntValue != 0 ? 1 : 0); } else if (item.isRealValue()) { buf.arr[offs] = (byte)(item.RealValue != 0.0 ? 1 : 0); } else { throwException("Conversion for field " + fieldName + " is not possible"); } item = item.NextSibling; offs += 1; } } continue; case ClassDescriptor.FieldType.tpArrayOfChar: case ClassDescriptor.FieldType.tpArrayOfShort: case ClassDescriptor.FieldType.tpArrayOfUShort: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len * 2); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isIntValue()) { Bytes.pack2(buf.arr, offs, (short)item.IntValue); } else if (item.isRealValue()) { Bytes.pack2(buf.arr, offs, (short)item.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } item = item.NextSibling; offs += 2; } } continue; case ClassDescriptor.FieldType.tpArrayOfEnum: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; Type elemType = f.FieldType.GetElementType(); buf.extend(offs + 4 + len * 4); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isIntValue()) { Bytes.pack4(buf.arr, offs, (int)item.IntValue); } else if (item.isRealValue()) { Bytes.pack4(buf.arr, offs, (int)item.RealValue); } else if (item.isStringValue()) { try { #if CF Bytes.pack4(buf.arr, offs, (int)ClassDescriptor.parseEnum(elemType, item.StringValue)); #else Bytes.pack4(buf.arr, offs, (int)Enum.Parse(elemType, item.StringValue)); #endif } catch (ArgumentException) { throwException("Invalid enum value"); } } else { throwException("Conversion for field " + fieldName + " is not possible"); } item = item.NextSibling; offs += 4; } } continue; case ClassDescriptor.FieldType.tpArrayOfInt: case ClassDescriptor.FieldType.tpArrayOfUInt: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len * 4); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isIntValue()) { Bytes.pack4(buf.arr, offs, (int)item.IntValue); } else if (item.isRealValue()) { Bytes.pack4(buf.arr, offs, (int)item.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } item = item.NextSibling; offs += 4; } } continue; case ClassDescriptor.FieldType.tpArrayOfLong: case ClassDescriptor.FieldType.tpArrayOfULong: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len * 8); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isIntValue()) { Bytes.pack8(buf.arr, offs, item.IntValue); } else if (item.isRealValue()) { Bytes.pack8(buf.arr, offs, (long)item.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } item = item.NextSibling; offs += 8; } } continue; case ClassDescriptor.FieldType.tpArrayOfFloat: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len * 4); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isIntValue()) { Bytes.packF4(buf.arr, offs, (float)item.IntValue); } else if (item.isRealValue()) { Bytes.packF4(buf.arr, offs, (float)item.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } item = item.NextSibling; offs += 4; } } continue; case ClassDescriptor.FieldType.tpArrayOfDouble: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len * 8); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isIntValue()) { Bytes.packF8(buf.arr, offs, (double)item.IntValue); } else if (item.isRealValue()) { Bytes.packF8(buf.arr, offs, item.RealValue); } else { throwException("Conversion for field " + fieldName + " is not possible"); } item = item.NextSibling; offs += 8; } } continue; case ClassDescriptor.FieldType.tpArrayOfDate: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len * 8); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isNullValue()) { Bytes.pack8(buf.arr, offs, -1); } else if (item.isStringValue()) { try { Bytes.packDate(buf.arr, offs, DateTime.Parse(item.StringValue)); } catch (FormatException) { throwException("Conversion for field " + fieldName + " is not possible"); } } item = item.NextSibling; offs += 8; } } continue; case ClassDescriptor.FieldType.tpArrayOfDecimal: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len * 16); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isStringValue()) { try { Bytes.packDecimal(buf.arr, offs, Decimal.Parse(item.StringValue)); } catch (FormatException) { throwException("Conversion for field " + fieldName + " is not possible"); } } item = item.NextSibling; offs += 16; } } continue; case ClassDescriptor.FieldType.tpArrayOfGuid: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len * 16); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { if (item.isStringValue()) { try { Bytes.packGuid(buf.arr, offs, new Guid(item.StringValue)); } catch (FormatException) { throwException("Conversion for field " + fieldName + " is not possible"); } } item = item.NextSibling; offs += 16; } } continue; case ClassDescriptor.FieldType.tpArrayOfString: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { System.String val = null; if (item.isIntValue()) { val = System.Convert.ToString(item.IntValue); } else if (item.isRealValue()) { val = item.RealValue.ToString(); } else if (item.isStringValue()) { val = item.StringValue; } else if (item.isNullValue()) { val = null; } else { throwException("Conversion for field " + fieldName + " is not possible"); } offs = buf.packString(offs, val); item = item.NextSibling; } } continue; case ClassDescriptor.FieldType.tpArrayOfObject: case ClassDescriptor.FieldType.tpArrayOfOid: case ClassDescriptor.FieldType.tpLink: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; buf.extend(offs + 4 + len * 4); Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { XMLElement href = item.getSibling("ref"); if (href == null) { throwException("<ref> element expected"); } int oid = mapId(getIntAttribute(href, "id")); Bytes.pack4(buf.arr, offs, oid); item = item.NextSibling; offs += 4; } } continue; case ClassDescriptor.FieldType.tpArrayOfValue: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; Bytes.pack4(buf.arr, offs, len); offs += 4; ClassDescriptor elemDesc = fd.valueDesc; while (--len >= 0) { offs = packObject(item, elemDesc, offs, buf); item = item.NextSibling; } } continue; case ClassDescriptor.FieldType.tpArrayOfRaw: if (elem == null || elem.isNullValue()) { buf.extend(offs + 4); Bytes.pack4(buf.arr, offs, -1); offs += 4; } else { XMLElement item = elem.getSibling("element"); int len = (item == null) ? 0 : item.Counter; Bytes.pack4(buf.arr, offs, len); offs += 4; while (--len >= 0) { offs = importBinary(item, offs, buf, fieldName); item = item.NextSibling; } } continue; } } return offs; }
private int packKeyPart(ByteBuffer buf, int dst, ClassDescriptor.FieldType type, object val) { switch (type) { case ClassDescriptor.FieldType.tpBoolean: dst = buf.packBool(dst, (bool)val); break; case ClassDescriptor.FieldType.tpByte: dst = buf.packI1(dst, (byte)val); break; case ClassDescriptor.FieldType.tpSByte: dst = buf.packI1(dst, (sbyte)val); break; case ClassDescriptor.FieldType.tpShort: dst = buf.packI2(dst, (short)val); break; case ClassDescriptor.FieldType.tpUShort: dst = buf.packI2(dst, (ushort)val); break; case ClassDescriptor.FieldType.tpChar: dst = buf.packI2(dst, (char)val); break; case ClassDescriptor.FieldType.tpInt: case ClassDescriptor.FieldType.tpOid: case ClassDescriptor.FieldType.tpEnum: dst = buf.packI4(dst, (int)val); break; case ClassDescriptor.FieldType.tpUInt: dst = buf.packI4(dst, (int)(uint)val); break; case ClassDescriptor.FieldType.tpObject: dst = buf.packI4(dst, val != null ? (int)((IPersistent)val).Oid : 0); break; case ClassDescriptor.FieldType.tpLong: dst = buf.packI8(dst, (long)val); break; case ClassDescriptor.FieldType.tpULong: dst = buf.packI8(dst, (long)(ulong)val); break; case ClassDescriptor.FieldType.tpDate: dst = buf.packDate(dst, (DateTime)val); break; case ClassDescriptor.FieldType.tpFloat: dst = buf.packF4(dst, (float)val); break; case ClassDescriptor.FieldType.tpDouble: dst = buf.packF8(dst, (double)val); break; case ClassDescriptor.FieldType.tpDecimal: dst = buf.packDecimal(dst, (decimal)val); break; case ClassDescriptor.FieldType.tpGuid: dst = buf.packGuid(dst, (Guid)val); break; case ClassDescriptor.FieldType.tpString: dst = buf.packString(dst, (string)val); break; case ClassDescriptor.FieldType.tpArrayOfByte: buf.extend(dst + 4); if (val != null) { byte[] arr = (byte[])val; int len = arr.Length; Bytes.pack4(buf.arr, dst, len); dst += 4; buf.extend(dst + len); Array.Copy(arr, 0, buf.arr, dst, len); dst += len; } else { Bytes.pack4(buf.arr, dst, 0); dst += 4; } break; default: Debug.Assert(false, "Invalid type"); break; } return(dst); }
Key createCompoundKey(ClassDescriptor.FieldType[] types, String[] values) { ByteBuffer buf = new ByteBuffer(); int dst = 0; for (int i = 0; i < types.Length; i++) { String val = values[i]; switch (types[i]) { case ClassDescriptor.FieldType.tpBoolean: dst = buf.packBool(dst, Int32.Parse(val) != 0); break; case ClassDescriptor.FieldType.tpByte: case ClassDescriptor.FieldType.tpSByte: dst = buf.packI1(dst, Int32.Parse(val)); break; case ClassDescriptor.FieldType.tpChar: case ClassDescriptor.FieldType.tpShort: case ClassDescriptor.FieldType.tpUShort: dst = buf.packI2(dst, Int32.Parse(val)); break; case ClassDescriptor.FieldType.tpInt: dst = buf.packI4(dst, Int32.Parse(val)); break; case ClassDescriptor.FieldType.tpEnum: case ClassDescriptor.FieldType.tpUInt: dst = buf.packI4(dst, (int)UInt32.Parse(val)); break; case ClassDescriptor.FieldType.tpObject: case ClassDescriptor.FieldType.tpOid: dst = buf.packI4(dst, mapId((int)UInt32.Parse(val))); break; case ClassDescriptor.FieldType.tpLong: dst = buf.packI8(dst, Int64.Parse(val)); break; case ClassDescriptor.FieldType.tpULong: dst = buf.packI8(dst, (long)UInt64.Parse(val)); break; case ClassDescriptor.FieldType.tpDate: dst = buf.packDate(dst, DateTime.Parse(val)); break; case ClassDescriptor.FieldType.tpFloat: dst = buf.packF4(dst, Single.Parse(val)); break; case ClassDescriptor.FieldType.tpDouble: dst = buf.packF8(dst, Double.Parse(val)); break; case ClassDescriptor.FieldType.tpDecimal: dst = buf.packDecimal(dst, Decimal.Parse(val)); break; case ClassDescriptor.FieldType.tpGuid: dst = buf.packGuid(dst, new Guid(val)); break; case ClassDescriptor.FieldType.tpString: dst = buf.packString(dst, val); break; case ClassDescriptor.FieldType.tpArrayOfByte: buf.extend(dst + 4 + (val.Length >> 1)); Bytes.pack4(buf.arr, dst, val.Length >> 1); dst += 4; for (int j = 0, n = val.Length; j < n; j += 2) { buf.arr[dst++] = (byte)((getHexValue(val[j]) << 4) | getHexValue(val[j + 1])); } break; default: throwException("Bad key type"); break; } } return new Key(buf.toArray()); }