BinaryStreamHandler.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace OpenSim.Framework.Servers
  6. {
  7. public delegate string BinaryMethod(byte[] data, string path, string param);
  8. public class BinaryStreamHandler : BaseStreamHandler
  9. {
  10. BinaryMethod m_method;
  11. override public byte[] Handle(string path, Stream request)
  12. {
  13. byte[] data = ReadFully(request);
  14. string param = GetParam(path);
  15. string responseString = m_method(data, path, param);
  16. return Encoding.UTF8.GetBytes(responseString);
  17. }
  18. public BinaryStreamHandler(string httpMethod, string path, BinaryMethod binaryMethod)
  19. : base(httpMethod, path)
  20. {
  21. m_method = binaryMethod;
  22. }
  23. private byte[] ReadFully(Stream stream)
  24. {
  25. byte[] buffer = new byte[32768];
  26. using (MemoryStream ms = new MemoryStream())
  27. {
  28. while (true)
  29. {
  30. int read = stream.Read(buffer, 0, buffer.Length);
  31. if (read <= 0)
  32. {
  33. return ms.ToArray();
  34. }
  35. ms.Write(buffer, 0, read);
  36. }
  37. }
  38. }
  39. }
  40. }