using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TEAMModelOS.SDK.Helper.Common.StringHelper
{
    public class BufferHelper
    {
        public static byte[] BufferMerge(byte[] first, byte[] last) {
            int len = first.Length + last.Length;
            byte[] lenArr = BitConverter.GetBytes(len);
            if (BitConverter.IsLittleEndian) {
                Array.Reverse(lenArr);
            }
            byte[] resArr = new byte[lenArr.Length + first.Length + last.Length];
            lenArr.CopyTo(resArr, 0);
            first.CopyTo(resArr, lenArr.Length);
            last.CopyTo(resArr, first.Length);
            return resArr;
        }

        public static Stream StreamMerge(Stream streamA, Stream streamB) {
            byte[] bufferA = new byte[streamA.Length];
            byte[] bufferB = new byte[streamB.Length];
            streamA.Read(bufferA, 0, bufferA.Length);
            streamA.Seek(0, SeekOrigin.Begin);
            streamB.Read(bufferB, 0, bufferB.Length);
            streamB.Seek(0, SeekOrigin.Begin);
            byte [] buffer= BufferMerge(bufferA, bufferB);
            Stream stream = new MemoryStream(buffer);
            return stream;

        }
    }
}
