reduce the size of messages, the domain system utilizes a compression scheme which eliminates the repetition of domain names in a message - CSharp System.Net

CSharp examples for System.Net:IP Address

Description

reduce the size of messages, the domain system utilizes a compression scheme which eliminates the repetition of domain names in a message

Demo Code

// Copyright (c) Microsoft. All rights reserved.
using System.Text;
using System.Collections.Generic;
using System;/*from  www.ja  v a  2 s.com*/

public class Main{
        /// <summary>
        /// In order to reduce the size of messages, the domain system utilizes a
        /// compression scheme which eliminates the repetition of domain names in a
        /// message.  In this scheme, an entire domain name or a list of labels at
        /// the end of a domain name is replaced with a pointer to a prior occurrence
        /// of the same name.
        /// </summary>
        /// <param name="domainName">A domain name. Labels are separated by dot.</param>
        /// <returns>The compressed UTF8 string.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when domainName is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Thrown when one label of domainName exceeded max length limit (63 bytes).
        /// </exception>
        public static byte[] ToCompressedUtf8String(string domainName)
        {
            if (domainName == null)
            {
                throw new ArgumentNullException("domainName");
            }

            const int MAX_LABEL_LENGTH = 63;

            List<byte> buf = new List<byte>();
            string[] labels = domainName.Split('.');
            for (int i = 0; i < labels.Length; i++)
            {
                byte[] labelBytes = Encoding.UTF8.GetBytes(labels[i]);
                if (labelBytes.Length > MAX_LABEL_LENGTH)
                {
                    throw new ArgumentException("Label exceeded max length.", "domainName");
                }
                buf.Add((byte)labelBytes.Length);
                buf.AddRange(labelBytes);
            }

            buf.Add(0);

            return buf.ToArray();
        }
}

Related Tutorials