| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | internal class XmlBufferedByteStreamReader : XmlByteStreamReader |
| | | 12 | | { |
| | | 13 | | private ByteStreamBufferedMessageData _bufferedMessageData; |
| | | 14 | | private int _offset; |
| | | 15 | | |
| | 15 | 16 | | public XmlBufferedByteStreamReader(ByteStreamBufferedMessageData bufferedMessageData, XmlDictionaryReaderQuotas |
| | | 17 | | { |
| | | 18 | | Fx.Assert(bufferedMessageData != null, "bufferedMessageData is null"); |
| | 15 | 19 | | _bufferedMessageData = bufferedMessageData; |
| | 15 | 20 | | _bufferedMessageData.Open(); |
| | | 21 | | |
| | 15 | 22 | | _offset = bufferedMessageData.Buffer.Offset; |
| | 15 | 23 | | this.quotas = quotas; |
| | 15 | 24 | | position = ReaderPosition.None; |
| | 15 | 25 | | } |
| | | 26 | | |
| | | 27 | | protected override void OnClose() |
| | | 28 | | { |
| | 9 | 29 | | _bufferedMessageData.Close(); |
| | 9 | 30 | | _bufferedMessageData = null; |
| | 9 | 31 | | _offset = 0; |
| | 9 | 32 | | base.OnClose(); |
| | 9 | 33 | | } |
| | | 34 | | |
| | | 35 | | public override int ReadContentAsBase64(byte[] buffer, int index, int count) |
| | | 36 | | { |
| | 6 | 37 | | EnsureInContent(); |
| | 6 | 38 | | ByteStreamMessageUtility.EnsureByteBoundaries(buffer, index, count, true); |
| | | 39 | | |
| | 6 | 40 | | if (count == 0) |
| | | 41 | | { |
| | 0 | 42 | | return 0; |
| | | 43 | | } |
| | | 44 | | |
| | 6 | 45 | | int bytesToCopy = Math.Min(_bufferedMessageData.Buffer.Count - _offset, count); |
| | | 46 | | |
| | 6 | 47 | | if (bytesToCopy == 0) |
| | | 48 | | { |
| | 1 | 49 | | position = ReaderPosition.EndElement; |
| | 1 | 50 | | return 0; |
| | | 51 | | } |
| | | 52 | | |
| | 5 | 53 | | Buffer.BlockCopy(_bufferedMessageData.Buffer.Array, _offset, buffer, index, bytesToCopy); |
| | 5 | 54 | | _offset += bytesToCopy; |
| | | 55 | | |
| | 5 | 56 | | return bytesToCopy; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | protected override byte[] OnToByteArray() |
| | | 60 | | { |
| | 4 | 61 | | int bytesToCopy = _bufferedMessageData.Buffer.Count; |
| | 4 | 62 | | byte[] buffer = new byte[bytesToCopy]; |
| | 4 | 63 | | Buffer.BlockCopy(_bufferedMessageData.Buffer.Array, _bufferedMessageData.Buffer.Offset, buffer, 0, bytesToCo |
| | 4 | 64 | | return buffer; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | protected override Stream OnToStream() |
| | | 68 | | { |
| | 2 | 69 | | return _bufferedMessageData.ToStream(); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | public override bool TryGetBase64ContentLength(out int length) |
| | | 73 | | { |
| | 4 | 74 | | if (!IsClosed) |
| | | 75 | | { |
| | | 76 | | // in ByteStream encoder, we're not concerned about individual xml nodes |
| | | 77 | | // therefore we can just return the entire segment of the buffer we're using in this reader. |
| | 4 | 78 | | length = _bufferedMessageData.Buffer.Count; |
| | 4 | 79 | | return true; |
| | | 80 | | } |
| | 0 | 81 | | length = -1; |
| | 0 | 82 | | return false; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | } |