GetByteCount() private method

private GetByteCount ( char chars, int count ) : int
chars char
count int
return int
Esempio n. 1
0
        private void SimpleTestInstance(string testString, int port)
        {
            // Open a socket to the server
            TcpClient client = new TcpClient("localhost", port);
            Socket    socket = client.Client;

            // This is the string we expect to get back
            String expectedString = "Welcome!\r\n" + testString.ToUpper();

            // Convert the string into an array of bytes and send them all out.
            // Note the use of the synchronous Send here.  We can use it because
            // we don't care if the testing thread is blocked for a while.
            byte[] outgoingBuffer = encoding.GetBytes(testString);
            socket.Send(outgoingBuffer);

            // Read bytes from the socket until we have the number we expect.
            // We are using a blocking synchronous Receive here.
            byte[] incomingBuffer = new byte[encoding.GetByteCount(expectedString)];
            int    index          = 0;

            while (index < incomingBuffer.Length)
            {
                index += socket.Receive(incomingBuffer, index, incomingBuffer.Length - index, 0);
            }

            // Convert the buffer into a string and make sure it is what was expected
            String result = encoding.GetString(incomingBuffer);

            Assert.AreEqual(expectedString, result);
        }
Esempio n. 2
0
 public void sendString(String st) { 
     UTF8Encoding asen = new UTF8Encoding();
     lock (lock1){
         s.Send(BitConverter.GetBytes(asen.GetByteCount(st)),4,SocketFlags.None); 
         s.Send(asen.GetBytes(st));
     }
 }
 public void PosTest2()
 {
     Char[] chars = new Char[] { };
     UTF8Encoding utf8 = new UTF8Encoding();
     int byteCount = utf8.GetByteCount(chars, 0, 0);
     Assert.Equal(0, byteCount);
 }
Esempio n. 4
0
        /// <summary>
        /// Set the String value in the array starting from the pos index
        /// String will be transmitted prefixed by length UInt16 in bytes and terminated by zero byte
        /// </summary>
        protected static int SetString(byte[] data, string value = "", int pos = -1)
        {
            if (value == null)
            {
                value = "";
            }

            if (data != null && pos > -1)
            {
                // ASCIIEncoding encodingLib = new System.Text.ASCIIEncoding();
                UTF8Encoding encodingLib   = new System.Text.UTF8Encoding();
                int          valueByteSize = encodingLib.GetByteCount(value);
                byte[]       valueData     = new byte[valueByteSize + 3]; // need space for character counter at the beginning of array and termination at end
                SetUInt16(valueData, (UInt16)valueByteSize, 0);           //first two bytes set the length of the string

                byte[] temp = new byte[valueByteSize];
                temp = encodingLib.GetBytes(value);

                Array.Copy(temp, 0, valueData, 2, temp.Length);
                //the last byte of valueData is allways zero based on our initial declaration. This indicates the terminator byte as well. Copy this to the main byte array
                Array.Copy(valueData, 0, data, pos, valueData.Length);
                return(pos + valueByteSize + 3); //we added three more characters/bytes than passed in
            }
            else
            {
                //we cannot write to null data reference or invalid pos, return zero pos or same pos as passed
                return(pos == -1 ? 0:pos);
            }
        }
Esempio n. 5
0
 public static int getByteCount(string value)
 {
     int count = 0;
     UTF8Encoding utf8 = new UTF8Encoding();
     count = utf8.GetByteCount(value);
     return count + 4;
 }
 public void PosTest1()
 {
     String chars = "UTF8 Encoding Example";
     UTF8Encoding utf8 = new UTF8Encoding();
     int byteCount = utf8.GetByteCount(chars);
     Assert.Equal(chars.Length, byteCount);
 }
 public void PosTest2()
 {
     String chars = "";
     UTF8Encoding utf8 = new UTF8Encoding();
     int byteCount = utf8.GetByteCount(chars);
     Assert.Equal(0, byteCount);
 }
 public void TestSizeWithUKPound()
 {
     UTF8Encoding encoding = new UTF8Encoding();
     int baseSize = 5;
     String test = "1£";
     BsonString bstr = new BsonString(test);
     Assert.AreEqual(baseSize + encoding.GetByteCount(test), bstr.Size, "Size didn't count the double wide pound symbol correctly");
 }
        /// <summary>
        /// Given a status code, compiles and then sends the proper response back to the client.
        /// </summary>
        /// <param name="status"></param>
        /// <param name="content"></param>
        private void CompileMessage(HttpStatusCode status, dynamic content)
        {
            StringBuilder message = new StringBuilder("HTTP/1.1 ");

            // If there doesn't need to be a JSON object, just return the status
            if (content == null)
            {
                // Forbidden
                if (status == HttpStatusCode.Forbidden)
                {
                    message.Append("403 FORBIDDEN \r\n");
                }
                // OK
                else if (status == HttpStatusCode.OK)
                {
                    message.Append("200 OK \r\n");
                }
                // Conflict
                else if (status == HttpStatusCode.Conflict)
                {
                    message.Append("409 CONFLICT \r\n");
                }
                // Finish by appending the message headers
                message.Append("content-type: application/json; charset=utf-8 \r\n");
                message.Append("content-length: 0 \r\n");
                message.Append("\r\n");
            }
            // Create the JSON object to be returned
            else
            {
                string convertedContent = JsonConvert.SerializeObject(content);
                int    contentLength    = encoding.GetByteCount(convertedContent);

                // Created
                if (status == HttpStatusCode.Created)
                {
                    message.Append("201 CREATED \r\n");
                }
                // Accepted
                else if (status == HttpStatusCode.Accepted)
                {
                    message.Append("202 ACCEPTED \r\n");
                }
                // OK
                else if (status == HttpStatusCode.OK)
                {
                    message.Append("200 OK \r\n");
                }
                // Finish by appending the message headers
                message.Append("content-type: application/json; charset=utf-8 \r\n");
                message.Append("content-length: " + contentLength + " \r\n");
                message.Append("\r\n");
                message.Append(convertedContent);
            }

            // Send the message we compiled.
            SendMessage(message.ToString());
        }
