DataStream.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //-----------------------------------------------------------------------------
  2. //
  3. // Copyright (c) Microsoft. All rights reserved.
  4. // This code is licensed under the Microsoft Public License.
  5. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  6. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  7. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  8. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  9. //
  10. //-----------------------------------------------------------------------------
  11. using System;
  12. using System.IO;
  13. namespace Microsoft.Cci.Pdb {
  14. internal class DataStream {
  15. internal DataStream() {
  16. }
  17. internal DataStream(int contentSize, BitAccess bits, int count) {
  18. this.contentSize = contentSize;
  19. if (count > 0) {
  20. this.pages = new int[count];
  21. bits.ReadInt32(this.pages);
  22. }
  23. }
  24. internal void Read(PdbReader reader, BitAccess bits) {
  25. bits.MinCapacity(contentSize);
  26. Read(reader, 0, bits.Buffer, 0, contentSize);
  27. }
  28. internal void Read(PdbReader reader, int position,
  29. byte[] bytes, int offset, int data) {
  30. if (position + data > contentSize) {
  31. throw new PdbException("DataStream can't read off end of stream. " +
  32. "(pos={0},siz={1})",
  33. position, data);
  34. }
  35. if (position == contentSize) {
  36. return;
  37. }
  38. int left = data;
  39. int page = position / reader.pageSize;
  40. int rema = position % reader.pageSize;
  41. // First get remained of first page.
  42. if (rema != 0) {
  43. int todo = reader.pageSize - rema;
  44. if (todo > left) {
  45. todo = left;
  46. }
  47. reader.Seek(pages[page], rema);
  48. reader.Read(bytes, offset, todo);
  49. offset += todo;
  50. left -= todo;
  51. page++;
  52. }
  53. // Now get the remaining pages.
  54. while (left > 0) {
  55. int todo = reader.pageSize;
  56. if (todo > left) {
  57. todo = left;
  58. }
  59. reader.Seek(pages[page], 0);
  60. reader.Read(bytes, offset, todo);
  61. offset += todo;
  62. left -= todo;
  63. page++;
  64. }
  65. }
  66. //private void AddPages(int page0, int count) {
  67. // if (pages == null) {
  68. // pages = new int[count];
  69. // for (int i = 0; i < count; i++) {
  70. // pages[i] = page0 + i;
  71. // }
  72. // } else {
  73. // int[] old = pages;
  74. // int used = old.Length;
  75. // pages = new int[used + count];
  76. // Array.Copy(old, pages, used);
  77. // for (int i = 0; i < count; i++) {
  78. // pages[used + i] = page0 + i;
  79. // }
  80. // }
  81. //}
  82. //internal int Pages {
  83. // get { return pages == null ? 0 : pages.Length; }
  84. //}
  85. internal int Length {
  86. get { return contentSize; }
  87. }
  88. //internal int GetPage(int index) {
  89. // return pages[index];
  90. //}
  91. internal int contentSize;
  92. internal int[] pages;
  93. }
  94. }