Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

118 linhas
5.0 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System.Globalization;
  22. using System.IO;
  23. using System.Security;
  24. using FileStream = System.IO.FileStream;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. public static partial class File
  28. {
  29. #region ReadAllBytes
  30. /// <summary>Opens a binary file, reads the contents of the file into a byte array, and then closes the file.</summary>
  31. /// <param name="path">The file to open for reading.</param>
  32. /// <returns>A byte array containing the contents of the file.</returns>
  33. [SecurityCritical]
  34. public static byte[] ReadAllBytes(string path)
  35. {
  36. return ReadAllBytesCore(null, path, PathFormat.RelativePath);
  37. }
  38. /// <summary>[AlphaFS] Opens a binary file, reads the contents of the file into a byte array, and then closes the file.</summary>
  39. /// <param name="path">The file to open for reading.</param>
  40. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  41. /// <returns>A byte array containing the contents of the file.</returns>
  42. [SecurityCritical]
  43. public static byte[] ReadAllBytes(string path, PathFormat pathFormat)
  44. {
  45. return ReadAllBytesCore(null, path, pathFormat);
  46. }
  47. #region Transactional
  48. /// <summary>[AlphaFS] Opens a binary file, reads the contents of the file into a byte array, and then closes the file.</summary>
  49. /// <param name="transaction">The transaction.</param>
  50. /// <param name="path">The file to open for reading.</param>
  51. /// <returns>A byte array containing the contents of the file.</returns>
  52. [SecurityCritical]
  53. public static byte[] ReadAllBytesTransacted(KernelTransaction transaction, string path)
  54. {
  55. return ReadAllBytesCore(transaction, path, PathFormat.RelativePath);
  56. }
  57. /// <summary>[AlphaFS] Opens a binary file, reads the contents of the file into a byte array, and then closes the file.</summary>
  58. /// <param name="transaction">The transaction.</param>
  59. /// <param name="path">The file to open for reading.</param>
  60. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  61. /// <returns>A byte array containing the contents of the file.</returns>
  62. [SecurityCritical]
  63. public static byte[] ReadAllBytesTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  64. {
  65. return ReadAllBytesCore(transaction, path, pathFormat);
  66. }
  67. #endregion // Transacted
  68. #endregion // ReadAllBytes
  69. #region Internal Methods
  70. /// <summary>Opens a binary file, reads the contents of the file into a byte array, and then closes the file.</summary>
  71. /// <exception cref="IOException"/>
  72. /// <param name="transaction">The transaction.</param>
  73. /// <param name="path">The file to open for reading.</param>
  74. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  75. /// <returns>A byte array containing the contents of the file.</returns>
  76. [SecurityCritical]
  77. internal static byte[] ReadAllBytesCore(KernelTransaction transaction, string path, PathFormat pathFormat)
  78. {
  79. byte[] buffer;
  80. using (FileStream fs = OpenReadTransacted(transaction, path, pathFormat))
  81. {
  82. int offset = 0;
  83. long length = fs.Length;
  84. if (length > int.MaxValue)
  85. throw new IOException(string.Format(CultureInfo.CurrentCulture, "File larger than 2GB: [{0}]", path));
  86. int count = (int)length;
  87. buffer = new byte[count];
  88. while (count > 0)
  89. {
  90. int n = fs.Read(buffer, offset, count);
  91. if (n == 0)
  92. throw new IOException("Unexpected end of file found");
  93. offset += n;
  94. count -= n;
  95. }
  96. }
  97. return buffer;
  98. }
  99. #endregion // Internal Methods
  100. }
  101. }