ContainerList_ReadBytes.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace System.Fire.Container {
  8. public partial class ContainerList {
  9. public byte[] ReadBytes(int FileIndex) {
  10. return ReadBytes(this._files[FileIndex]);
  11. }
  12. public byte[] ReadBytes(ContainerFile File) {
  13. return ReadBytes(File, 0, File.Size);
  14. }
  15. // ---
  16. public byte[] ReadBytes(int FileIndex, long StartPos) {
  17. return ReadBytes(this._files[FileIndex], StartPos);
  18. }
  19. public byte[] ReadBytes(ContainerFile File, long StartPos) {
  20. return ReadBytes(File, StartPos, File.Size - StartPos);
  21. }
  22. // ---
  23. public byte[] ReadBytes(int FileIndex, long StartPos, long Length) {
  24. return this.ReadBytes(this._files[FileIndex], StartPos, Length);
  25. }
  26. public byte[] ReadBytes(ContainerFile File, long StartPos, long Length) {
  27. long len = Length;
  28. if (len > File.Size - StartPos)
  29. len = File.Size - StartPos;
  30. byte[] res = new byte[0];
  31. System.Diagnostics.Debug.WriteLine("ReadBytes 1: " + StartPos + " | " + Length + " | " + len);
  32. for (int i = 0, c = File.NumChunks; i < c; i++) {
  33. int containerSize = this.GetContainerLenFromChunk(File.ChunkPos, i);
  34. long xStart = Math.Max(0, StartPos);
  35. System.Diagnostics.Debug.WriteLine("ReadBytes 2: " + i + " | " + xStart + " | " + containerSize);
  36. if(StartPos <= containerSize) {
  37. int bytesToReadFromContainer = (int)Math.Min(len, containerSize - StartPos);
  38. byte[] b = this.ReadChunk(File.ChunkPos, i).Skip((int)xStart).Take(bytesToReadFromContainer).ToArray();
  39. System.Diagnostics.Debug.WriteLine("ReadBytes X: " + xStart + " | " + bytesToReadFromContainer + " | " + b.Length);
  40. res.Concat(b);
  41. StartPos -= bytesToReadFromContainer;
  42. len -= b.Length;
  43. } else {
  44. StartPos -= containerSize;
  45. }
  46. if (len <= 0)
  47. break;
  48. }
  49. return res;
  50. }
  51. }
  52. }