ContainerList_Stack.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /// <summary>
  10. /// Create a new Data-Stack to store the data in
  11. /// </summary>
  12. /// <param name="FileName">Set a file name for the data</param>
  13. /// <returns>Returns the Stack-ID. If it failed to create a stack, it will return -1</returns>
  14. public int CreateStack(string FileName) {
  15. for (int i = 0; i < 256; i++) {
  16. if (this._stack[i] == null) {
  17. this._stack[i] = new ContainerStack(FileName);
  18. this._stack[i].IsDone = false;
  19. return i;
  20. }
  21. }
  22. return -1;
  23. }
  24. /// <summary>
  25. /// Create a new Data-Stack to store the data in
  26. /// </summary>
  27. /// <returns>Returns the Stack-ID. If it failed to create a stack, it will return -1</returns>
  28. public int CreateStack() {
  29. for (int i = 0; i < 256; i++) {
  30. if (this._stack[i] == null) {
  31. this._stack[i] = new ContainerStack();
  32. this._stack[i].IsDone = false;
  33. return i;
  34. }
  35. }
  36. return -1;
  37. }
  38. /// <summary>
  39. /// Close the Stack, set the file data and write the stack
  40. /// </summary>
  41. /// <param name="StackId">The Stack-ID</param>
  42. /// <returns>Returns the File-ID</returns>
  43. public int CloseStack(int StackId) {
  44. this._stack[StackId].IsDone = true;
  45. this.WriteStack(StackId);
  46. ContainerFile cf = new ContainerFile();
  47. byte[] chunks = this._stack[StackId].getStackPositions();
  48. cf.UploadDate = DateTime.Now;
  49. cf.FileInfoPos = this.WriteFileInfo(new FileInfoX(this._stack[StackId].FileName));
  50. cf.ChunkPos = this.WriteChunk(chunks);
  51. cf.ChunkLen = chunks.Length;
  52. cf.Size = this._stack[StackId].WrittenBytes;
  53. cf.MediaType = Mime.getMediaType(this._stack[StackId].FileName);
  54. this._stack[StackId] = null;
  55. int fi = this._files.Count();
  56. this._files.Add(cf);
  57. return fi;
  58. }
  59. /// <summary>
  60. /// Write one chunk from the stack into a container
  61. /// </summary>
  62. /// <param name="stackId">The Stack-ID</param>
  63. private void WriteStack(int stackId) {
  64. // Get best container
  65. this.WriteStack(this._stack[stackId]);
  66. }
  67. /// <summary>
  68. /// Write one chunk from the stack into a container
  69. /// </summary>
  70. /// <param name="stack">The Stack</param>
  71. private void WriteStack(ContainerStack stack) {
  72. Container c = this.GetBestContainer(stack);
  73. ushort pos = c.WriteChunk(stack.read(c.ChunkSize));
  74. /*System.Diagnostics.Debug.WriteLine("Writ: " +
  75. c.GroupId.ToString().PadLeft(3) + " | " +
  76. c.ContainerId.ToString().PadLeft(3) + " | " +
  77. pos.ToString().PadLeft(3) + " | "
  78. );*/
  79. stack.AddStackPosition(c, pos);
  80. }
  81. /// <summary>
  82. /// Writes all data from a stack into a container
  83. /// </summary>
  84. /// <param name="stackId">The Stack-ID</param>
  85. private void WriteAllStack(int stackId) {
  86. // Get best container
  87. this.WriteAllStack(this._stack[stackId]);
  88. }
  89. /// <summary>
  90. /// Writes all data from a stack into a container
  91. /// </summary>
  92. /// <param name="stack">The Stack</param>
  93. private void WriteAllStack(ContainerStack stack) {
  94. while (stack.Size > 0) {
  95. Container c = this.GetBestContainer(stack);
  96. ushort pos = c.WriteChunk(stack.read(c.ChunkSize));
  97. System.Diagnostics.Debug.WriteLine("Writ: " +
  98. c.GroupId.ToString().PadLeft(3) + " | " +
  99. c.ContainerId.ToString().PadLeft(3) + " | " +
  100. pos.ToString().PadLeft(3) + " | "
  101. );
  102. stack.AddStackPosition(c, pos);
  103. }
  104. }
  105. }
  106. }