Example #1
0
        private static unsafe void TestEmpty(ILZ4Compressor compressor, ILZ4Decompressor decompressor)
        {
            var bytes = new byte[50];

            byte[] dst    = compressor.Compress(bytes);
            var    result = decompressor.Decompress(dst);
        }
Example #2
0
 /// <summary>
 /// Decompress the data, Offset and Length cannot be used
 /// </summary>
 /// <param name="data">The data to decompress</param>
 /// <param name="offset">CANNOT BE USED</param>
 /// <param name="length">CANNOT BE USED</param>
 /// <returns></returns>
 public override byte[] Decode(ref byte[] data, ref uint offset, ref uint length, ref PacketHeader packetHeader)
 {
     if (packetHeader.isCompressed)
     {
         byte[] deCompressed = Lz4Decompressor.Decompress(data);
         offset = 0;
         length = (uint)deCompressed.Length;
         return(deCompressed);
     }
     return(data);
 }
Example #3
0
 private static void TestUnknownSize(string directory, ILZ4Compressor compressor, ILZ4Decompressor decompressor)
 {
     foreach (var file in Directory.GetFiles(directory))
     {
         var bytes        = File.ReadAllBytes(file);
         var compressed   = compressor.Compress(bytes);
         var decompressed = decompressor.Decompress(compressed);
         if (!bytes.SequenceEqual(decompressed))
         {
             throw new Exception("Compressing/Decompressing " + file + " failed.");
         }
     }
 }
Example #4
0
        public void DecompressData()
        {
            if (decompressor != null && frameData != null && frameData.Length > compHeaderSize && encType == FrameEncodeType.Compressed)
            {
                encType = (FrameEncodeType)frameData[0];
                int frameSize = System.BitConverter.ToInt32(frameData, 1);

                byte[] decompressBuffer = new byte[frameSize];
                int    decompSize       = decompressor.Decompress(frameData, compHeaderSize, decompressBuffer, 0, frameData.Length - compHeaderSize);
                frameData = decompressBuffer;

                decompressBuffer = null;
            }
        }
Example #5
0
    public override ExtendedDataInput Decompress(ExtendedDataInput input, int length, int unitLength, int elementCount, int extra, bool isLittleEndian)
    {
        int offset = 8;

        byte[]           output = CreateColumnVector(elementCount, extra, unitLength, isLittleEndian, 0).array();
        ILZ4Decompressor t      = LZ4DecompressorFactory.CreateNew();

        while (length > 0)
        {
            int blockSize = input.readInt();
            if (blockSize < 0)
            {
                blockSize = blockSize & 2147483647;
            }
            length   -= sizeof(int);
            blockSize = Math.Min(blockSize, length);
            if (blockSize == 0)
            {
                break;
            }

            byte[] src   = new byte[blockSize];
            int    index = 0;
            while (index < blockSize)
            {
                index += ((BinaryReader)input).Read(src, index, blockSize - index);
            }
            byte[] ret = t.Decompress(src);
            if (offset + ret.Length > output.Length)
            {
                byte[] tmp = new byte[Math.Max(offset + ret.Length, output.Length) * 2];
                Array.Copy(output, tmp, offset);
                output = tmp;
            }
            Buffer.BlockCopy(ret, 0, output, offset, ret.Length);
            offset += ret.Length;
            length -= blockSize;
        }

        if (isLittleEndian)
        {
            return(new LittleEndianDataInputStream(new MemoryStream(output)));
        }

        return(new BigEndianDataInputStream(new MemoryStream(output)));
    }
Example #6
0
    /// <summary>
    /// 接收到消息
    /// </summary>
    void OnReceive(byte[] bytes, int length)
    {
        memStream.Seek(0, SeekOrigin.End);
        memStream.Write(bytes, 0, length);
        //Reset to beginning
        memStream.Seek(0, SeekOrigin.Begin);
        bool isFullMsg = false;
        int  totalLen  = 0;

        //这里要优化,协议HeadStruct已经解析完毕,下次就不需要解析了 memStream.Seek(0, 6);
        while (RemainingBytes() >= PROTOCOL_HEAD_LENGTH)
        {
            totalLen = (int)reader.ReadUInt32();
            int    messageLen = totalLen & PROTOCOL_HEAD_LENGTH_MASK;
            ushort mainId     = reader.ReadUInt16();
            if (RemainingBytes() >= messageLen)
            {
                isFullMsg = true;
                int    compressFlag = totalLen & PROTOCOL_HEAD_COMPRESS_MASK;
                byte[] data         = reader.ReadBytes(messageLen);
                if (0 != compressFlag)
                {
                    data = decompressor.Decompress(data);
                }
                NetworkManagerController.GetInstance().AddEvent(mainId, data);
            }
            else
            {
                //Back up the position two bytes
                memStream.Position = memStream.Position - PROTOCOL_HEAD_LENGTH;
                break;
            }
        }
        //Create a new stream with any leftover bytes
        if (isFullMsg)
        {
            byte[] leftover = reader.ReadBytes((int)RemainingBytes());
            memStream.SetLength(0);                 //Clear
            memStream.Write(leftover, 0, leftover.Length);
        }
    }
Example #7
0
 private static unsafe void TestEmpty(ILZ4Compressor compressor, ILZ4Decompressor decompressor)
 {
     var bytes = new byte[50];
     byte[] dst = compressor.Compress(bytes);
     var result = decompressor.Decompress(dst);
 }
Example #8
0
 private static void TestUnknownSize(string directory, ILZ4Compressor compressor, ILZ4Decompressor decompressor)
 {
     foreach (var file in Directory.GetFiles(directory))
     {
         var bytes = File.ReadAllBytes(file);
         var compressed = compressor.Compress(bytes);
         var decompressed = decompressor.Decompress(compressed);
         if (!bytes.SequenceEqual(decompressed))
             throw new Exception("Compressing/Decompressing " + file + " failed.");
     }
 }
Example #9
0
        public byte[] Decompress(MemoryStream input)
        {
            var bytes = input.ToArray();

            return(_decompressor.Decompress(bytes));
        }