ByteReader.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Utilities
  5. {
  6. public class ByteReader
  7. {
  8. public static byte ReadByte(byte[] buffer, int offset)
  9. {
  10. return buffer[offset];
  11. }
  12. public static byte ReadByte(byte[] buffer, ref int offset)
  13. {
  14. offset++;
  15. return buffer[offset - 1];
  16. }
  17. public static byte[] ReadBytes(byte[] buffer, int offset, int length)
  18. {
  19. byte[] result = new byte[length];
  20. Array.Copy(buffer, offset, result, 0, length);
  21. return result;
  22. }
  23. public static byte[] ReadBytes(byte[] buffer, ref int offset, int length)
  24. {
  25. offset += length;
  26. return ReadBytes(buffer, offset - length, length);
  27. }
  28. /// <summary>
  29. /// Will return the ANSI string stored in the buffer
  30. /// </summary>
  31. public static string ReadAnsiString(byte[] buffer, int offset, int count)
  32. {
  33. // ASCIIEncoding.ASCII.GetString will convert some values to '?' (byte value of 63)
  34. // Any codepage will do, but the only one that Mono supports is 28591.
  35. return ASCIIEncoding.GetEncoding(28591).GetString(buffer, offset, count);
  36. }
  37. public static string ReadAnsiString(byte[] buffer, ref int offset, int count)
  38. {
  39. offset += count;
  40. return ReadAnsiString(buffer, offset - count, count);
  41. }
  42. public static string ReadUTF16String(byte[] buffer, int offset, int numberOfCharacters)
  43. {
  44. int numberOfBytes = numberOfCharacters * 2;
  45. return Encoding.Unicode.GetString(buffer, offset, numberOfBytes);
  46. }
  47. public static string ReadUTF16String(byte[] buffer, ref int offset, int numberOfCharacters)
  48. {
  49. int numberOfBytes = numberOfCharacters * 2;
  50. offset += numberOfBytes;
  51. return ReadUTF16String(buffer, offset - numberOfBytes, numberOfCharacters);
  52. }
  53. public static string ReadNullTerminatedAnsiString(byte[] buffer, int offset)
  54. {
  55. StringBuilder builder = new StringBuilder();
  56. char c = (char)ByteReader.ReadByte(buffer, offset);
  57. while (c != '\0')
  58. {
  59. builder.Append(c);
  60. offset++;
  61. c = (char)ByteReader.ReadByte(buffer, offset);
  62. }
  63. return builder.ToString();
  64. }
  65. public static string ReadNullTerminatedUTF16String(byte[] buffer, int offset)
  66. {
  67. StringBuilder builder = new StringBuilder();
  68. char c = (char)LittleEndianConverter.ToUInt16(buffer, offset);
  69. while (c != 0)
  70. {
  71. builder.Append(c);
  72. offset += 2;
  73. c = (char)LittleEndianConverter.ToUInt16(buffer, offset);
  74. }
  75. return builder.ToString();
  76. }
  77. public static string ReadNullTerminatedAnsiString(byte[] buffer, ref int offset)
  78. {
  79. string result = ReadNullTerminatedAnsiString(buffer, offset);
  80. offset += result.Length + 1;
  81. return result;
  82. }
  83. public static string ReadNullTerminatedUTF16String(byte[] buffer, ref int offset)
  84. {
  85. string result = ReadNullTerminatedUTF16String(buffer, offset);
  86. offset += result.Length * 2 + 2;
  87. return result;
  88. }
  89. public static byte[] ReadBytes(Stream stream, int count)
  90. {
  91. MemoryStream temp = new MemoryStream();
  92. ByteUtils.CopyStream(stream, temp, count);
  93. return temp.ToArray();
  94. }
  95. /// <summary>
  96. /// Return all bytes from current stream position to the end of the stream
  97. /// </summary>
  98. public static byte[] ReadAllBytes(Stream stream)
  99. {
  100. MemoryStream temp = new MemoryStream();
  101. ByteUtils.CopyStream(stream, temp);
  102. return temp.ToArray();
  103. }
  104. }
  105. }