SelectObjectResult.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using COSXML.CosException;
  6. using COSXML.Common;
  7. using System.Xml;
  8. namespace COSXML.Model.Object
  9. {
  10. public sealed class SelectObjectResult : CosResult
  11. {
  12. public String searchContent;
  13. public String outputFilePath;
  14. public Stat stat;
  15. private COSXML.Callback.OnProgressCallback progressCallback;
  16. public sealed class Stat
  17. {
  18. public long BytesScanned;
  19. public long BytesProcessed;
  20. public long BytesReturned;
  21. public override string ToString()
  22. {
  23. return string.Format("BytesScanned:{0}, BytesProcessed:{1}, BytesReturned:{2}",
  24. BytesScanned, BytesProcessed, BytesReturned);
  25. }
  26. }
  27. internal override void ExternInfo(CosRequest cosRequest)
  28. {
  29. base.ExternInfo(cosRequest);
  30. this.outputFilePath = ((SelectObjectRequest)cosRequest).outputFilePath;
  31. this.progressCallback = ((SelectObjectRequest)cosRequest).progressCallback;
  32. }
  33. internal override void ParseResponseBody(System.IO.Stream inputStream, string contentType, long contentLength)
  34. {
  35. // Read One Message for each loop
  36. // readToString(inputStream);
  37. System.IO.Stream outputStream;
  38. if (outputFilePath != null)
  39. {
  40. outputStream = new System.IO.FileStream(outputFilePath, FileMode.Create);
  41. }
  42. else
  43. {
  44. outputStream = new MemoryStream();
  45. }
  46. using (outputStream)
  47. {
  48. byte[] tempBuffer = new byte[4];
  49. while (true)
  50. {
  51. TryRead(inputStream, tempBuffer, 0, 4);
  52. long messageEntireLength = Bytes4ToInt(tempBuffer);
  53. TryRead(inputStream, tempBuffer, 0, 4);
  54. long headerSectionLength = Bytes4ToInt(tempBuffer);
  55. TryRead(inputStream, tempBuffer, 0, 4);
  56. long preludeCRC = Bytes4ToInt(tempBuffer);
  57. Dictionary<String, String> headers = new Dictionary<string, string>();
  58. // read header
  59. long headerSectionRemainLength = headerSectionLength;
  60. while (headerSectionRemainLength > 0)
  61. {
  62. TryRead(inputStream, tempBuffer, 0, 1);
  63. int headerNameLength = Bytes1ToInt(tempBuffer);
  64. byte[] headerNameBuffer = new byte[headerNameLength];
  65. TryRead(inputStream, headerNameBuffer, 0, headerNameLength);
  66. String headerName = Bytes2stringUTF8(headerNameBuffer);
  67. // 7
  68. inputStream.ReadByte();
  69. TryRead(inputStream, tempBuffer, 0, 2);
  70. int valueLength = Bytes2ToInt(tempBuffer);
  71. byte[] valueBuffer = new byte[valueLength];
  72. TryRead(inputStream, valueBuffer, 0, valueLength);
  73. String value = Bytes2stringUTF8(valueBuffer);
  74. if (headers.ContainsKey(headerName))
  75. {
  76. headers.Remove(headerName);
  77. }
  78. headers.Add(headerName, value);
  79. headerSectionRemainLength -= 1 + headerNameLength + 3 + valueLength;
  80. }
  81. long payloadLength = messageEntireLength - headerSectionLength - 16;
  82. string messageType;
  83. headers.TryGetValue(":message-type", out messageType);
  84. string eventType;
  85. headers.TryGetValue(":event-type", out eventType);
  86. // Console.WriteLine("message = " + messageType + ", event = " + eventType);
  87. bool isComplete = false;
  88. if ("event".Equals(messageType))
  89. {
  90. byte[] buffer;
  91. switch (eventType)
  92. {
  93. case "Records":
  94. int totalRead = 0;
  95. buffer = new byte[1024];
  96. while (payloadLength > totalRead)
  97. {
  98. int readLength = (int)Math.Min(payloadLength - totalRead, 1024);
  99. int readBytes = TryRead(inputStream, buffer, 0, readLength);
  100. outputStream.Write(buffer, 0, readBytes);
  101. totalRead += readBytes;
  102. }
  103. break;
  104. case "Progress":
  105. buffer = new byte[payloadLength];
  106. TryRead(inputStream, buffer, 0, (int)payloadLength);
  107. Stat stat = ParseStatsBody(buffer);
  108. progressCallback(stat.BytesProcessed, stat.BytesScanned);
  109. break;
  110. case "Cont":
  111. buffer = new byte[payloadLength];
  112. TryRead(inputStream, buffer, 0, (int)payloadLength);
  113. break;
  114. case "Stats":
  115. buffer = new byte[payloadLength];
  116. TryRead(inputStream, buffer, 0, (int)payloadLength);
  117. this.stat = ParseStatsBody(buffer);
  118. break;
  119. case "End":
  120. default:
  121. isComplete = true;
  122. break;
  123. }
  124. }
  125. else
  126. if ("error".Equals(messageType))
  127. {
  128. string errorCode = null;
  129. string errorMessage = null;
  130. headers.TryGetValue(":error-code", out errorCode);
  131. headers.TryGetValue(":error-message", out errorMessage);
  132. throw new System.IO.IOException(string.Format(
  133. "search error happends with code :{0} and message: {1}",
  134. errorCode, errorMessage));
  135. }
  136. if (isComplete)
  137. {
  138. if (outputFilePath == null)
  139. {
  140. outputStream.Position = 0;
  141. searchContent = ReadToString(outputStream);
  142. }
  143. break;
  144. }
  145. TryRead(inputStream, tempBuffer, 0, 4);
  146. long messageCRC = Bytes4ToInt(tempBuffer);
  147. }
  148. }
  149. }
  150. private int TryRead(System.IO.Stream inputStream, byte[] buffer, int offset, int count)
  151. {
  152. int read = 0;
  153. int maxRead = 10;
  154. int remainReadCount = count;
  155. while (remainReadCount > 0 && maxRead > 0 && inputStream.CanRead)
  156. {
  157. maxRead--;
  158. read = inputStream.Read(buffer, count - remainReadCount, remainReadCount);
  159. remainReadCount -= read;
  160. }
  161. if (remainReadCount > 0)
  162. {
  163. throw new System.IO.IOException("input stream is end unexpectly !");
  164. }
  165. return count - remainReadCount;
  166. }
  167. private Stat ParseStatsBody(byte[] body)
  168. {
  169. XmlReader xmlReader = XmlReader.Create(new MemoryStream(body));
  170. Stat stat = new Stat();
  171. try
  172. {
  173. while (xmlReader.Read())
  174. {
  175. switch (xmlReader.NodeType)
  176. {
  177. case XmlNodeType.Element:
  178. if ("BytesScanned".Equals(xmlReader.Name, StringComparison.OrdinalIgnoreCase))
  179. {
  180. xmlReader.Read();
  181. stat.BytesScanned = Convert.ToInt64(xmlReader.Value);
  182. }
  183. else
  184. if ("BytesProcessed".Equals(xmlReader.Name, StringComparison.OrdinalIgnoreCase))
  185. {
  186. xmlReader.Read();
  187. stat.BytesProcessed = Convert.ToInt64(xmlReader.Value);
  188. }
  189. else
  190. if ("BytesReturned".Equals(xmlReader.Name, StringComparison.OrdinalIgnoreCase))
  191. {
  192. xmlReader.Read();
  193. stat.BytesReturned = Convert.ToInt64(xmlReader.Value);
  194. }
  195. break;
  196. }
  197. }
  198. }
  199. catch (XmlException e)
  200. {
  201. Console.WriteLine(e.StackTrace);
  202. }
  203. return stat;
  204. }
  205. private string ReadToString(System.IO.Stream inputStream)
  206. {
  207. string content = null;
  208. using (StreamReader reader = new StreamReader(inputStream))
  209. {
  210. content = reader.ReadToEnd();
  211. }
  212. return content;
  213. }
  214. private string Bytes2stringUTF8(byte[] data)
  215. {
  216. return System.Text.Encoding.UTF8.GetString(data);
  217. }
  218. private int Bytes2ToInt(byte[] data)
  219. {
  220. return ((data[0] & 0xFF) << 8) | (data[1] & 0xFF);
  221. }
  222. private int Bytes1ToInt(byte[] data)
  223. {
  224. return (data[0] & 0xFF);
  225. }
  226. private long Bytes4ToInt(byte[] data)
  227. {
  228. return ((data[0] & 0xFF) << 24) | ((data[1] & 0xFF) << 16)
  229. | ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
  230. }
  231. }
  232. }