RandomAccessFile.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. {
  45. int num = _stream.Read(buffer, start, size);
  46. if (num == 0)
  47. {
  48. throw new EofException();
  49. }
  50. size -= num;
  51. start += num;
  52. }
  53. }
  54. public void Seek(long pos)
  55. {
  56. _stream.Position = pos;
  57. }
  58. public void SetLength(long len)
  59. {
  60. _stream.SetLength(len);
  61. }
  62. public void Write(int value)
  63. {
  64. _stream.Write(BitConverter.GetBytes(value), 0, 4);
  65. }
  66. public void Write(byte[] buffer)
  67. {
  68. _stream.Write(buffer, 0, buffer.Length);
  69. }
  70. public void Write(byte[] buffer, int start, int size)
  71. {
  72. _stream.Write(buffer, start, size);
  73. }
  74. }
  75. }