ConsoleEncoding.Buffers.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // --------------------------------------------------
  2. // UnityInjector - ConsoleEncoding.Buffers.cs
  3. // Copyright (c) Usagirei 2015 - 2015
  4. // --------------------------------------------------
  5. namespace UnityInjector.ConsoleUtil
  6. {
  7. // --------------------------------------------------
  8. // Code ported from
  9. // https://gist.github.com/asm256/9bfb88336a1433e2328a
  10. // Which in turn was seemingly ported from
  11. // http://jonskeet.uk/csharp/ebcdic/
  12. // using only safe (managed) code
  13. // --------------------------------------------------
  14. internal partial class ConsoleEncoding
  15. {
  16. private byte[] _byteBuffer = new byte[256];
  17. private char[] _charBuffer = new char[256];
  18. private byte[] _zeroByte = new byte[0];
  19. private char[] _zeroChar = new char[0];
  20. private void ExpandByteBuffer(int count)
  21. {
  22. if (_byteBuffer.Length < count)
  23. _byteBuffer = new byte[count];
  24. }
  25. private void ExpandCharBuffer(int count)
  26. {
  27. if (_charBuffer.Length < count)
  28. _charBuffer = new char[count];
  29. }
  30. private void ReadByteBuffer(byte[] bytes, int index, int count)
  31. {
  32. for (int i = 0; i < count; i++)
  33. bytes[index + i] = _byteBuffer[i];
  34. }
  35. private void ReadCharBuffer(char[] chars, int index, int count)
  36. {
  37. for (int i = 0; i < count; i++)
  38. chars[index + i] = _charBuffer[i];
  39. }
  40. private void WriteByteBuffer(byte[] bytes, int index, int count)
  41. {
  42. ExpandByteBuffer(count);
  43. for (int i = 0; i < count; i++)
  44. _byteBuffer[i] = bytes[index + i];
  45. }
  46. private void WriteCharBuffer(char[] chars, int index, int count)
  47. {
  48. ExpandCharBuffer(count);
  49. for (int i = 0; i < count; i++)
  50. _charBuffer[i] = chars[index + i];
  51. }
  52. }
  53. }