Reduced buffer size while hashing to 16K
[pithos-ms-client] / trunk / Pithos.Network / WebExtensions.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Net;
6 using System.IO;
7 using log4net;
8
9 namespace Pithos.Network
10 {
11     public static class WebExtensions
12     {
13
14         public static string ReadToEnd(this HttpWebResponse response)
15         {
16             using (var stream = response.GetResponseStream())
17             {
18                 if (stream == null)
19                     return null;
20                 using (var reader = new StreamReader(stream))
21                 {
22                     var body = reader.ReadToEnd();
23                     return body;
24                 }
25             }
26         }
27     
28         public static void LogError(this ILog log,HttpWebResponse response)
29         {
30             if (log.IsDebugEnabled)
31             {
32                 if (response != null)
33                 {
34                     var body = response.ReadToEnd();
35                     log.ErrorFormat("Headers:\n{0}\nBody:{1}", response.Headers,body);
36                 }
37             }
38         }
39
40         public static TextReader GetLoggedReader(this Stream stream, ILog log)
41         {
42             var reader = new StreamReader(stream);
43             if (!log.IsDebugEnabled)
44                 return reader;
45             
46             using (reader)
47             {
48                 var body = reader.ReadToEnd();
49                 log.DebugFormat("JSON response: {0}", body);
50                 return new StringReader(body);
51             }
52         }
53
54
55         public static TOut NullSafe<TIn, TOut>(this TIn obj, Func<TIn, TOut> memberAction)
56         {
57             //Note we should not use obj != null because it can not test value types and also
58             //compiler has to lift the type to a nullable type for doing the comparision with null.
59             return (!EqualityComparer<TIn>.Default.Equals(obj, default(TIn))) ? memberAction(obj) : default(TOut);
60
61         }
62
63
64
65
66     }
67 }