RandomAccessFile.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.IO;
  3. namespace SharpCifs.Util.Sharpen
  4. {
  5. public class RandomAccessFile
  6. {
  7. private FileStream _stream;
  8. public RandomAccessFile (FilePath file, string mode) : this(file.GetPath (), mode)
  9. {
  10. }
  11. public RandomAccessFile (string file, string mode)
  12. {
  13. if (mode.IndexOf ('w') != -1)
  14. _stream = new FileStream (file, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  15. else
  16. _stream = new FileStream (file, FileMode.Open, FileAccess.Read);
  17. }
  18. public void Close ()
  19. {
  20. //Stream.`Close` method deleted
  21. //_stream.Close ();
  22. _stream.Dispose();
  23. }
  24. public long GetFilePointer ()
  25. {
  26. return _stream.Position;
  27. }
  28. public long Length ()
  29. {
  30. return _stream.Length;
  31. }
  32. public int Read (byte[] buffer)
  33. {
  34. int r = _stream.Read (buffer, 0, buffer.Length);
  35. return r > 0 ? r : -1;
  36. }
  37. public int Read (byte[] buffer, int start, int size)
  38. {
  39. return _stream.Read (buffer, start, size);
  40. }
  41. public void ReadFully (byte[] buffer, int start, int size)
  42. {
  43. while (size > 0) {
  44. int num = _stream.Read (buffer, start, size);
  45. if (num == 0) {
  46. throw new EofException ();
  47. }
  48. size -= num;
  49. start += num;
  50. }
  51. }
  52. public void Seek (long pos)
  53. {
  54. _stream.Position = pos;
  55. }
  56. public void SetLength (long len)
  57. {
  58. _stream.SetLength (len);
  59. }
  60. public void Write (int value)
  61. {
  62. _stream.Write (BitConverter.GetBytes (value), 0, 4);
  63. }
  64. public void Write (byte[] buffer)
  65. {
  66. _stream.Write (buffer, 0, buffer.Length);
  67. }
  68. public void Write (byte[] buffer, int start, int size)
  69. {
  70. _stream.Write (buffer, start, size);
  71. }
  72. }
  73. }