Esempio n. 10
0
        public static byte[] ToUTF8Bytes(this string cultureStringMsg)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            int dataLen = encoding.GetByteCount(cultureStringMsg);
            byte[] utf8bytes = new byte[dataLen];
            Encoding.UTF8.GetBytes(cultureStringMsg, 0, cultureStringMsg.Length, utf8bytes, 0);

            return utf8bytes;
        }
Esempio n. 11
0
 public void NegTest1()
 {
     String chars = null;
     UTF8Encoding utf8 = new UTF8Encoding();
     Assert.Throws<ArgumentNullException>(() =>
     {
         int byteCount = utf8.GetByteCount(chars);
     });
 }
Esempio n. 12
0
        public static SslStream Init_ServerCommunication(string URL, int Port)
        {
            UTF8Encoding encoder = new UTF8Encoding();

            //
            //First create the header
            //
            byte ver = 2;
            byte opcode = 0;
            ushort response = 0;
            string uri = @"h";
            string data = "Hiya: \"hi\"";

            byte[] header = new byte[8];
            header[0] = ver;
            header[1] = opcode;
            header[2] = BitConverter.GetBytes(response)[0];
            header[3] = BitConverter.GetBytes(response)[1];

            if (encoder.GetByteCount(uri) > ushort.MaxValue)
                throw new Exception("The URI is too large to send");
            ushort uriLength = (ushort)encoder.GetByteCount(uri);

            if (encoder.GetByteCount(data) > ushort.MaxValue)
                throw new Exception("The data is too large to send");
            ushort dataLength = (ushort)encoder.GetByteCount(data);

            header[4] = BitConverter.GetBytes(uriLength)[0];
            header[5] = BitConverter.GetBytes(uriLength)[1];
            header[6] = BitConverter.GetBytes(dataLength)[0];
            header[7] = BitConverter.GetBytes(dataLength)[1];

            TcpClient clientSocket = new TcpClient(URL, Port);

            SslStream sslStream = new SslStream(clientSocket.GetStream(), false, new RemoteCertificateValidationCallback(CertificateValidationCallback));

            sslStream.AuthenticateAsClient(URL);

            sslStream.Write(header, 0, 8);
            sslStream.Write(encoder.GetBytes(uri), 0, uriLength);
            sslStream.Write(encoder.GetBytes(data), 0, dataLength);

            return sslStream;
        }
Esempio n. 13
0
 public void PosTest1()
 {
     Char[] chars = new Char[] {
                     '\u0023',
                     '\u0025',
                     '\u03a0',
                     '\u03a3'  };
     UTF8Encoding utf8 = new UTF8Encoding();
     int byteCount = utf8.GetByteCount(chars, 1, 2);
 }
Esempio n. 14
0
        public static string GetLengthAsByte(string strToCount)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            if (!string.IsNullOrEmpty(strToCount))
            {
                return(encoding.GetByteCount(strToCount).ToString());
            }

            return("0");
        }
		protected string ConvertUTF8ToBytes(string str) 
		{
			UTF8Encoding utf8 = new UTF8Encoding();

			int byteCount = utf8.GetByteCount(str);
			Byte[] bytes = new Byte[byteCount];
			utf8.GetBytes(str, 0, str.Length, bytes, 0);

			return Convert.ToBase64String(bytes);
		}
Esempio n. 16
0
        public void PosTest2()
        {
            Byte[] bytes;
            Char[] chars = new Char[] { };

            UTF8Encoding utf8 = new UTF8Encoding();

            int byteCount = utf8.GetByteCount(chars, 0, 0);
            bytes = new Byte[byteCount];
            int charsEncodedCount = utf8.GetChars(bytes, 0, 0, chars, 0);
            Assert.Equal(0, charsEncodedCount);
        }
Esempio n. 17
0
 public void NegTest3()
 {
     Char[] chars = new Char[] {
                     '\u0023',
                     '\u0025',
                     '\u03a0',
                     '\u03a3'  };
     UTF8Encoding utf8 = new UTF8Encoding();
     Assert.Throws<ArgumentOutOfRangeException>(() =>
     {
         int byteCount = utf8.GetByteCount(chars, 1, -2);
     });
 }
Esempio n. 18
0
        /// <summary>
        /// �����û���
        /// </summary>
        /// <param name="str">��֤���ַ���</param>
        /// <returns></returns>
        public static bool IsReaderName(string str)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            int byteCount = encoding.GetByteCount(str);
            int strLen = str.Length;

            if (strLen == byteCount)
            {
                return true;
            }

            return false;
        }
Esempio n. 19
0
        /// <summary>
        /// 全角验证
        /// </summary>
        /// <param name="str">验证的字符串</param>
        /// <returns></returns>
        public static bool IsSBC(string str)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            int byteCount = encoding.GetByteCount(str);
            int strLen = str.Length;

            if (byteCount == strLen * 3)
            {
                return true;
            }

            return false;
        }
        public void SendNotificationMessage(string token, string alertText, int badge)
        {
            string key = "your urban airship app key";
            string appsecret = "your urban airship app secret";

            var aps = new loadobject
            {
                DeviceToken = new List<string> {token},

                APS = new loadobject.APSBody
                {
                    Badge = badge,
                    Alert = alertText

                }

            };

            // construct json to be send to U.A. REST API
            var json = aps.ToJsonString();

            // communicate with urban airship push server
            var uri = new Uri("https://go.urbanairship.com/api/push/");
            var encoding = new UTF8Encoding();
            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.Credentials = new NetworkCredential(key, appsecret);
            request.ContentType = "application/json";
            request.ContentLength = encoding.GetByteCount(json);

            using (var stream = request.GetRequestStream())
            {
                stream.Write(encoding.GetBytes(json), 0, encoding.GetByteCount(json));
                stream.Close();
                var response = request.GetResponse();
                response.Close();
            }
        }
