예제 #1
0
        public virtual async Task Storage_InputStreamThenOuputStreamProducesEquivalentData(Stream testData, PixelStorageOptions pixelStorageOptions, Stream image)
        {
            testData.Position = 0;
            var originalDataStream = new MemoryStream();
            await testData.CopyToAsync(originalDataStream);

            originalDataStream.Position = 0;

            var pixelDataStream = new MemoryStream();

            byte[] paddingBytes;
            using (var pixelWriterStream = new PixelStorageWriterStream(pixelDataStream, image, pixelStorageOptions, true))
            {
                paddingBytes = new byte[pixelWriterStream.BytesPerUnit - originalDataStream.Length % pixelWriterStream.BytesPerUnit];
                // Use an uneven write size to ensure remainder bytes are handled properly
                while (originalDataStream.Length != originalDataStream.Position)
                {
                    foreach (int writeSize in writeSizes)
                    {
                        byte[] buffer         = new byte[writeSize];
                        int    totalBytesRead = 0;
                        while (totalBytesRead < writeSize)
                        {
                            int bytesRead =
                                await
                                originalDataStream.ReadAsync(buffer, totalBytesRead,
                                                             buffer.Length - totalBytesRead);

                            if (bytesRead == 0)
                            {
                                break;
                            }
                            totalBytesRead += bytesRead;
                        }

                        await pixelWriterStream.WriteAsync(buffer, 0, totalBytesRead);
                    }
                }

                await pixelWriterStream.WriteAsync(paddingBytes, 0, paddingBytes.Length);
            }

            var decodedData = new MemoryStream();

            pixelDataStream.Position = 0;
            using (var imageReaderStream = new PixelStorageReaderStream(pixelDataStream, pixelStorageOptions, true))
            {
                await imageReaderStream.CopyToAsync(decodedData, 4096);
            }

            byte[] expectedData = originalDataStream.ToArray().Concat(paddingBytes).ToArray();
            var    actualData   = decodedData.ToArray();

            if (!expectedData.SequenceEqual(actualData))
            {
                CollectionAssert.AreEqual(expectedData, actualData);
                Assert.Fail();
            }
        }
예제 #2
0
        private async Task WriteEmbeddedImagePaddingAsync(PixelStorageWriterStream pixelStorageStream)
        {
            var embeddedImageDataStream = pixelStorageStream.EmbeddedImageDataStream;

            if (pixelStorageStream.BytesLeftInUnit != 0)
            {
                // Pad the final unit of the pixel storage stream
                // Cannot pad with embedded image data stream as the
                // stream will also be read from within the storage stream
                // advancing the image stream too far.
                await pixelStorageStream.WriteAsync(new byte[pixelStorageStream.BytesLeftInUnit], 0, pixelStorageStream.BytesLeftInUnit);
            }

            // Directly copy remaining image data to underlying image stream.
            await new EmbeddedImagePadding(embeddedImageDataStream, EncodingConfiguration.BufferSize)
            .PadDataAsync(pixelStorageStream.ImageFormatterStream);
        }