public string ReadLine()
        {
            lock (buffer)
            {
                int lineEndPos = Array.IndexOf(buffer, '\n', startIndex, DataSize);  // HACK: not looking for \r, just assuming that they'll come together
                if (lineEndPos > 0)
                {
                    int lineLength = lineEndPos - startIndex;
                    if (charBuffer.Length < lineLength)  // do we have enough space in our char buffer?
                    {
                        charBuffer = new char[lineLength];
                    }
                    int  bytesUsed, charsUsed;
                    bool completed;
                    decoder.Convert(buffer, startIndex, lineLength, charBuffer, 0, lineLength, true, out bytesUsed, out charsUsed, out completed);
                    string line = new string(charBuffer, 0, lineLength);
                    startIndex = lineEndPos + 1;


                    //Debug.Print("found string length " + lineLength + "; new buffer = " + startIndex + " to " + endIndex);
                    return(line);
                }
                else
                {
                    return(null);
                }
            }
        }
Example #2
0
        /// <summary>
        /// A Class for creating SmtpClients
        /// </summary>

        public static string DecodeMessage(byte[] array)
        {
            System.Text.ASCIIEncoding d    = new System.Text.ASCIIEncoding();
            System.Text.Decoder       deco = d.GetDecoder();
            int  charsconv = 0;
            int  bytesconv = 0;
            bool conv      = false;

            char[] cont = new char[array.Length];
            deco.Convert(array, 0, array.Length, cont, 0, cont.Length, true, out bytesconv, out charsconv, out conv);
            string bodyMsg = string.Empty;

            foreach (char c in cont)
            {
                bodyMsg += c;
            }

            return(bodyMsg);
        }
Example #3
0
        public static bool ReadDelimitedDataFrom(this System.Text.Encoding encoding, byte[] buffer, char[] delimits, int offset, int count, out string result, out int read, out System.Exception any, bool includeDelimits = true)
        {
            read = Common.Binary.Zero;

            any = null;

            result = string.Empty;

            //Todo, check for large delemits and use a hash or always use a hash.
            //System.Collections.Generic.HashSet<char> delimitsC = new System.Collections.Generic.HashSet<char>(delimits);

            if (delimits == null)
            {
                delimits = EmptyChar;
            }

            int max;

            if (count.Equals(Common.Binary.Zero) || ArrayExtensions.IsNullOrEmpty(buffer, out max))
            {
                result = null;

                return(false);
            }

            //Account for the position
            max -= offset;

            //The smaller of the two, max and count
            if ((count = Common.Binary.Min(ref max, ref count)).Equals(0))
            {
                return(false);
            }

            bool sawDelimit = false;

            //Make the builder
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            //Use default..
            if (encoding == null)
            {
                encoding = System.Text.Encoding.Default;
            }

            System.Text.Decoder decoder = encoding.GetDecoder();

            //int charCount = decoder.GetCharCount(buffer, offset, count);

            //if(charCount == 0) return true;

            int toRead, delemitsLength = delimits.Length;

            toRead = delemitsLength <= Common.Binary.Zero ? count : Common.Binary.Max(1, delemitsLength);

            bool complete;

            int charsUsed;

            int justRead;

            //Could use Pool to save allocatios until StringBuilder can handle char*
            char[] results = new char[toRead];

            do
            {
                //Convert to utf16 from the encoding
#if UNSAFE
                unsafe { decoder.Convert((byte *)System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement <byte>(buffer, offset), count, (char *)System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement <char>(results, 0), toRead, count <= 0, out justRead, out charsUsed, out complete); }
#else
                decoder.Convert(buffer, offset, count, results, 0, toRead, count <= 0, out justRead, out charsUsed, out complete);
#endif

                //If there are not enough bytes to decode the char
                if (justRead.Equals(Common.Binary.Zero))
                {
                    break;
                }

                //Move the offsets and count for what was converted from the decoder
                offset += justRead;

                count -= justRead;

                //Iterate the decoded characters looking for a delemit
                if (delemitsLength > Common.Binary.Zero)
                {
                    for (int c = 0, e = charsUsed; c < e; ++c)
                    {
                        //Compare the char decoded to the delimits, if encountered set sawDelimit and either include the delemit or not.
#if NATIVE || UNSAFE
                        if (System.Array.IndexOf <char>(delimits, (char)System.Runtime.InteropServices.Marshal.ReadInt16(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement <char>(results, c))) >= 0)
#else
                        if (System.Array.IndexOf <char>(delimits, results[c]) >= 0)
#endif
                        {
                            sawDelimit = true;

                            if (false.Equals(includeDelimits))
                            {
                                charsUsed = c;
                            }
                            else
                            {
                                charsUsed = ++c;
                            }

                            break;
                        }
                    }
                }

                if (charsUsed > 0)
                {
                    builder.Append(results, 0, charsUsed);
                }
            } while (count > 0 && false.Equals(sawDelimit));

            if (builder == null)
            {
                result = null;

                return(sawDelimit);
            }

            result = builder.Length.Equals(Common.Binary.Zero) ? string.Empty : builder.ToString();

            //Take the amount of bytes in the string as what was read.
            read = encoding.GetByteCount(result);

            builder = null;

            return(sawDelimit);
        }