ByteWriter.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Utilities
  5. {
  6. public class ByteWriter
  7. {
  8. public static void WriteByte(byte[] buffer, int offset, byte value)
  9. {
  10. buffer[offset] = value;
  11. }
  12. public static void WriteByte(byte[] buffer, ref int offset, byte value)
  13. {
  14. buffer[offset] = value;
  15. offset += 1;
  16. }
  17. public static void WriteBytes(byte[] buffer, int offset, byte[] bytes)
  18. {
  19. WriteBytes(buffer, offset, bytes, bytes.Length);
  20. }
  21. public static void WriteBytes(byte[] buffer, ref int offset, byte[] bytes)
  22. {
  23. WriteBytes(buffer, offset, bytes);
  24. offset += bytes.Length;
  25. }
  26. public static void WriteBytes(byte[] buffer, int offset, byte[] bytes, int length)
  27. {
  28. Array.Copy(bytes, 0, buffer, offset, length);
  29. }
  30. public static void WriteBytes(byte[] buffer, ref int offset, byte[] bytes, int length)
  31. {
  32. Array.Copy(bytes, 0, buffer, offset, length);
  33. offset += length;
  34. }
  35. public static void WriteAnsiString(byte[] buffer, int offset, string value)
  36. {
  37. WriteAnsiString(buffer, offset, value, value.Length);
  38. }
  39. public static void WriteAnsiString(byte[] buffer, ref int offset, string value)
  40. {
  41. WriteAnsiString(buffer, ref offset, value, value.Length);
  42. }
  43. public static void WriteAnsiString(byte[] buffer, int offset, string value, int maximumLength)
  44. {
  45. byte[] bytes = ASCIIEncoding.GetEncoding(28591).GetBytes(value);
  46. Array.Copy(bytes, 0, buffer, offset, Math.Min(value.Length, maximumLength));
  47. }
  48. public static void WriteAnsiString(byte[] buffer, ref int offset, string value, int fieldLength)
  49. {
  50. WriteAnsiString(buffer, offset, value, fieldLength);
  51. offset += fieldLength;
  52. }
  53. public static void WriteUTF16String(byte[] buffer, int offset, string value)
  54. {
  55. WriteUTF16String(buffer, offset, value, value.Length);
  56. }
  57. public static void WriteUTF16String(byte[] buffer, ref int offset, string value)
  58. {
  59. WriteUTF16String(buffer, ref offset, value, value.Length);
  60. }
  61. public static void WriteUTF16String(byte[] buffer, int offset, string value, int maximumNumberOfCharacters)
  62. {
  63. byte[] bytes = UnicodeEncoding.Unicode.GetBytes(value);
  64. int maximumNumberOfBytes = Math.Min(value.Length, maximumNumberOfCharacters) * 2;
  65. Array.Copy(bytes, 0, buffer, offset, maximumNumberOfBytes);
  66. }
  67. public static void WriteUTF16String(byte[] buffer, ref int offset, string value, int numberOfCharacters)
  68. {
  69. WriteUTF16String(buffer, offset, value, numberOfCharacters);
  70. offset += numberOfCharacters * 2;
  71. }
  72. public static void WriteNullTerminatedAnsiString(byte[] buffer, int offset, string value)
  73. {
  74. WriteAnsiString(buffer, offset, value);
  75. WriteByte(buffer, offset + value.Length, 0x00);
  76. }
  77. public static void WriteNullTerminatedAnsiString(byte[] buffer, ref int offset, string value)
  78. {
  79. WriteNullTerminatedAnsiString(buffer, offset, value);
  80. offset += value.Length + 1;
  81. }
  82. public static void WriteNullTerminatedUTF16String(byte[] buffer, int offset, string value)
  83. {
  84. WriteUTF16String(buffer, offset, value);
  85. WriteBytes(buffer, offset + value.Length * 2, new byte[] { 0x00, 0x00 });
  86. }
  87. public static void WriteNullTerminatedUTF16String(byte[] buffer, ref int offset, string value)
  88. {
  89. WriteNullTerminatedUTF16String(buffer, offset, value);
  90. offset += value.Length * 2 + 2;
  91. }
  92. public static void WriteBytes(Stream stream, byte[] bytes)
  93. {
  94. stream.Write(bytes, 0, bytes.Length);
  95. }
  96. public static void WriteBytes(Stream stream, byte[] bytes, int count)
  97. {
  98. stream.Write(bytes, 0, count);
  99. }
  100. public static void WriteAnsiString(Stream stream, string value)
  101. {
  102. WriteAnsiString(stream, value, value.Length);
  103. }
  104. public static void WriteAnsiString(Stream stream, string value, int fieldLength)
  105. {
  106. byte[] bytes = ASCIIEncoding.GetEncoding(28591).GetBytes(value);
  107. stream.Write(bytes, 0, Math.Min(bytes.Length, fieldLength));
  108. if (bytes.Length < fieldLength)
  109. {
  110. byte[] zeroFill = new byte[fieldLength - bytes.Length];
  111. stream.Write(zeroFill, 0, zeroFill.Length);
  112. }
  113. }
  114. public static void WriteUTF8String(Stream stream, string value)
  115. {
  116. byte[] bytes = UnicodeEncoding.UTF8.GetBytes(value);
  117. stream.Write(bytes, 0, bytes.Length);
  118. }
  119. public static void WriteUTF16String(Stream stream, string value)
  120. {
  121. byte[] bytes = UnicodeEncoding.Unicode.GetBytes(value);
  122. stream.Write(bytes, 0, bytes.Length);
  123. }
  124. public static void WriteUTF16BEString(Stream stream, string value)
  125. {
  126. byte[] bytes = UnicodeEncoding.BigEndianUnicode.GetBytes(value);
  127. stream.Write(bytes, 0, bytes.Length);
  128. }
  129. }
  130. }