Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement...
Stats
Group by Month/ Group by Week
T-SQL
  1. -- Group by month
  2. SELECT
  3.     YEAR([ImportDate]) iYear,
  4.     MONTH([ImportDate]) iMonth,
  5.     SUM([SourceItemCount]) ItemCount,
  6.     SUM([ImportItemCount]) ImportCount
  7. FROM [RssFromOrgImportLog] WITH (NOLOCK)
  8. GROUP BY YEAR([ImportDate]), MONTH([ImportDate])
  9. ORDER BY iYear DESC, iMonth DESC
  10.  
  11. -- Group by week
  12. SELECT
  13.     DATEADD(dd, (DATEDIFF(dd, -53690, [ImportDate])/7) * 7, -53690) iWeek,
  14.     SUM([SourceItemCount]) ItemCount,
  15.     SUM([ImportItemCount]) ImportCount
  16. FROM [RssFromOrgImportLog] WITH (NOLOCK)
  17. GROUP BY DATEADD(dd, (DATEDIFF(dd, -53690, [ImportDate])/7) * 7, -53690)
  18. ORDER BY iWeek DESC
Gzipped logger
  1. public class GZipLogger
  2. {
  3.     /// <summary>
  4.     /// GZip起始位元
  5.     /// </summary>
  6.     private static byte[] m_byGzHeader = new byte[] { 
  7.         31, 139, 8, 0, 0, 0, 0, 0, 4, 0, 237, 189, 7, 96, 28, 73, 150, 37, 38, 47, 109, 
  8.         202, 123, 127, 74, 245, 74, 215, 224, 116, 161, 8, 128, 96, 19, 36, 216, 144, 64, 
  9.         16, 236, 193, 136, 205, 230, 146, 236, 29, 105, 71, 35, 41, 171, 42, 129, 202, 101,
  10.         86, 101, 93, 102, 22, 64, 204, 237, 157, 188, 247, 222, 123, 239, 189, 247, 222,
  11.         123,239, 189, 247, 186, 59, 157, 78, 39, 247, 223, 255, 63, 92, 102, 100, 1, 108, 
  12.         246, 206, 74, 218, 201, 158, 33, 128, 170, 200, 31, 63, 126, 124, 31, 63, 34 
  13.     };
  14.     /// <summary>
  15.     /// 記錄Log
  16.     /// </summary>
  17.     /// <param name="strLogFileName">檔案位置</param>
  18.     /// <param name="strMessage">訊息</param>
  19.     public static void Log(string strLogFileName, string strMessage)
  20.     {
  21.         FileStream fsFileInput = new FileStream(strLogFileName, FileMode.Append);
  22.         GZipStream gzsFileInput = new GZipStream(fsFileInput, CompressionMode.Compress);
  23.         byte[] byBuffer = Encoding.UTF8.GetBytes(strMessage);
  24.         gzsFileInput.Write(byBuffer, 0, byBuffer.Length);
  25.         gzsFileInput.Close();
  26.         fsFileInput.Close();
  27.     }
  28.     /// <summary>
  29.     /// 讀取Log
  30.     /// </summary>
  31.     /// <param name="strLogFileName">Log檔位置</param>
  32.     /// <returns>Log內容</returns>
  33.     public static string Read(string strLogFileName)
  34.     {
  35.         if (String.IsNullOrEmpty(strLogFileName))
  36.             throw new ArgumentException("strLogFileName is null or empty.", "strLogFileName");
  37.  
  38.         StringBuilder sbTemp = new StringBuilder();
  39.  
  40.         #region 讀檔
  41.         FileStream fsInputFile = new FileStream(strLogFileName, FileMode.Open);
  42.         MemoryStream msInputFile = new MemoryStream();
  43.         int iReaded;
  44.         byte[] byBuffer = new byte[512];
  45.         while ((iReaded = fsInputFile.Read(byBuffer, 0, byBuffer.Length)) > 0)
  46.         {
  47.             msInputFile.Write(byBuffer, 0, iReaded);
  48.         }
  49.         fsInputFile.Close();
  50.         msInputFile.Close();
  51.         #endregion
  52.  
  53.         List<byte[]> lsSection = SplitBytes(msInputFile.ToArray());
  54.         foreach (byte[] bySection in lsSection)
  55.         {
  56.             sbTemp.Append(UnZip(bySection));
  57.         }
  58.         return sbTemp.ToString();
  59.     }
  60.     /// <summary>
  61.     /// 還原壓縮的GZip位元陣列為字串
  62.     /// </summary>
  63.     /// <param name="byBuffer">已壓縮的GZip位元陣列</param>
  64.     /// <returns>還原的字串</returns>
  65.     private static string UnZip(byte[] byBuffer)
  66.     {
  67.         if (byBuffer == null || byBuffer.Length == 0)
  68.             throw new ArgumentException("byBuffer is null or empty.", "byBuffer");
  69.  
  70.         MemoryStream msInput = new MemoryStream(byBuffer);
  71.         GZipStream gzsInput = new GZipStream(msInput, CompressionMode.Decompress);
  72.         StreamReader srInput = new StreamReader(gzsInput);
  73.         string strContent = srInput.ReadToEnd();
  74.         srInput.Close();
  75.         gzsInput.Close();
  76.         msInput.Close();
  77.         return strContent;
  78.     }
  79.     /// <summary>
  80.     /// 分割GZip位元陣列
  81.     /// </summary>
  82.     /// <param name="byBuffer">來源位元陣列</param>
  83.     /// <returns>分割結果</returns>
  84.     private static List<byte[]> SplitBytes(byte[] byBuffer)
  85.     {
  86.         if (byBuffer == null || byBuffer.Length == 0)
  87.             throw new ArgumentException("byBuffer is null or empty.", "byBuffer");
  88.  
  89.         #region 記錄每個區段起始點
  90.         List<int> lsBreaks = new List<int>();
  91.         int iStartIndex = 0;
  92.         while ((iStartIndex = IndexOf(byBuffer, iStartIndex)) > -1)
  93.         {
  94.             lsBreaks.Add(iStartIndex);
  95.             iStartIndex++;
  96.         }
  97.         lsBreaks.Add(byBuffer.Length);
  98.         #endregion
  99.  
  100.         #region 分割
  101.         List<byte[]> lsSection = new List<byte[]>();
  102.         for (int i = 0; i < lsBreaks.Count - 1; i++)
  103.         {
  104.             int iArrayLength = lsBreaks[+ 1] - lsBreaks[i];
  105.             byte[] byTemp = new byte[iArrayLength];
  106.             Array.Copy(byBuffer, lsBreaks[i], byTemp, 0, byTemp.Length);
  107.             lsSection.Add(byTemp);
  108.         }
  109.         #endregion
  110.  
  111.         return lsSection;
  112.     }
  113.     /// <summary>
  114.     /// 傳回下一個GZip位元陣列的起始位置
  115.     /// </summary>
  116.     /// <param name="byBuffer">來源位元陣列</param>
  117.     /// <param name="iStartIndex">起始位置,若找不到則傳回-1</param>
  118.     /// <returns></returns>
  119.     private static int IndexOf(byte[] byBuffer, int iStartIndex)
  120.     {
  121.         if (byBuffer == null || byBuffer.Length == 0)
  122.             throw new ArgumentException("byBuffer is null or empty.", "byBuffer");
  123.  
  124.         if (iStartIndex > -1 && (iStartIndex + m_byGzHeader.Length < byBuffer.Length))
  125.         {
  126.             for (int i = iStartIndex; i < byBuffer.Length; i++)
  127.             {
  128.                 // 起始位元相同, 且剩餘長度大於GZip標頭時
  129.                 if (byBuffer[i] == m_byGzHeader[0] && byBuffer.Length - i - 1 >= m_byGzHeader.Length)
  130.                 {
  131.                     #region 比對標頭
  132.                     byte[] temp = new byte[m_byGzHeader.Length];
  133.                     Array.Copy(byBuffer, i, temp, 0, temp.Length);
  134.                     if (CompareHeader(temp))
  135.                     {
  136.                         return i;
  137.                     }
  138.                     #endregion
  139.                 }
  140.             }
  141.         }
  142.         return -1;
  143.     }
  144.     /// <summary>
  145.     /// 比對起始的GZip位元
  146.     /// </summary>
  147.     /// <param name="byBuffer">來源位元陣列</param>
  148.     /// <returns>位元陣列相同為True, 反之為False</returns>
  149.     private static bool CompareHeader(byte[] byBuffer)
  150.     {
  151.         if (byBuffer == null || byBuffer.Length == 0)
  152.             throw new ArgumentException("byBuffer is null or empty.", "byBuffer");
  153.  
  154.         if (byBuffer.Length != m_byGzHeader.Length) return false;
  155.         for (int i = 0; i < byBuffer.Length; i++)
  156.         {
  157.             if (byBuffer[i] != m_byGzHeader[i]) return false;
  158.         }
  159.         return true;
  160.     }
  161.  
  162. }