Esempio n. 21
0
        public void PosTest1()
        {
            Byte[] bytes;
            Char[] chars = new Char[] {
                            '\u0023',
                            '\u0025',
                            '\u03a0',
                            '\u03a3'  };

            UTF8Encoding utf8 = new UTF8Encoding();
            int byteCount = utf8.GetByteCount(chars, 1, 2);
            bytes = new Byte[byteCount];
            int charsEncodedCount = utf8.GetChars(bytes, 1, 2, chars, 0);
        }
Esempio n. 22
0
 static public int GetByteCount__A_Char(IntPtr l)
 {
     try {
         System.Text.UTF8Encoding self = (System.Text.UTF8Encoding)checkSelf(l);
         System.Char[]            a1;
         checkArray(l, 2, out a1);
         var ret = self.GetByteCount(a1);
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
        /// <summary>
        /// Generates a hash value
        /// </summary>
        /// <param name="clearData">Data to be hashed</param>
        /// <param name="saltValue">The salt to add on to the end of the data</param>
        /// <param name="hash">The hashing algorithm</param>
        /// <returns>The final hash from the input string</returns>
        public static string HashPassword(string clearData, string saltValue, HashAlgorithm hash)
        {
            var encoding = new UTF8Encoding();

            if (clearData != null && hash != null)
            {
                // If the salt string is null or the length is invalid then
                // create a new valid salt value.

                if (saltValue == null)
                {
                    // Generate a salt string.
                    saltValue = GenerateSaltValue();
                }

                // Convert the salt string and the password string to a single
                // array of bytes. Note that the password string is Unicode and
                // therefore may or may not have a zero in every other byte.

                var binarySaltValue = new byte[saltValue.Length];
                Buffer.BlockCopy(saltValue.ToCharArray(), 0, binarySaltValue, 0, binarySaltValue.Length);

                var valueToHash = new byte[4 + encoding.GetByteCount(clearData)];
                var binaryPassword = encoding.GetBytes(clearData);

                // Copy the salt value and the password to the hash buffer.

                binarySaltValue.CopyTo(valueToHash, 0);
                binaryPassword.CopyTo(valueToHash, 2);

                var hashValue = hash.ComputeHash(valueToHash);

                // The hashed password is the salt plus the hash value (as a string).

                var hashedPassword = "";

                foreach (var hexdigit in hashValue)
                {
                    hashedPassword += hexdigit.ToString("X2", CultureInfo.InvariantCulture.NumberFormat);
                }

                // Return the hashed password as a string.

                return hashedPassword;
            }
            return null;
        }
Esempio n. 24
0
        public void NegTest2()
        {
            Byte[] bytes;
            Char[] chars = new Char[] {
                            '\u0023',
                            '\u0025',
                            '\u03a0',
                            '\u03a3'  };
            UTF8Encoding utf8 = new UTF8Encoding();

            int byteCount = utf8.GetByteCount(chars, 1, 2);
            bytes = null;
            Assert.Throws<ArgumentNullException>(() =>
            {
                int bytesEncodedCount = utf8.GetBytes(chars, 1, 2, bytes, 0);
            });
        }
Esempio n. 25
0
        public PagarMeResponse Execute()
        {
            HttpWebResponse response;
            var request = GetRequest();
            bool isError = false;

            if (Body != null && (Method == "POST" || Method == "PUT"))
            {
                var encoding = new UTF8Encoding(false);

                request.ContentLength = encoding.GetByteCount(Body);

                using (var stream = request.GetRequestStream())
                using (var writer = new StreamWriter(stream, encoding))
                    writer.Write(Body);
            }

            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException e)
            {
                isError = true;
                response = (HttpWebResponse)e.Response;

                if (response == null)
                    throw e;
            }

            var body = "";

            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream, Encoding.UTF8))
                body = reader.ReadToEnd();

            if (isError)
                throw new PagarMeException(new PagarMeError(response.StatusCode, body));
            else
                return new PagarMeResponse(response.StatusCode, body);
        }
Esempio n. 26
0
 protected static int GetUTF8StringLength(string s)
 {
     UTF8Encoding enc = new UTF8Encoding();
     return enc.GetByteCount(s);
 }
Esempio n. 27
0
        public static void WritePrimitive(Stream stream, string value)
        {
            if (value == null)
            {
                WritePrimitive(stream, (uint)0);
                return;
            }

            var encoding = new UTF8Encoding(false, true);

            int len = encoding.GetByteCount(value);

            WritePrimitive(stream, (uint)len + 1);

            var buf = new byte[len];

            encoding.GetBytes(value, 0, value.Length, buf, 0);

            stream.Write(buf, 0, len);
        }
Esempio n. 28
0
        private byte[] BuildPacket()
        {
            byte[] buffer;
            if (DataBuffer==null)
            {
                DataBuffer = new Byte[0];
            }

            if (Version=="1.0")
            {
                if ((Method=="")&&(ResponseCode==-1))
                {
                    return(DataBuffer);
                }
            }

            UTF8Encoding UTF8 = new UTF8Encoding();
            String sbuf;
            IDictionaryEnumerator en = TheHeaders.GetEnumerator();
            en.Reset();

            if (Method!="")
            {
                if (Version!="")
                {
                    sbuf = Method + " " + EscapeString(MethodData) + " HTTP/" + Version + "\r\n";
                }
                else
                {
                    sbuf = Method + " " + EscapeString(MethodData) + "\r\n";
                }
            }
            else
            {
                sbuf = "HTTP/" + Version + " " + ResponseCode.ToString() + " " + ResponseData + "\r\n";
            }
            while(en.MoveNext())
            {
                if ((String)en.Key!="CONTENT-LENGTH" || OverrideContentLength==true)
                {
                    if (en.Value.GetType()==typeof(string))
                    {
                        sbuf += (String)en.Key + ": " + (String)en.Value + "\r\n";
                    }
                    else
                    {
                        sbuf += (String)en.Key + ":";
                        foreach(string v in (ArrayList)en.Value)
                        {
                            sbuf += (" " + v + "\r\n");
                        }
                    }
                }
            }
            if (StatusCode==-1 && DontShowContentLength==false)
            {
                sbuf += "Content-Length: " + DataBuffer.Length.ToString() + "\r\n";
            }
            else if (Version!="1.0" && Version!="0.9" && Version!="" && DontShowContentLength==false)
            {
                if (OverrideContentLength==false)
                {
                    sbuf += "Content-Length: " + DataBuffer.Length.ToString() + "\r\n";
                }
            }

            sbuf += "\r\n";

            buffer = new byte[UTF8.GetByteCount(sbuf) + DataBuffer.Length];
            UTF8.GetBytes(sbuf,0,sbuf.Length,buffer,0);
            Array.Copy(DataBuffer,0,buffer,buffer.Length-DataBuffer.Length,DataBuffer.Length);
            return(buffer);
        }
