示例#1
0
文件: Buffer.cs 项目: slagusev/api
 private static void OutputBuffer(ByteBuffer byteBuffer, Array dst, Type dstType, int dstStartIndex, int dstIndexLength)
 {
     if (dstType == typeof(bool))
     {
         for (var i = 0; i < dstIndexLength; i++)
         {
             dst[dstStartIndex + i] = byteBuffer.Get(i) == 1;
         }
     }
     else if (dstType == typeof(byte))
     {
         //nothing to do, the dst array is wrapped already.
     }
     else if (dstType == typeof(char))
     {
         var charBuffer = byteBuffer.AsCharBuffer();
         charBuffer.Get((char[])dst, dstStartIndex, dstIndexLength);
     }
     else if (dstType == typeof(short))
     {
         var shortBuffer = byteBuffer.AsShortBuffer();
         shortBuffer.Get((short[])dst, dstStartIndex, dstIndexLength);
     }
     else if (dstType == typeof(float))
     {
         var floatBuffer = byteBuffer.AsFloatBuffer();
         floatBuffer.Get((float[])dst, dstStartIndex, dstIndexLength);
     }
     else if (dstType == typeof(int))
     {
         var intBuffer = byteBuffer.AsIntBuffer();
         intBuffer.Get((int[])dst, dstStartIndex, dstIndexLength);
     }
     else if (dstType == typeof(double))
     {
         var doubleBuffer = byteBuffer.AsDoubleBuffer();
         doubleBuffer.Get((double[])dst, dstStartIndex, dstIndexLength);
     }
     else if (dstType == typeof(long))
     {
         var longBuffer = byteBuffer.AsLongBuffer();
         longBuffer.Get((long[])dst, dstStartIndex, dstIndexLength);
     }
     else
     {
         throw new NotImplementedException("System.Buffer.OutputBuffer");
     }
 }
示例#2
0
文件: Buffer.cs 项目: slagusev/api
        private static ByteBuffer CreateByteBuffer(Array src, Type srcType, int srcStartIndex, int srcIndexLength, Array dst, Type dstType, int dstStartIndex, int dstIndexLength)
        {
            ByteBuffer buffer = null;

            if (dstType == typeof(byte))
            {
                buffer = ByteBuffer.Wrap((byte[])dst, dstStartIndex, dstIndexLength);
            }
            else if (srcType == typeof(byte))
            {
                buffer = ByteBuffer.Wrap((byte[])src, srcStartIndex, srcIndexLength);
            }
            else
            {
                var numberOfBytes = src.Length * NumberOfBytesForPrimitiveType(srcType);
                buffer = ByteBuffer.Allocate(numberOfBytes);
            }

            buffer.Order(ByteOrder.NativeOrder());

            if (srcType == typeof(byte))
            {
                if (dstType == typeof(byte))
                {
                    //both source and dest are bytes, we wrapped the dest, so copy the source into it (for as many bytes as needed).
                    buffer.Put((byte[])src, srcStartIndex, srcIndexLength);
                }
            }
            else if (srcType == typeof(bool))
            {
                for (var i = 0; i < srcIndexLength; i++)
                {
                    if ((bool)src[srcStartIndex + i])
                    {
                        buffer.Put((byte)1);
                    }
                    else
                    {
                        buffer.Put((byte)0);
                    }
                }
            }
            else if (srcType == typeof(char))
            {
                var charBuffer = buffer.AsCharBuffer();
                charBuffer.Put((char[])src, srcStartIndex, srcIndexLength);
            }
            else if (srcType == typeof(short))
            {
                var shortBuffer = buffer.AsShortBuffer();
                shortBuffer.Put((short[])src, srcStartIndex, srcIndexLength);
            }
            else if (srcType == typeof(float))
            {
                var floatBuffer = buffer.AsFloatBuffer();
                floatBuffer.Put((float[])src, srcStartIndex, srcIndexLength);
            }
            else if (srcType == typeof(int))
            {
                var intBuffer = buffer.AsIntBuffer();
                intBuffer.Put((int[])src, srcStartIndex, srcIndexLength);
            }
            else if (srcType == typeof(double))
            {
                var doubleBuffer = buffer.AsDoubleBuffer();
                doubleBuffer.Put((double[])src, srcStartIndex, srcIndexLength);
            }
            else if (srcType == typeof(long))
            {
                var longBuffer = buffer.AsLongBuffer();
                longBuffer.Put((long[])src, srcStartIndex, srcIndexLength);
            }
            else
            {
                throw new NotImplementedException("System.Buffer.CreateByteBuffer");
            }

            return(buffer);
        }