DataStream.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. using System;
  4. using System.IO;
  5. namespace Microsoft.Cci.Pdb {
  6. internal class DataStream {
  7. internal DataStream() {
  8. }
  9. internal DataStream(int contentSize, BitAccess bits, int count) {
  10. this.contentSize = contentSize;
  11. if (count > 0) {
  12. this.pages = new int[count];
  13. bits.ReadInt32(this.pages);
  14. }
  15. }
  16. internal void Read(PdbReader reader, BitAccess bits) {
  17. bits.MinCapacity(contentSize);
  18. Read(reader, 0, bits.Buffer, 0, contentSize);
  19. }
  20. internal void Read(PdbReader reader, int position,
  21. byte[] bytes, int offset, int data) {
  22. if (position + data > contentSize) {
  23. throw new PdbException("DataStream can't read off end of stream. " +
  24. "(pos={0},siz={1})",
  25. position, data);
  26. }
  27. if (position == contentSize) {
  28. return;
  29. }
  30. int left = data;
  31. int page = position / reader.pageSize;
  32. int rema = position % reader.pageSize;
  33. // First get remained of first page.
  34. if (rema != 0) {
  35. int todo = reader.pageSize - rema;
  36. if (todo > left) {
  37. todo = left;
  38. }
  39. reader.Seek(pages[page], rema);
  40. reader.Read(bytes, offset, todo);
  41. offset += todo;
  42. left -= todo;
  43. page++;
  44. }
  45. // Now get the remaining pages.
  46. while (left > 0) {
  47. int todo = reader.pageSize;
  48. if (todo > left) {
  49. todo = left;
  50. }
  51. reader.Seek(pages[page], 0);
  52. reader.Read(bytes, offset, todo);
  53. offset += todo;
  54. left -= todo;
  55. page++;
  56. }
  57. }
  58. //private void AddPages(int page0, int count) {
  59. // if (pages == null) {
  60. // pages = new int[count];
  61. // for (int i = 0; i < count; i++) {
  62. // pages[i] = page0 + i;
  63. // }
  64. // } else {
  65. // int[] old = pages;
  66. // int used = old.Length;
  67. // pages = new int[used + count];
  68. // Array.Copy(old, pages, used);
  69. // for (int i = 0; i < count; i++) {
  70. // pages[used + i] = page0 + i;
  71. // }
  72. // }
  73. //}
  74. //internal int Pages {
  75. // get { return pages == null ? 0 : pages.Length; }
  76. //}
  77. internal int Length {
  78. get { return contentSize; }
  79. }
  80. //internal int GetPage(int index) {
  81. // return pages[index];
  82. //}
  83. internal int contentSize;
  84. internal int[] pages;
  85. }
  86. }