diff options
author | UbitUmarov | 2015-11-10 14:29:13 +0000 |
---|---|---|
committer | UbitUmarov | 2015-11-10 14:29:13 +0000 |
commit | ae9d40c7d044670f59014ca37b3fc042167eae14 (patch) | |
tree | 662249b55efd9c5ae6b296a6823920ede4d5d04c /OpenSim | |
parent | fix: set Normalized55FPS default to TRUE (diff) | |
download | opensim-SC-ae9d40c7d044670f59014ca37b3fc042167eae14.zip opensim-SC-ae9d40c7d044670f59014ca37b3fc042167eae14.tar.gz opensim-SC-ae9d40c7d044670f59014ca37b3fc042167eae14.tar.bz2 opensim-SC-ae9d40c7d044670f59014ca37b3fc042167eae14.tar.xz |
add a StringToBytes variant that takes Maximum lenght as argument.
Diffstat (limited to 'OpenSim')
-rw-r--r-- | OpenSim/Framework/Util.cs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 3e6d8ef..4dafaef 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -2022,6 +2022,56 @@ namespace OpenSim.Framework | |||
2022 | } | 2022 | } |
2023 | 2023 | ||
2024 | /// <summary> | 2024 | /// <summary> |
2025 | /// Convert a string to a byte format suitable for transport in an LLUDP packet. The output is truncated to MaxLength bytes if necessary. | ||
2026 | /// </summary> | ||
2027 | /// <param name="str"> | ||
2028 | /// If null or empty, then an bytes[0] is returned. | ||
2029 | /// Using "\0" will return a conversion of the null character to a byte. This is not the same as bytes[0] | ||
2030 | /// </param> | ||
2031 | /// <param name="args"> | ||
2032 | /// Arguments to substitute into the string via the {} mechanism. | ||
2033 | /// </param> | ||
2034 | /// <returns></returns> | ||
2035 | public static byte[] StringToBytes(string str, int MaxLength, params object[] args) | ||
2036 | { | ||
2037 | return StringToBytes1024(string.Format(str, args), MaxLength); | ||
2038 | } | ||
2039 | |||
2040 | /// <summary> | ||
2041 | /// Convert a string to a byte format suitable for transport in an LLUDP packet. The output is truncated to MaxLength bytes if necessary. | ||
2042 | /// </summary> | ||
2043 | /// <param name="str"> | ||
2044 | /// If null or empty, then an bytes[0] is returned. | ||
2045 | /// Using "\0" will return a conversion of the null character to a byte. This is not the same as bytes[0] | ||
2046 | /// </param> | ||
2047 | /// <returns></returns> | ||
2048 | public static byte[] StringToBytes(string str, int MaxLength) | ||
2049 | { | ||
2050 | if (String.IsNullOrEmpty(str)) | ||
2051 | return Utils.EmptyBytes; | ||
2052 | |||
2053 | if (!str.EndsWith("\0")) | ||
2054 | str += "\0"; | ||
2055 | |||
2056 | // Because this is UTF-8 encoding and not ASCII, it's possible we | ||
2057 | // might have gotten an oversized array even after the string trim | ||
2058 | byte[] data = UTF8.GetBytes(str); | ||
2059 | |||
2060 | if (data.Length > MaxLength) | ||
2061 | { | ||
2062 | int cut = MaxLength -1 ; | ||
2063 | if((data[cut] & 0x80 ) != 0 ) | ||
2064 | { | ||
2065 | while(cut > 0 && (data[cut] & 0xc0) != 0xc0) | ||
2066 | cut--; | ||
2067 | } | ||
2068 | Array.Resize<byte>(ref data, cut + 1); | ||
2069 | data[cut] = 0; | ||
2070 | } | ||
2071 | |||
2072 | return data; | ||
2073 | } | ||
2074 | /// <summary> | ||
2025 | /// Pretty format the hashtable contents to a single line. | 2075 | /// Pretty format the hashtable contents to a single line. |
2026 | /// </summary> | 2076 | /// </summary> |
2027 | /// <remarks> | 2077 | /// <remarks> |