Exemple #1
0
        /// <summary> </summary>
        /// <param name="s">
        /// </param>
        /// <param name="withSize">if true, returns an array with an initial int with its size
        /// </param>
        /// <param name="totalSpace">The total space of the string (can be bigger that the real string size - to support later in place update)
        /// </param>
        /// <returns> The byte array that represent the string
        /// </returns>
        /// <throws>  UnsupportedEncodingException </throws>
        public byte[] StringToByteArray(System.String s, bool withSize, int totalSpace, bool withEncoding)
        {
            byte[] bytes = null;
            if (withEncoding && hasEncoding)
            {
                try {
                    bytes = Encoding.GetEncoding(ENCODING).GetBytes(s);
                } catch (Exception e) {
                    throw new ODBRuntimeException(NeoDatisError.UnsupportedEncoding.AddParameter(encoding));
                }
            }
            else
            {
                bytes = Encoding.ASCII.GetBytes(s);
            }


            if (!withSize)
            {
                return(bytes);
            }
            int totalSize = 0;

            if (totalSpace == -1)
            {
                // we always store a string with X the size to enable in place update for bigger string later
                totalSize = OdbConfiguration.GetStringSpaceReserveFactor() * bytes.Length + 2 * ODBType.NativeInt.GetSize();
            }
            else
            {
                totalSize = totalSpace;
            }
            byte[] totalSizeBytes = IntToByteArray(totalSize);
            byte[] stringRealSize = IntToByteArray(bytes.Length);
            byte[] bytes2         = new byte[totalSize + IntSize_x_2];
            for (int i = 0; i < 4; i++)
            {
                bytes2[i] = totalSizeBytes[i];
            }
            for (int i = 4; i < 8; i++)
            {
                bytes2[i] = stringRealSize[i - 4];
            }
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes2[i + 8] = bytes[i];
            }
            return(bytes2);
        }