/// <summary> Get blob contents as byte array /// /// </summary> /// <param name="blob">contents including blob prefix /// </param> /// <returns> byte array according to content type in blob prefix /// </returns> public static byte[] getBytes(String str) { byte[] bytes = null; String data = removeBlobPrefix(str); try { Encoding encoding = ISO_8859_1_Encoding.getInstance(); bytes = encoding.GetBytes(data); } catch (Exception) { bytes = null; } return(bytes); }
/// <param name="ContentType"> /// </param> /// <returns> /// </returns> public static Encoding getEncodingFromContentType(char ContentType) { Encoding encoding; if (ContentType == CONTENT_TYPE_UNICODE) { encoding = Encoding.Unicode; } else if (ContentType == CONTENT_TYPE_ANSI) { encoding = Manager.Environment.GetEncoding(); } else { encoding = ISO_8859_1_Encoding.getInstance(); } return(encoding); }
/// <summary> Create a blob of the specified content type from the byte array /// /// </summary> /// <param name="Bytes"> /// </param> /// <param name="contentType"> /// </param> /// <returns> string /// </returns> public static String createFromBytes(byte[] bytes, char contentType) { String blobStr = ""; String blobPrefix; String blobData; blobPrefix = BlobType.getBlobPrefixForContentType(contentType); try { Encoding encoding = ISO_8859_1_Encoding.getInstance(); blobData = encoding.GetString(bytes, 0, bytes.Length); } catch (Exception) { blobData = null; } blobStr = blobPrefix + blobData; return(blobStr); }
/// <summary> /// Translate value of the spanned field to actual value. Field value will be in the format of length and data /// [ (Length | data) ((Length | data)).....] /// This function will read length and data of each packet and return actual value. /// </summary> /// <param name="fldsVal"></param> /// <param name="firstSegLen"></param> /// <param name="idx"></param> /// <param name="type"></param> /// <param name="result"></param> /// <param name="useHex"></param> /// <param name="noOfPackets"> Total number of packets sent from the server</param> /// <returns></returns> public static int getSpannedField(byte[] fldsVal, int firstSegLen, int idx, StorageAttribute type, StringBuilder result, bool useHex, short noOfPackets) { int endIdx = idx + firstSegLen; int len; int begin = idx; //char asciiCode; String tmp; StringBuilder suffixBuf = null; int parsedLen; Encoding tmpEnc; if (UtilStrByteMode.isLocaleDefLangDBCS()) { tmpEnc = ISO_8859_1_Encoding.getInstance(); } else { tmpEnc = Manager.Environment.GetEncoding(); } if (endIdx > fldsVal.Length) { throw new ApplicationException("in Record.getSpannedField() data string too short:\n" + fldsVal); } // append first segment result.Remove(0, result.Length); result.Append(tmpEnc.GetString(fldsVal, idx, firstSegLen)); noOfPackets--; if (noOfPackets <= 0) { return(0); } idx += firstSegLen; // next 4 characters are the length of the string (hex number) of special bytes. endIdx = idx + 4; tmp = tmpEnc.GetString(fldsVal, idx, 4); len = Convert.ToInt32(tmp, 16); idx = endIdx; if (endIdx > fldsVal.Length) { throw new ApplicationException("in Record.getSpannedField() data string too short:\n" + fldsVal); } // next 4 chars are the length of next segment endIdx = idx + 4; tmp = tmpEnc.GetString(fldsVal, idx, 4); len = Convert.ToInt32(tmp, 16); idx = endIdx; if ((len & 0x8000) > 0) { // Oops, next segment may also be spanned suffixBuf = new StringBuilder(); parsedLen = getSpannedField(fldsVal, len, idx, type, suffixBuf, useHex, noOfPackets); //after using recursive function teh suffixBuf is in the right transformed form result.Append(suffixBuf.ToString()); idx += parsedLen; } else { // next segment isn't spanned. It must be the last one. endIdx = idx + len; if (endIdx > fldsVal.Length) { throw new ApplicationException("in Record.fillFieldsData() data string too short:\n" + fldsVal); } result.Append(tmpEnc.GetString(fldsVal, idx, len)); idx = endIdx; } return(idx - begin); }
/// <summary> /// /// </summary> /// <param name="itemVal"></param> /// <param name="itemAttr"></param> /// <param name="itemLen"></param> /// <param name="offset"></param> /// <returns></returns> public static Object deSerializeItemVal(byte[] itemVal, StorageAttribute itemAttr, int itemLen, FldStorage fieldStorage, ref int offset) { Object val = null; int count = 0; int endIdx = 0; int len = 0; string tmp = string.Empty; StringBuilder suffixBuf; string tempVal = string.Empty; short noOfPackets = 0; switch (itemAttr) { case StorageAttribute.ALPHA: count = 4; endIdx = offset + count; tmp = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); len = Convert.ToInt32(tmp, 16); count = len; offset = endIdx; val = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); break; case StorageAttribute.UNICODE: count = 4; endIdx = offset + count; tmp = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); len = Convert.ToInt32(tmp, 16); count = len * 2; offset = endIdx; val = Encoding.Unicode.GetString(itemVal, offset, count); break; case StorageAttribute.BOOLEAN: count = 4; endIdx = offset + count; tmp = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); len = Convert.ToInt32(tmp, 16); count = len; offset = endIdx; val = BitConverter.ToInt16(Manager.Environment.GetEncoding().GetBytes(Manager.Environment.GetEncoding().GetString(itemVal, offset, count)), 0); break; case StorageAttribute.BLOB: count = 2; endIdx = offset + count; tmp = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); noOfPackets = BitConverter.ToInt16(Manager.Environment.GetEncoding().GetBytes(Manager.Environment.GetEncoding().GetString(itemVal, offset, count)), 0); offset = endIdx; count = 4; endIdx = offset + count; tmp = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); len = Convert.ToInt32(tmp, 16); count = len; offset = endIdx; if ((len & 0x8000) > 0) { suffixBuf = new StringBuilder(); count = getSpannedField(itemVal, len, offset, itemAttr, suffixBuf, false, noOfPackets); tempVal = suffixBuf.ToString(); } else { if (UtilStrByteMode.isLocaleDefLangDBCS()) { tempVal = ISO_8859_1_Encoding.getInstance().GetString(itemVal, offset, count); } else { tempVal = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); } } switch (fieldStorage) { case FldStorage.AnsiBlob: case FldStorage.UnicodeBlob: val = BlobType.getString(tempVal); break; case FldStorage.Blob: if (UtilStrByteMode.isLocaleDefLangDBCS()) { val = ISO_8859_1_Encoding.getInstance().GetBytes(BlobType.removeBlobPrefix(tempVal)); } else { val = Manager.Environment.GetEncoding().GetBytes(BlobType.removeBlobPrefix(tempVal)); } break; } break; case StorageAttribute.NUMERIC: { count = 4; endIdx = offset + count; tmp = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); len = Convert.ToInt32(tmp, 16); count = len; offset = endIdx; switch (fieldStorage) { case FldStorage.NumericFloat: case FldStorage.NumericSigned: byte[] array = new byte[count]; Array.Copy(itemVal, offset, array, 0, array.Length); if (fieldStorage == FldStorage.NumericFloat) { val = BitConverter.ToDouble(array, 0); } else { val = BitConverter.ToInt32(array, 0); } break; case FldStorage.NumericPackedDec: case FldStorage.NumericString: val = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); break; } } break; case StorageAttribute.TIME: case StorageAttribute.DATE: count = 4; endIdx = offset + count; tmp = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); len = Convert.ToInt32(tmp, 16); count = len; offset = endIdx; switch (fieldStorage) { case FldStorage.TimeInteger: case FldStorage.DateInteger: val = BitConverter.ToInt32(Manager.Environment.GetEncoding().GetBytes(Manager.Environment.GetEncoding().GetString(itemVal, offset, count)), 0); break; case FldStorage.TimeString: case FldStorage.DateString: val = Manager.Environment.GetEncoding().GetString(itemVal, offset, count); break; } break; } offset = offset + count; return(val); }
/// <summary> /// /// </summary> /// <param name="itemVal"></param> /// <param name="itemAttr"></param> /// <returns></returns> public static byte[] serializeItemVal(String itemVal, StorageAttribute itemAttr) { Debug.Assert(itemVal != null); string valueSize; string tmpBufLen = string.Empty; Byte [] tmpBuf = null; List <byte> contentWithLength = new List <byte>(); int pos = 0; int fldValLen = 0; String tempItemVal = string.Empty; int noOfPackets = 0; Byte [] tmpNoOfPackets = null; String tmpStrNoOfPackets = string.Empty; switch (itemAttr) { case StorageAttribute.NUMERIC: case StorageAttribute.DATE: case StorageAttribute.TIME: NUM_TYPE numType = new NUM_TYPE(itemVal); tmpBuf = Misc.ToByteArray(numType.Data); break; case StorageAttribute.BOOLEAN: tmpBuf = Manager.Environment.GetEncoding().GetBytes(itemVal); break; case StorageAttribute.ALPHA: itemVal = StrUtil.rtrim(itemVal); valueSize = (Convert.ToString(UtilStrByteMode.lenB(itemVal), 16)).ToUpper(); // add leading zeros (if needed) for (int j = 0; j < 4 - valueSize.Length; j++) { tmpBufLen += "0"; } tmpBufLen += valueSize; contentWithLength.AddRange(Manager.Environment.GetEncoding().GetBytes(tmpBufLen)); contentWithLength.AddRange(Manager.Environment.GetEncoding().GetBytes(itemVal)); tmpBuf = new byte[contentWithLength.Count]; contentWithLength.CopyTo(tmpBuf); break; case StorageAttribute.UNICODE: itemVal = StrUtil.rtrim(itemVal); valueSize = (Convert.ToString(itemVal.Length, 16)).ToUpper(); // add leading zeros (if needed) for (int j = 0; j < 4 - valueSize.Length; j++) { tmpBufLen += "0"; } tmpBufLen += valueSize; contentWithLength.AddRange(Manager.Environment.GetEncoding().GetBytes(tmpBufLen)); contentWithLength.AddRange(Encoding.Unicode.GetBytes(itemVal)); tmpBuf = new byte[contentWithLength.Count]; contentWithLength.CopyTo(tmpBuf); break; case StorageAttribute.BLOB: pos = 0; // blob will be serialized in packet of size 0xFFFF. // So format of serialized buffer for blob is // no. of packets (n) + length1 + data1 + length2 + data2 + ......length n + datan fldValLen = ISO_8859_1_Encoding.getInstance().GetByteCount(itemVal); noOfPackets = (int)fldValLen / 0xFFFF; tmpBufLen = "FFFF"; tmpNoOfPackets = Manager.Environment.GetEncoding().GetBytes(tmpBufLen); for (int i = 0; i < noOfPackets; i++) { tempItemVal = itemVal.Substring(pos, 0xFFFF); pos += 0xFFFF; contentWithLength.AddRange(tmpNoOfPackets); contentWithLength.AddRange(ISO_8859_1_Encoding.getInstance().GetBytes(tempItemVal)); } int lastPacketSize = fldValLen % 0xFFFF; if (lastPacketSize > 0) { tempItemVal = itemVal.Substring(pos, (fldValLen) - (pos)); byte[] tempItemValBytes = ISO_8859_1_Encoding.getInstance().GetBytes(tempItemVal); tmpBufLen = tempItemValBytes.Length.ToString("X4"); contentWithLength.AddRange(Manager.Environment.GetEncoding().GetBytes(tmpBufLen)); contentWithLength.AddRange(ISO_8859_1_Encoding.getInstance().GetBytes(tempItemVal)); noOfPackets++; } tmpStrNoOfPackets = noOfPackets.ToString("D4"); tmpNoOfPackets = Manager.Environment.GetEncoding().GetBytes(tmpStrNoOfPackets); tmpBuf = new byte[contentWithLength.Count + tmpNoOfPackets.Length]; tmpNoOfPackets.CopyTo(tmpBuf, 0); contentWithLength.CopyTo(tmpBuf, tmpNoOfPackets.Length); break; } //end of the type case block return(tmpBuf); }