Esempio n. 29
0
        public bool TryReadChars(char[] chars, int offset, int count, out int actual)
        {
            Fx.Assert(offset + count <= chars.Length, string.Format("offset '{0}' + count '{1}' MUST BE <= chars.Length '{2}'", offset, count, chars.Length)); 

            if (type == ValueHandleType.Unicode)
                return TryReadUnicodeChars(chars, offset, count, out actual);

            if (type != ValueHandleType.UTF8)
            {
                actual = 0;
                return false;
            }

            int charOffset = offset;
            int charCount = count;
            byte[] bytes = bufferReader.Buffer;
            int byteOffset = this.offset;
            int byteCount = this.length;
            bool insufficientSpaceInCharsArray = false; 

            while (true)
            {
                while (charCount > 0 && byteCount > 0)
                {
                    // fast path for codepoints U+0000 - U+007F
                    byte b = bytes[byteOffset];
                    if (b >= 0x80)
                        break;
                    chars[charOffset] = (char)b;
                    byteOffset++;
                    byteCount--;
                    charOffset++;
                    charCount--;
                }

                if (charCount == 0 || byteCount == 0 || insufficientSpaceInCharsArray)
                    break;

                int actualByteCount;
                int actualCharCount;

                UTF8Encoding encoding = new UTF8Encoding(false, true);
                try
                {
                    // If we're asking for more than are possibly available, or more than are truly available then we can return the entire thing
                    if (charCount >= encoding.GetMaxCharCount(byteCount) || charCount >= encoding.GetCharCount(bytes, byteOffset, byteCount))
                    {
                        actualCharCount = encoding.GetChars(bytes, byteOffset, byteCount, chars, charOffset);
                        actualByteCount = byteCount;
                    }
                    else
                    {
                        Decoder decoder = encoding.GetDecoder();

                        // Since x bytes can never generate more than x characters this is a safe estimate as to what will fit
                        actualByteCount = Math.Min(charCount, byteCount);

                        // We use a decoder so we don't error if we fall across a character boundary
                        actualCharCount = decoder.GetChars(bytes, byteOffset, actualByteCount, chars, charOffset);

                        // We might've gotten zero characters though if < 4 bytes were requested because
                        // codepoints from U+0000 - U+FFFF can be up to 3 bytes in UTF-8, and represented as ONE char
                        // codepoints from U+10000 - U+10FFFF (last Unicode codepoint representable in UTF-8) are represented by up to 4 bytes in UTF-8 
                        //                                    and represented as TWO chars (high+low surrogate)
                        // (e.g. 1 char requested, 1 char in the buffer represented in 3 bytes)
                        while (actualCharCount == 0)
                        {
                            // Note the by the time we arrive here, if actualByteCount == 3, the next decoder.GetChars() call will read the 4th byte
                            // if we don't bail out since the while loop will advance actualByteCount only after reading the byte. 
                            if (actualByteCount >= 3 && charCount < 2)
                            {
                                // If we reach here, it means that we're: 
                                // - trying to decode more than 3 bytes and, 
                                // - there is only one char left of charCount where we're stuffing decoded characters. 
                                // In this case, we need to back off since decoding > 3 bytes in UTF-8 means that we will get 2 16-bit chars 
                                // (a high surrogate and a low surrogate) - the Decoder will attempt to provide both at once 
                                // and an ArgumentException will be thrown complaining that there's not enough space in the output char array.  

                                // actualByteCount = 0 when the while loop is broken out of; decoder goes out of scope so its state no longer matters

                                insufficientSpaceInCharsArray = true; 
                                break; 
                            }
                            else
                            {
                                Fx.Assert(byteOffset + actualByteCount < bytes.Length, 
                                    string.Format("byteOffset {0} + actualByteCount {1} MUST BE < bytes.Length {2}", byteOffset, actualByteCount, bytes.Length));
                                
                                // Request a few more bytes to get at least one character
                                actualCharCount = decoder.GetChars(bytes, byteOffset + actualByteCount, 1, chars, charOffset);
                                actualByteCount++;
                            }
                        }

                        // Now that we actually retrieved some characters, figure out how many bytes it actually was
                        actualByteCount = encoding.GetByteCount(chars, charOffset, actualCharCount);
                    }
                }
                catch (FormatException exception)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateEncodingException(bytes, byteOffset, byteCount, exception));
                }

                // Advance
                byteOffset += actualByteCount;
                byteCount -= actualByteCount;

                charOffset += actualCharCount;
                charCount -= actualCharCount;
            }

            this.offset = byteOffset;
            this.length = byteCount;

            actual = (count - charCount);
            return true;
        }
