PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Passbook.Sample.Web/Controllers/BinaryFormatter.cs

https://github.com/Comezon/dotnet-passbook
C# | 39 lines | 34 code | 5 blank | 0 comment | 1 complexity | 942770384222de1c6af3127cca5254fe MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http.Formatting;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Passbook.Sample.Web.Controllers
  8. {
  9. class BinaryFormatter : MediaTypeFormatter
  10. {
  11. public override bool CanReadType(Type type)
  12. {
  13. return true;
  14. }
  15. public override bool CanWriteType(Type type)
  16. {
  17. return type == typeof(Byte[]);
  18. }
  19. public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
  20. {
  21. throw new NotImplementedException();
  22. }
  23. public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, System.Net.TransportContext transportContext)
  24. {
  25. var task = Task.Factory.StartNew(() =>
  26. {
  27. var array = value as byte[];
  28. stream.Write(array, 0, array.Length);
  29. stream.Flush();
  30. });
  31. return task;
  32. }
  33. }
  34. }