aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Servers/BinaryStreamHandler.cs
blob: 7d4e4ce19960a239c933e0182d9443f5c663014c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace OpenSim.Framework.Servers
{
    public delegate string BinaryMethod(byte[] data, string path, string param);

    public class BinaryStreamHandler : BaseStreamHandler
    {
        BinaryMethod m_method;

        override public byte[] Handle(string path, Stream request)
        {
            byte[] data = ReadFully(request);
            string param = GetParam(path);
            string responseString = m_method(data, path, param);
            
            return Encoding.UTF8.GetBytes(responseString);
        }

        public BinaryStreamHandler(string httpMethod, string path, BinaryMethod binaryMethod)
            : base(httpMethod, path)
        {
            m_method = binaryMethod;
        }

        private byte[] ReadFully(Stream stream)
        {
            byte[] buffer = new byte[32768];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);

                    if (read <= 0)
                    {
                        return ms.ToArray();
                    }
                    
                    ms.Write(buffer, 0, read);
                }
            }           
        }
    }

}