Generic in .net
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace TestConsole
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Person p = new Person("Bruce", 30);
  14.  
  15.             Console.WriteLine("PrintXML : ");
  16.             PrintXML(p);
  17.  
  18.             Console.WriteLine("PrintTwo : ");
  19.             PrintTwo("Time", DateTime.Now);
  20.  
  21.             StringBuilder sb = new StringBuilder();
  22.             //p = CreateGenericNew(p);      // without public non-parameter constructor, not allowed
  23.             sb = CreateGenericNew(sb);      // it's ok
  24.             Console.WriteLine("Object is null : {0}", sb == null);
  25.  
  26.             p = CreateGenericDefault(p);    // it's ok
  27.             sb = CreateGenericDefault(sb);  // it's ok
  28.             Console.WriteLine("Object is null : {0}", sb == null);
  29.  
  30.             Console.Read();
  31.         }
  32.  
  33.         static void PrintXML<T>(T obj) where T : ISerializeXML
  34.         {
  35.             Console.WriteLine(obj.ToXML());
  36.         }
  37.  
  38.         static void PrintTwo<T, V>(T key, V value)
  39.         {
  40.             Console.WriteLine("{0}={1}", key, value);
  41.         }
  42.  
  43.         static T CreateGenericNew<T>(T obj) where T : new()
  44.         {
  45.             return new T();
  46.         }
  47.  
  48.         static T CreateGenericDefault<T>(T obj)
  49.         {
  50.             return default(T);
  51.         }
  52.     }
  53.  
  54.     interface ISerializeXML
  55.     {
  56.         string ToXML();
  57.     }
  58.  
  59.     class Person : ISerializeXML
  60.     {
  61.         public string Name { get; private set; }
  62.         public int Age { get; private set; }
  63.  
  64.         public Person(string name, int age)
  65.         {
  66.             this.Name = name;
  67.             this.Age = age;
  68.         }
  69.  
  70.         #region ISerializeXML 成員
  71.  
  72.         public string ToXML()
  73.         {
  74.             return string.Format(
  75.                 @"<person><name>{0}</name><age>{1}</age></person>",
  76.                 this.Name,
  77.                 this.Age
  78.             );
  79.         }
  80.  
  81.         #endregion
  82.  
  83.         public override string ToString()
  84.         {
  85.             return string.Format("{0},{1}", this.Name, this.Age);
  86.         }
  87.     }
  88. }
output:
  1. PrintXML : 
  2. <person><name>Bruce</name><age>30</age></person>
  3. PrintTwo : 
  4. Time=2010/4/16 00:46:28
  5. Object is null : False
  6. Object is null : True
see also:
http://msdn.microsoft.com/en-us/library/ms379564%28VS.80%29.aspx
http://proglab-justin.blogspot.com/2010/01/net-genericdefaulttnew-t_23.html