예제 #1
0
        private static string EncodeThroughStream(BinaryCodec codec, byte[] data)
        {
            using (var stringWriter = new StringWriter())
            {
                using (var codecStream = new BinaryEncoderStream(codec, stringWriter))
                {
                    var random = new Random(5);

                    int inputOffset    = 0;
                    int inputRemaining = data.Length;

                    while (inputRemaining > 0)
                    {
                        int inputToWrite = random.Next(1, inputRemaining);

                        codecStream.Write(data, inputOffset, inputToWrite);
                        inputOffset    += inputToWrite;
                        inputRemaining -= inputToWrite;
                    }

                    codecStream.Flush();
                }

                return(stringWriter.ToString());
            }
        }
예제 #2
0
        private static void StreamTest(BinaryCodec codec)
        {
            var random = new Random(10);

            byte[] inputData = new byte[10240];
            for (int iIndex = 0; iIndex < inputData.Length; iIndex++)
            {
                inputData[iIndex] = (byte)random.Next(256);
            }

            StringWriter outputWriter = new StringWriter();

            using (BinaryEncoderStream codecStream = new BinaryEncoderStream(codec, outputWriter))
            {
                int inputOffset = 0;
                int inputEnd    = 0;

                while (inputOffset < inputData.Length)
                {
                    inputEnd = Math.Min(inputData.Length, inputEnd + 12); // (byte)random.Next(1, 128));

                    codecStream.Write(inputData, inputOffset, inputEnd - inputOffset);
                    inputOffset = inputEnd;
                }
            }

            var encodedText  = outputWriter.ToString();
            var decodedBytes = Decode(codec, encodedText);
            var inputLength  = inputData.Length;

            if (decodedBytes.Length != inputLength)
            {
                Console.WriteLine($"{inputLength} - decoded {decodedBytes.Length} bytes");
                return;
            }

            for (int iIndex = 0; iIndex < inputLength; iIndex++)
            {
                if (decodedBytes[iIndex] != inputData[iIndex])
                {
                    Console.WriteLine($"{inputLength} - incorrect byte at {iIndex}");
                    return;
                }
            }
        }