XmlBuffer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. #define BINARY
  5. namespace System.ServiceModel
  6. {
  7. using System.Collections.Generic;
  8. using System.Runtime;
  9. using System.ServiceModel.Channels;
  10. using System.Xml;
  11. class XmlBuffer
  12. {
  13. List<Section> sections;
  14. byte[] buffer;
  15. int offset;
  16. BufferedOutputStream stream;
  17. BufferState bufferState;
  18. XmlDictionaryWriter writer;
  19. XmlDictionaryReaderQuotas quotas;
  20. enum BufferState
  21. {
  22. Created,
  23. Writing,
  24. Reading,
  25. }
  26. struct Section
  27. {
  28. int offset;
  29. int size;
  30. XmlDictionaryReaderQuotas quotas;
  31. public Section(int offset, int size, XmlDictionaryReaderQuotas quotas)
  32. {
  33. this.offset = offset;
  34. this.size = size;
  35. this.quotas = quotas;
  36. }
  37. public int Offset
  38. {
  39. get { return this.offset; }
  40. }
  41. public int Size
  42. {
  43. get { return this.size; }
  44. }
  45. public XmlDictionaryReaderQuotas Quotas
  46. {
  47. get { return this.quotas; }
  48. }
  49. }
  50. public XmlBuffer(int maxBufferSize)
  51. {
  52. if (maxBufferSize < 0)
  53. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxBufferSize", maxBufferSize,
  54. SR.GetString(SR.ValueMustBeNonNegative)));
  55. int initialBufferSize = Math.Min(512, maxBufferSize);
  56. stream = new BufferManagerOutputStream(SR.XmlBufferQuotaExceeded, initialBufferSize, maxBufferSize,
  57. BufferManager.CreateBufferManager(0, int.MaxValue));
  58. sections = new List<Section>(1);
  59. }
  60. public int BufferSize
  61. {
  62. get
  63. {
  64. Fx.Assert(bufferState == BufferState.Reading, "Buffer size shuold only be retrieved during Reading state");
  65. return buffer.Length;
  66. }
  67. }
  68. public int SectionCount
  69. {
  70. get { return this.sections.Count; }
  71. }
  72. public XmlDictionaryWriter OpenSection(XmlDictionaryReaderQuotas quotas)
  73. {
  74. if (bufferState != BufferState.Created)
  75. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
  76. bufferState = BufferState.Writing;
  77. this.quotas = new XmlDictionaryReaderQuotas();
  78. quotas.CopyTo(this.quotas);
  79. if (this.writer == null)
  80. {
  81. this.writer = XmlDictionaryWriter.CreateBinaryWriter(stream, XD.Dictionary, null, true);
  82. }
  83. else
  84. {
  85. ((IXmlBinaryWriterInitializer)this.writer).SetOutput(stream, XD.Dictionary, null, true);
  86. }
  87. return this.writer;
  88. }
  89. public void CloseSection()
  90. {
  91. if (bufferState != BufferState.Writing)
  92. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
  93. this.writer.Close();
  94. bufferState = BufferState.Created;
  95. int size = (int)stream.Length - offset;
  96. sections.Add(new Section(offset, size, this.quotas));
  97. offset += size;
  98. }
  99. public void Close()
  100. {
  101. if (bufferState != BufferState.Created)
  102. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
  103. bufferState = BufferState.Reading;
  104. int bufferSize;
  105. buffer = stream.ToArray(out bufferSize);
  106. writer = null;
  107. stream = null;
  108. }
  109. Exception CreateInvalidStateException()
  110. {
  111. return new InvalidOperationException(SR.GetString(SR.XmlBufferInInvalidState));
  112. }
  113. public XmlDictionaryReader GetReader(int sectionIndex)
  114. {
  115. if (bufferState != BufferState.Reading)
  116. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
  117. Section section = sections[sectionIndex];
  118. XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(buffer, section.Offset, section.Size, XD.Dictionary, section.Quotas, null, null);
  119. reader.MoveToContent();
  120. return reader;
  121. }
  122. public void WriteTo(int sectionIndex, XmlWriter writer)
  123. {
  124. if (bufferState != BufferState.Reading)
  125. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
  126. XmlDictionaryReader reader = GetReader(sectionIndex);
  127. try
  128. {
  129. writer.WriteNode(reader, false);
  130. }
  131. finally
  132. {
  133. reader.Close();
  134. }
  135. }
  136. }
  137. }