public void ExpandOnSingleSegment()
        {
            PooledMemoryStream stream = s_manager.GetStream();

            byte[] buffer = new byte[4];
            new Random(42).NextBytes(buffer);
            stream.Write(buffer);

            Assert.Equal(16, stream.Capacity);
            Assert.Equal(4, stream.Length);
            Assert.Equal(4, stream.Position);

            stream.SetLength(8);

            Assert.Equal(16, stream.Capacity);
            Assert.Equal(8, stream.Length);
            Assert.Equal(4, stream.Position);

            stream.Seek(0, SeekOrigin.Begin);

            byte[] buffer2 = new byte[8];
            Assert.Equal(8, stream.Read(buffer2));

            Assert.True(buffer2.AsSpan(0, 4).SequenceEqual(buffer.AsSpan(0, 4)));
            Assert.True(buffer2.AsSpan(4, 4).SequenceEqual(s_emptyBytes.AsSpan(0, 4)));
        }
        public static void PooledMemoryStream_Write_BeyondCapacity()
        {
            using (PooledMemoryStream memoryStream = new PooledMemoryStream())
            {
                long   origLength = memoryStream.Length;
                byte[] bytes      = new byte[10];
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)i;
                }
                int spanPastEnd = 5;
                memoryStream.Seek(spanPastEnd, SeekOrigin.End);

                // Test Write
                memoryStream.Write(bytes, 0, bytes.Length);
                long pos = memoryStream.Position;
                Assert.Equal(pos, origLength + spanPastEnd + bytes.Length);
                Assert.Equal(memoryStream.Length, origLength + spanPastEnd + bytes.Length);

                // Verify bytes were correct.
                memoryStream.Position = origLength;
                byte[] newData = new byte[bytes.Length + spanPastEnd];
                int    n       = memoryStream.Read(newData, 0, newData.Length);
                Assert.Equal(n, newData.Length);
                for (int i = 0; i < spanPastEnd; i++)
                {
                    Assert.Equal(0, newData[i]);
                }
                for (int i = 0; i < bytes.Length; i++)
                {
                    Assert.Equal(bytes[i], newData[i + spanPastEnd]);
                }
            }
        }
        public void ShrinkFromTwoSegment()
        {
            PooledMemoryStream stream = s_manager.GetStream();

            byte[] buffer = new byte[12];
            new Random(42).NextBytes(buffer);
            stream.Write(buffer);
            stream.Write(buffer);

            Assert.Equal(32, stream.Capacity);
            Assert.Equal(24, stream.Length);
            Assert.Equal(24, stream.Position);

            stream.SetLength(12);

            Assert.Equal(16, stream.Capacity);
            Assert.Equal(12, stream.Length);
            Assert.Equal(12, stream.Position);

            stream.Seek(0, SeekOrigin.Begin);

            byte[] buffer2 = new byte[12];
            Assert.Equal(12, stream.Read(buffer2));

            Assert.True(buffer2.AsSpan(0, 12).SequenceEqual(buffer.AsSpan(0, 12)));
        }
        protected virtual async Task <Image <Rgb24> > ReadTileImageAsync(int layer, int column, int row, CancellationToken cancellationToken)
        {
            using (PooledMemoryStream ms = PooledMemoryStreamManager.Shared.GetStream())
            {
                await CopyTileStreamAsync(layer, column, row, ms, cancellationToken).ConfigureAwait(false);

                ms.Seek(0, SeekOrigin.Begin);
                return(Image.Load <Rgb24>(_configuration, ms));
            }
        }
示例#5
0
        public void TestWriteMiddle()
        {
            var data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            using (var stm = new PooledMemoryStream(ArrayPool <byte> .Shared))
            {
                stm.Write(data, 0, data.Length);
                stm.Seek(4, SeekOrigin.Begin);
                stm.Write(data, 0, data.Length);
                var ar = stm.ToArray();
                Assert.Equal(data.Take(4).Concat(data), ar);
            }
        }
示例#6
0
        private static PooledMemoryStream CreateExpandableMemoryStream(byte[] bytes)
        {
            PooledMemoryStream pooledMemoryStream;

            if (bytes != null)
            {
                pooledMemoryStream = new PooledMemoryStream(bytes.Length);
                pooledMemoryStream.Write(bytes, 0, bytes.Length);
                pooledMemoryStream.Seek(0L, SeekOrigin.Begin);
            }
            else
            {
                pooledMemoryStream = new PooledMemoryStream(16384);
            }
            return(pooledMemoryStream);
        }
        public void SetLengthOnEmptyInstance()
        {
            PooledMemoryStream stream = s_manager.GetStream();

            stream.SetLength(10);

            Assert.Equal(10, stream.Length);
            Assert.Equal(16, stream.Capacity);
            Assert.Equal(0, stream.Position);

            byte[] buffer = new byte[11];
            new Random(42).NextBytes(buffer);
            Assert.Equal(10, stream.Read(buffer, 0, 10));
            Assert.True(buffer.AsSpan(0, 10).SequenceEqual(s_emptyBytes.AsSpan(0, 10)));

            stream.Seek(0, SeekOrigin.Begin);
            Assert.Equal(10, stream.Read(buffer, 0, 11));
        }
        public static void MemoryStream_WriteByte_BeyondCapacity()
        {
            using (PooledMemoryStream memoryStream = new PooledMemoryStream())
            {
                long   origLength = memoryStream.Length;
                byte[] bytes      = new byte[10];
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)i;
                }
                int spanPastEnd = 5;
                memoryStream.Seek(spanPastEnd, SeekOrigin.End);

                // Test WriteByte
                origLength            = memoryStream.Length;
                memoryStream.Position = memoryStream.Length + spanPastEnd;
                memoryStream.WriteByte(0x42);
                long expected = origLength + spanPastEnd + 1;
                Assert.Equal(expected, memoryStream.Position);
                Assert.Equal(expected, memoryStream.Length);
            }
        }