Esempio n. 30
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (twitpicfiles == null)
            {
                MessageBox.Show("please select twitpic folder ", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (textBoxTwitlog.Text == "")
            {
                MessageBox.Show("please select twitter csv file ", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string exportstr = "";
            int htmlcount = 0;
            int exportcount = 0;
            textBoxLog.AppendText("twitter log csv file:" + textBoxTwitlog.Text + Environment.NewLine);
            TextFieldParser parser = new TextFieldParser(textBoxTwitlog.Text, Encoding.GetEncoding("UTF-8"));
            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");
            while (parser.EndOfData == false)
            {
                string[] tweetdata = parser.ReadFields();
                if ((!CheckRT(tweetdata)) && (CheckTwitpic(tweetdata)))
                {
                    string[] picstr = CheckPicDict(tweetdata, twitpicdic, twitpicfiles);
                    if (picstr.Length > 0)
                    {
                        string htm = GenHtml(tweetdata,
                            textBoxpicurl.Text,
                            textBoxTwitterurl.Text,
                            textBoxTwilogurl.Text,
                            textBoxSearchdomain.Text,
                            textBoxSearchname.Text,
                            twitpicdic,
                            twitpicfiles);
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(tweetdata[tweet_id] + ".htm", false, System.Text.Encoding.GetEncoding("UTF-8"));
                        sw.Write(htm);
                        sw.Close();
                        htmlcount++;
                        exportstr += GenExport(textBoxAuthor.Text,
                            textBoxTitle.Text,
                            textBoxStatus.Text,
                            textBoxAllowComments.Text,
                            textBoxConvertBreaks.Text,
                            textBoxAllowPings.Text,
                            textBoxCategory.Text,
                            tweetdata[timestamp],
                            htm,
                            "",
                            "",
                            textBoxKeywords.Text);
                    }

                }
                if ((htmlcount % 100) == 0)
                {
                    System.Text.Encoding utfenc = new System.Text.UTF8Encoding(false);
                    int utfsize = utfenc.GetByteCount(exportstr);
                    if (utfsize > postsizelimit)
                    {
                        System.IO.StreamWriter swexportdivided =
                            new System.IO.StreamWriter(
                                "post" + exportcount.ToString("00") + ".txt",
                                false, utfenc);
                        swexportdivided.Write(exportstr);
                        swexportdivided.Close();
                        exportstr = "";
                        exportcount++;
                    }
                    labelline.Text = htmlcount + " html files " + exportcount + " text files";
                    Application.DoEvents();
                }
            }
            parser.Close();

            System.Text.Encoding enc = new System.Text.UTF8Encoding(false); // without BOM
            System.IO.StreamWriter swexport = new System.IO.StreamWriter(
                    "post" + exportcount.ToString("00") + ".txt", false, enc);
            swexport.Write(exportstr);
            swexport.Close();
            exportcount++;
            labelline.Text = htmlcount + " html files " + exportcount + " text files";
            textBoxLog.Text += htmlcount + " html files " + exportcount + " text files" + Environment.NewLine;
        }
 public int GetByteCount() => _utf8Encoding.GetByteCount(_unicode);
Esempio n. 32
0
 /// <summary>
 /// Count the number of bytes that are required to encode
 /// a string in UTF8.
 /// </summary>
 /// <param name="Text"></param>
 /// <returns></returns>
 public static int CountUTF8(this string Text) {
     var Encoding = new UTF8Encoding();
     return Encoding.GetByteCount(Text);
     }
Esempio n. 33
0
        static void TestWithTypedMessage()
        {
            string baseAddress = SizedTcpDuplexTransportBindingElement.SizedTcpScheme + "://localhost:8000";
            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
            Binding binding = new CustomBinding(new SizedTcpDuplexTransportBindingElement());
            host.AddServiceEndpoint(typeof(ITypedTest), binding, "");
            host.Open();

            Console.WriteLine("Host opened");

            Socket socket = GetConnectedSocket(8000);

            string request = @"<s:Envelope
        xmlns:s=""http://www.w3.org/2003/05/soap-envelope""
        xmlns:a=""http://www.w3.org/2005/08/addressing"">
    <s:Header>
        <a:Action s:mustUnderstand=""1"">http://tempuri.org/ITypedTest/Add</a:Action>
        <a:MessageID>urn:uuid:c2998797-7312-481a-8f73-230406b12bea</a:MessageID>
        <a:ReplyTo>
            <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
        </a:ReplyTo>
        <a:To s:mustUnderstand=""1"">ENDPOINT_ADDRESS</a:To>
    </s:Header>
    <s:Body>
        <Add xmlns=""http://tempuri.org/"">
            <x>4</x>
            <y>5</y>
        </Add>
    </s:Body>
</s:Envelope>";

            request = request.Replace("ENDPOINT_ADDRESS", baseAddress);

            Encoding encoding = new UTF8Encoding(false);
            int byteCount = encoding.GetByteCount(request);
            byte[] reqBytes = new byte[byteCount + 4];
            Formatting.SizeToBytes(byteCount, reqBytes, 0);
            encoding.GetBytes(request, 0, request.Length, reqBytes, 4);
            Console.WriteLine("Sending those bytes:");
            Debugging.PrintBytes(reqBytes);
            socket.Send(reqBytes);
            byte[] recvBuffer = new byte[10000];
            int bytesRecvd = socket.Receive(recvBuffer);
            Console.WriteLine("Received {0} bytes", bytesRecvd);
            Debugging.PrintBytes(recvBuffer, bytesRecvd);

            Console.WriteLine("Press ENTER to send another request");
            Console.ReadLine();

            request = @"<s:Envelope
        xmlns:s=""http://www.w3.org/2003/05/soap-envelope""
        xmlns:a=""http://www.w3.org/2005/08/addressing"">
    <s:Header>
        <a:Action s:mustUnderstand=""1"">http://tempuri.org/ITypedTest/Subtract</a:Action>
        <a:MessageID>urn:uuid:c2998797-7312-481a-8f73-230406b12bea</a:MessageID>
        <a:ReplyTo>
            <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
        </a:ReplyTo>
        <a:To s:mustUnderstand=""1"">ENDPOINT_ADDRESS</a:To>
    </s:Header>
    <s:Body>
        <Subtract xmlns=""http://tempuri.org/"">
            <x>4</x>
            <y>5</y>
        </Subtract>
    </s:Body>
</s:Envelope>";

            request = request.Replace("ENDPOINT_ADDRESS", baseAddress);
            byteCount = encoding.GetByteCount(request);
            reqBytes = new byte[byteCount + 4];
            Formatting.SizeToBytes(byteCount, reqBytes, 0);
            encoding.GetBytes(request, 0, request.Length, reqBytes, 4);
            Console.WriteLine("Sending those bytes:");
            Debugging.PrintBytes(reqBytes);
            socket.Send(reqBytes);
            bytesRecvd = socket.Receive(recvBuffer);
            Console.WriteLine("Received {0} bytes", bytesRecvd);
            Debugging.PrintBytes(recvBuffer, bytesRecvd);

            socket.Close();

            Console.WriteLine("Press ENTER to close");
            Console.ReadLine();
            host.Close();
        }
Esempio n. 34
0
 public override void WriteString(string value, UTF8Encoding encoding)
 {
     var length = encoding.GetByteCount(value);
     Position += 4 + length + 1;
 }
Esempio n. 35
0
            byte[] bytes = BitConverter.GetBytes(value);
        WriteBigEndian(bytes);
        }

        //写入短整型[i][i][i][i][i]
Esempio n. 36
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (twitpicfiles == null)
            {
                MessageBox.Show("please select twitpic folder ", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (textBoxTwitlog.Text == "")
            {
                MessageBox.Show("please select twitter csv file ", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string exportstr   = "";
            int    htmlcount   = 0;
            int    exportcount = 0;

            textBoxLog.AppendText("twitter log csv file:" + textBoxTwitlog.Text + Environment.NewLine);
            TextFieldParser parser = new TextFieldParser(textBoxTwitlog.Text, Encoding.GetEncoding("UTF-8"));

            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");
            while (parser.EndOfData == false)
            {
                string[] tweetdata = parser.ReadFields();
                if ((!CheckRT(tweetdata)) && (CheckTwitpic(tweetdata)))
                {
                    string[] picstr = CheckPicDict(tweetdata, twitpicdic, twitpicfiles);
                    if (picstr.Length > 0)
                    {
                        string htm = GenHtml(tweetdata,
                                             textBoxpicurl.Text,
                                             textBoxTwitterurl.Text,
                                             textBoxTwilogurl.Text,
                                             textBoxSearchdomain.Text,
                                             textBoxSearchname.Text,
                                             twitpicdic,
                                             twitpicfiles);
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(tweetdata[tweet_id] + ".htm", false, System.Text.Encoding.GetEncoding("UTF-8"));
                        sw.Write(htm);
                        sw.Close();
                        htmlcount++;
                        exportstr += GenExport(textBoxAuthor.Text,
                                               textBoxTitle.Text,
                                               textBoxStatus.Text,
                                               textBoxAllowComments.Text,
                                               textBoxConvertBreaks.Text,
                                               textBoxAllowPings.Text,
                                               textBoxCategory.Text,
                                               tweetdata[timestamp],
                                               htm,
                                               "",
                                               "",
                                               textBoxKeywords.Text);
                    }
                }
                if ((htmlcount % 100) == 0)
                {
                    System.Text.Encoding utfenc = new System.Text.UTF8Encoding(false);
                    int utfsize = utfenc.GetByteCount(exportstr);
                    if (utfsize > postsizelimit)
                    {
                        System.IO.StreamWriter swexportdivided =
                            new System.IO.StreamWriter(
                                "post" + exportcount.ToString("00") + ".txt",
                                false, utfenc);
                        swexportdivided.Write(exportstr);
                        swexportdivided.Close();
                        exportstr = "";
                        exportcount++;
                    }
                    labelline.Text = htmlcount + " html files " + exportcount + " text files";
                    Application.DoEvents();
                }
            }
            parser.Close();

            System.Text.Encoding   enc      = new System.Text.UTF8Encoding(false); // without BOM
            System.IO.StreamWriter swexport = new System.IO.StreamWriter(
                "post" + exportcount.ToString("00") + ".txt", false, enc);
            swexport.Write(exportstr);
            swexport.Close();
            exportcount++;
            labelline.Text   = htmlcount + " html files " + exportcount + " text files";
            textBoxLog.Text += htmlcount + " html files " + exportcount + " text files" + Environment.NewLine;
        }
Esempio n. 37
0
        public static byte[] CByteUTF8(string sData)
        {
            byte[] nuller = null;

            try
            {
                System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                byte[] byter = new byte[encoding.GetByteCount(sData)];
                encoding.GetBytes(sData, 0, sData.Length, byter, 0);
                return byter;
            }
            catch { }

            return nuller;
        }
Esempio n. 38
0
        public static void Main(string[] args)
        {
            // Recognize that a URL maps to a service method and extract parameters
            Regex r = new Regex(@"^/BoggleService.svc/games/(\d*)$");

            string url1 = "/BoggleService.svc/games";
            Match  m1   = r.Match(url1);

            if (m1.Success)
            {
                Console.WriteLine(url1 + " matches and contains parameter " + m1.Groups[1]);
            }
            else
            {
                Console.WriteLine(url1 + " doesn't match");
            }

            string url2 = "/BoggleService.svc/games/187";
            Match  m2   = r.Match(url2);

            if (m2.Success)
            {
                Console.WriteLine(url2 + " matches and contains parameter " + m2.Groups[1]);
            }
            else
            {
                Console.WriteLine(url2 + " doesn't match");
            }
            Console.ReadLine();

            // Deserialize JSON into something of a specific type
            string json = "{\"Name\" : \"John\", \"Age\": 42}";
            Person p1   = JsonConvert.DeserializeObject <Person>(json);

            Console.WriteLine(p1.Name + " " + p1.Age);
            Console.ReadLine();

            // Omit default properties when serializing JSON
            Person p2 = new Person {
                Name = "Jane"
            };
            String s = JsonConvert.SerializeObject(p2, new JsonSerializerSettings
            {
                DefaultValueHandling = DefaultValueHandling.Ignore
            });

            Console.WriteLine(s);
            Console.ReadLine();

            // Determine how many bytes it will take to encode a string
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            string s1 = "Hello World";
            int    n1 = encoding.GetByteCount(s1);

            Console.WriteLine(s1 + " takes " + n1 + " bytes");
            string s2 = "αβδ";
            int    n2 = encoding.GetByteCount(s2);

            Console.WriteLine(s2 + " takes " + n2 + " bytes");
            Console.ReadLine();
        }
Esempio n. 39
0
 public override int GetByteCount(char[] chars, int index, int count, bool flush)
 {
     mustFlush = flush;
     return(encoding.GetByteCount(chars, index, count, this));
 }
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                // Check identity of current user
                if (!IdentityManager.IsAuthenticated())
                {
                    throw new AuthenticationException("Not authenticated into the system!");
                }

                string documentType = (context.Request.QueryString["documentType"] ?? string.Empty).Trim().ToLower();

                // Special case handling to export a loan into an xml file
                if (documentType == "loanxml")
                {
                    // it brings in query string parameters
                    string t_loanId     = context.Request.QueryString["loanId"];
                    string t_encryptssn = context.Request.QueryString["encryptssn"];

                    Guid _loanId = new Guid(t_loanId);

                    // it grabs loan's XML
                    String _loanXml = LoanServiceFacade.GetLoanXml(_loanId, AccountHelper.GetUserAccountId(), Boolean.Parse(t_encryptssn));

                    // flushes loan's XML into the context response
                    if (!string.IsNullOrWhiteSpace(_loanXml))
                    {
                        context.Response.ContentType = "text/xml-downloaded-only";
                        context.Response.AddHeader("Content-Disposition", "attachment; filename=loan_data.xml");
                        context.Response.Write(_loanXml);
                        context.Response.Flush();
                        context.Response.End();
                    }
                    return;
                }

                // Special case handling
                if (documentType == "loanservicecontentxml" || documentType == "loanservicefailuremessage")
                {
                    string data     = String.Empty;
                    string filename = "data.xml";

                    try
                    {
                        string eventType = (context.Request.QueryString["eventType"] ?? string.Empty).Trim();
                        if (String.IsNullOrEmpty(eventType))
                        {
                            throw new Exception("Valid Loan Service Event Type must be provided.");
                        }

                        string rawEventId = String.Empty;

                        long eventId = 0;
                        if (!long.TryParse(context.Request.QueryString["eventId"], out eventId))
                        {
                            rawEventId = EncryptionHelper.DecryptRijndael((context.Request.QueryString["eventId"] ?? string.Empty).Trim(), EncriptionKeys.Default);
                        }
                        if (rawEventId != String.Empty)
                        {
                            long.TryParse(rawEventId, out eventId);
                        }

                        if (eventId == 0)
                        {
                            throw new Exception("Valid Loan Service Event Id must be provided.");
                        }
                        var loanServiceEvent = LoanServiceFacade.GetLoanServiceEvent(eventType, eventId);
                        if (loanServiceEvent == null)
                        {
                            data = "<Error>Unable to retrieve service event</Error>";
                        }
                        else if (String.IsNullOrWhiteSpace(loanServiceEvent.ContentXml))
                        {
                            data = "<Error>Unable to retrieve service event parameters</Error>";
                        }
                        else
                        {
                            switch (documentType)
                            {
                            case "loanservicecontentxml":
                                data = loanServiceEvent.ContentXml;
                                break;

                            case "loanservicefailuremessage":
                                data     = loanServiceEvent.FailureMessage + Environment.NewLine + Environment.NewLine + loanServiceEvent.FailureStackTrace;
                                filename = "ErrorDetails.txt";
                                break;

                            default:
                                data = "<Error>Unknown documentType parameter</Error>";
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        TraceHelper.Error(TraceCategory.ConciergeDocumentDownloader, ex.ToString(), ex, Guid.Empty, IdentityManager.GetUserAccountId());
                        data = "<Error>Unknown Error</Error>";
                    }

                    context.Response.ContentType = "text/xml-downloaded-only";
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                    context.Response.Write(data);
                    context.Response.Flush();
                    context.Response.End();

                    return;
                }
                else if (!String.IsNullOrEmpty(context.Request.QueryString["documentType"]) && context.Request.QueryString["documentType"] == "logItem" &&
                         !String.IsNullOrEmpty(context.Request.QueryString["loanId"]) &&
                         !String.IsNullOrEmpty(context.Request.QueryString["userId"]))
                {
                    Guid loanId = Guid.Empty;
                    if (!Guid.TryParse(context.Request.QueryString["loanId"], out loanId))
                    {
                        loanId = new Guid(EncryptionHelper.DecryptRijndael(context.Request.QueryString["loanId"], EncriptionKeys.Default));
                    }

                    if (loanId == Guid.Empty)
                    {
                        throw new Exception("Error while getting LoanId!");
                    }

                    int userId = 0;
                    if (!Int32.TryParse(context.Request.QueryString["userId"], out userId))
                    {
                        userId = Int32.Parse(EncryptionHelper.DecryptRijndael(context.Request.QueryString["userId"], EncriptionKeys.Default));
                    }
                    if (userId == 0)
                    {
                        throw new Exception("Error while getting UserId!");
                    }

                    var name = context.Server.UrlDecode(context.Request.QueryString["name"]);

                    string loanNumber    = null;
                    int    outLoanNumber = 0;

                    if (context.Request.QueryString["loanNumber"] == "Pending" || context.Request.QueryString["loanNumber"] == String.Empty)
                    {
                        loanNumber = context.Request.QueryString["loanNumber"];
                    }

                    if (loanNumber == null)
                    {
                        if (!Int32.TryParse(context.Request.QueryString["loanNumber"], out outLoanNumber))
                        {
                            loanNumber = EncryptionHelper.DecryptRijndael(context.Request.QueryString["loanNumber"], EncriptionKeys.Default);
                        }
                        else
                        {
                            loanNumber = outLoanNumber.ToString();
                        }
                    }

                    if (loanNumber == null)
                    {
                        throw new Exception("Error while getting loanNumber!");
                    }


                    List <IntegrationLogFolder> integrationDocuments = IntegrationLogServiceFacade.GetIntegrationLogItems(loanId, userId, loanNumber.ToString());

                    String xml      = null;
                    String fileName = String.Empty;

                    var isFnm = false;

                    if (integrationDocuments != null)
                    {
                        foreach (IntegrationLogFolder integrationLogItems in integrationDocuments)
                        {
                            bool isBreak = false;

                            foreach (IntegrationLogItem integrationLogItem in integrationLogItems.Items)
                            {
                                if (integrationLogItem.Name.Equals(name))
                                {
                                    fileName = integrationLogItem.Name;
                                    xml      = integrationLogItem.Data;

                                    if (integrationLogItem.EventId == "503")
                                    {
                                        isFnm = true;
                                    }

                                    isBreak = true;
                                    break;
                                }
                            }

                            if (isBreak)
                            {
                                break;
                            }
                        }
                    }

                    if (xml != null)
                    {
                        var utf8Encoding = new System.Text.UTF8Encoding();

                        context.Response.Clear();

                        context.Response.ContentType = isFnm ? DocumentContentType.TXT.GetStringValue() : DocumentContentType.XML.GetStringValue();

                        fileName += isFnm ? ".fnm" : ".xml";

                        context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
                        context.Response.BufferOutput = true;
                        context.Response.AddHeader("Content-Length", utf8Encoding.GetByteCount(xml).ToString());
                        context.Response.BinaryWrite(utf8Encoding.GetBytes(xml));
                        context.Response.Flush();
                        context.Response.End();
                    }

                    return;
                }


                if (!String.IsNullOrEmpty(context.Request.QueryString["repositoryItemId"]))
                {
                    // download document from manage documents
                    Guid repositoryItemId = Guid.Empty;
                    if (!Guid.TryParse(context.Request.QueryString["repositoryItemId"], out repositoryItemId))
                    {
                        repositoryItemId = new Guid(EncryptionHelper.DecryptRijndael(context.Request.QueryString["repositoryItemId"], EncriptionKeys.Default));
                    }

                    if (repositoryItemId == Guid.Empty)
                    {
                        throw new Exception("Error while getting RepositoryItemId!");
                    }

                    FileStoreItem item = FileStoreServiceFacade.GetFileStoreItemById(repositoryItemId, -1);

                    if (item.FileStoreItemFile == null)
                    {
                        throw new Exception("Error while getting file from repository. RepositoryItemId='" + repositoryItemId + "'");
                    }

                    string fileName = String.IsNullOrEmpty(Path.GetExtension(item.FileStoreItemFile.Filename)) ?
                                      String.Format("\"{0}.{1}\"", item.FileStoreItemFile.Filename, item.FileStoreItemFile.ContentType.ToString().ToLower()) :
                                      String.Format("\"{0}\"", item.FileStoreItemFile.Filename);

                    bool openInBrowser = false;
                    Boolean.TryParse(context.Request.QueryString["browser"], out openInBrowser);

                    using (var stream = new MemoryStream())
                    {
                        try
                        {
                            context.Response.Clear();
                            context.Response.ContentType = item.FileStoreItemFile.ContentType.GetStringValue();
                            if (!openInBrowser)
                            {
                                context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
                            }
                            context.Response.BufferOutput = true;
                            context.Response.AddHeader("Content-Length", item.FileStoreItemFile.Data.Length.ToString());
                            context.Response.BinaryWrite(item.FileStoreItemFile.Data);
                            context.Response.Flush();
                            context.Response.End();
                        }
                        catch // Suppress response flushing errors
                        {
                        }
                    }
                }
                else
                {
                    // download document from export to los form
                    Guid loanId              = new Guid(EncryptionHelper.DecryptRijndael(context.Request.QueryString["loanId"], EncriptionKeys.Default));
                    int  userAccountId       = IdentityManager.GetUserAccountId();
                    int  selectedFormatValue = int.Parse(context.Request.QueryString["selectedFormatValue"]);

                    var    borrowers        = BorrowerServiceFacade.GetBorrowerIdAndCoborrowerId(loanId, userAccountId);
                    string borrowerLastName = borrowers.BorrowerLastName;

                    // If selected file format is 0, Filename will be todayDate_borrower_URLA1003.fnm, otherwise todayDate_borrower_URLA1003.xnm
                    StringBuilder fileNameBuilder = new StringBuilder();
                    fileNameBuilder.Append(DateTime.Now.Year);
                    fileNameBuilder.Append(DateTime.Now.Month);
                    fileNameBuilder.Append(DateTime.Now.Day);
                    fileNameBuilder.Append(borrowerLastName);
                    fileNameBuilder.Append("_URLA1003.");
                    fileNameBuilder.Append(selectedFormatValue == 0 ? "fnm" : "xml");

                    // Get 1003 file from URLA service
                    string urlaFile = URLADeliveryServiceFacade.DeliverURLA(loanId, (URLADeliveryFormat)selectedFormatValue, userAccountId);

                    if (String.IsNullOrEmpty(urlaFile))
                    {
                        throw new Exception("Urla file could not be generated!");
                    }

                    using (var stream = new MemoryStream())
                    {
                        // Write out urla xml
                        using (var xmlWriter = new XmlTextWriter(stream, Encoding.ASCII))
                        {
                            xmlWriter.WriteRaw(urlaFile);

                            // Wlush the document to the memory stream
                            xmlWriter.Flush();

                            // Get a byte array from the memory stream
                            byte[] byteArray = stream.ToArray();

                            try
                            {
                                // Populate context response with data
                                context.Response.Clear();
                                context.Response.ContentType = "application/octet-stream";
                                context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileNameBuilder);
                                context.Response.BufferOutput = true;
                                context.Response.AddHeader("Content-Length", byteArray.Length.ToString());
                                context.Response.BinaryWrite(byteArray);
                                context.Response.Flush();
                                context.Response.End();
                                xmlWriter.Close();
                            }
                            catch // Suppress response flushing errors
                            {
                            }
                        }
                    }
                }
            }
            catch (ThreadAbortException)
            {
                // TODO: figure out why is throwing this exception after response headers have been sent
                // Suppress for now
            }
            catch (Exception ex)
            {
                TraceHelper.Error(TraceCategory.ConciergeDocumentDownloader, ex.ToString(), ex, Guid.Empty, IdentityManager.GetUserAccountId());

                context.Session.Add("DocumentDownloadError", ex.Message);
                // context.Response.Redirect(@"~\ConciergeHome.aspx?errorCode=210", true);
            }
        }