示例#9
0
        protected override void OnBeforeSave()
        {
            if (this.decodedItem == null && !base.AttachmentCollection.IsDirty && base.IsRestricted && (base.Recipients.IsDirty || base.IsPropertyDirty(ItemSchema.Sender)))
            {
                this.EnsureIsDecoded();
            }
            if (this.decodedItem != null)
            {
                string contentClass = base.TryGetProperty(InternalSchema.ContentClass) as string;
                if (this.rmsTemplate == null)
                {
                    this.UnprotectAllAttachments();
                    RightsManagedMessageItem.CopyProtectableData(this.decodedItem, this);
                    if (ObjectClass.IsRightsManagedContentClass(contentClass))
                    {
                        base.Delete(StoreObjectSchema.ContentClass);
                    }
                }
                else
                {
                    this.charsetDetectionStringForProtectedData = new StringBuilder((int)Math.Min(this.ProtectedBody.Size, 2147483647L));
                    this.GetCharsetDetectionStringFromProtectedData(this.charsetDetectionStringForProtectedData);
                    if (!ObjectClass.IsRightsManagedContentClass(contentClass))
                    {
                        this[StoreObjectSchema.ContentClass] = "rpmsg.message";
                    }
                    if (this.isSending)
                    {
                        byte[][] valueOrDefault = base.GetValueOrDefault <byte[][]>(InternalSchema.DRMLicense);
                        if (valueOrDefault != null && valueOrDefault.Length == RightsManagedMessageItem.EmptyDrmLicense.Length && valueOrDefault[0].Length == RightsManagedMessageItem.EmptyDrmLicense[0].Length)
                        {
                            base.DeleteProperties(new PropertyDefinition[]
                            {
                                InternalSchema.DRMLicense
                            });
                        }
                    }
                    else if (base.IsDraft && base.GetValueOrDefault <byte[][]>(InternalSchema.DRMLicense) == null)
                    {
                        this[InternalSchema.DRMLicense] = RightsManagedMessageItem.EmptyDrmLicense;
                    }
                    base.AttachmentCollection.RemoveAll();
                    using (StreamAttachment streamAttachment = base.AttachmentCollection.Create(AttachmentType.Stream) as StreamAttachment)
                    {
                        streamAttachment.FileName    = "message.rpmsg";
                        streamAttachment.ContentType = "application/x-microsoft-rpmsg-message";
                        using (Stream stream = new PooledMemoryStream(131072))
                        {
                            if (this.serverUseLicense == null || ((this.UsageRights & ContentRight.Owner) == ContentRight.Owner && this.rmsTemplate.RequiresRepublishingWhenRecipientsChange && this.CanRepublish && (base.Recipients.IsDirty || (base.IsPropertyDirty(ItemSchema.Sender) && this.conversationOwner == null))))
                            {
                                if (this.ConversationOwner == null)
                                {
                                    throw new InvalidOperationException("Conversation owner must be set before protecting the message.");
                                }
                                this.UnprotectAllAttachments();
                                using (MsgToRpMsgConverter msgToRpMsgConverter = new MsgToRpMsgConverter(this, this.ConversationOwner, this.orgId, this.rmsTemplate, this.options))
                                {
                                    msgToRpMsgConverter.Convert(this.decodedItem, stream);
                                    using (Stream stream2 = base.OpenPropertyStream(MessageItemSchema.DRMServerLicenseCompressed, PropertyOpenMode.Create))
                                    {
                                        DrmEmailCompression.CompressUseLicense(msgToRpMsgConverter.ServerUseLicense, stream2);
                                    }
                                    if (this.InternalSession != null && this.InternalSession.MailboxOwner.Sid != null)
                                    {
                                        ExDateTime useLicenseExpiryTime = RmsClientManagerUtils.GetUseLicenseExpiryTime(msgToRpMsgConverter.ServerUseLicense, this.UsageRights);
                                        this[MessageItemSchema.DRMRights]     = (int)this.UsageRights;
                                        this[MessageItemSchema.DRMExpiryTime] = useLicenseExpiryTime;
                                        using (RightsSignatureBuilder rightsSignatureBuilder = new RightsSignatureBuilder(msgToRpMsgConverter.ServerUseLicense, msgToRpMsgConverter.PublishLicense, RmsClientManager.EnvironmentHandle, msgToRpMsgConverter.LicensePair))
                                        {
                                            this[MessageItemSchema.DRMPropsSignature] = rightsSignatureBuilder.Sign(this.UsageRights, useLicenseExpiryTime, this.InternalSession.MailboxOwner.Sid);
                                        }
                                    }
                                    goto IL_362;
                                }
                            }
                            using (MsgToRpMsgConverter msgToRpMsgConverter2 = new MsgToRpMsgConverter(this, this.orgId, this.publishLicense, this.serverUseLicense, this.options))
                            {
                                msgToRpMsgConverter2.Convert(this.decodedItem, stream);
                            }
IL_362:
                            using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create))
                            {
                                stream.Seek(0L, SeekOrigin.Begin);
                                Util.StreamHandler.CopyStreamData(stream, contentStream);
                            }
                        }
                        bool flag = false;
                        foreach (AttachmentHandle handle in this.decodedItem.AttachmentCollection)
                        {
                            if (!CoreAttachmentCollection.IsInlineAttachment(handle))
                            {
                                flag = true;
                                break;
                            }
                        }
                        this[InternalSchema.AllAttachmentsHidden] = !flag;
                        streamAttachment.Save();
                    }
                }
                this.decodedItem.Dispose();
                this.decodedItem       = null;
                this.effectiveRights   = ContentRight.Owner;
                this.publishLicense    = null;
                this.restrictionInfo   = null;
                this.rmsTemplate       = null;
                this.serverUseLicense  = null;
                this.conversationOwner = null;
            }
            base.OnBeforeSave();
        }
示例#10
0
        public void WriteWithSeek(bool useSpan)
        {
            var manager = new NonePooledMemoryStreamManager(new PooledMemoryStreamOptions()
            {
                MinimumSegmentSize = 16
            });

            var ms = new MemoryStream();
            PooledMemoryStream stream = manager.GetStream();

            var writeStream = new BroadcastWriteStream(ms, stream);

            byte[] buffer = new byte[8];
            buffer.AsSpan().Fill(1);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 8));
            }
            else
            {
                writeStream.Write(buffer, 0, 8);
            }

            buffer.AsSpan().Fill(2);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 8));
            }
            else
            {
                writeStream.Write(buffer, 0, 8);
            }

            buffer.AsSpan().Fill(3);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 8));
            }
            else
            {
                writeStream.Write(buffer, 0, 8);
            }

            buffer.AsSpan().Fill(4);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 8));
            }
            else
            {
                writeStream.Write(buffer, 0, 8);
            }

            ms.Seek(24, SeekOrigin.Begin);
            stream.Seek(24, SeekOrigin.Begin);

            buffer.AsSpan().Fill(5);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 4));
            }
            else
            {
                writeStream.Write(buffer, 0, 4);
            }

            AssertHelper.StreamEqual(ms, stream);
        }