using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace System.Fire.Container {
public partial class ContainerList {
///
/// Create a new Data-Stack to store the data in
///
/// Set a file name for the data
/// Returns the Stack-ID. If it failed to create a stack, it will return -1
public int CreateStack(string FileName) {
for (int i = 0; i < 256; i++) {
if (this._stack[i] == null) {
this._stack[i] = new ContainerStack(FileName);
this._stack[i].IsDone = false;
return i;
}
}
return -1;
}
///
/// Create a new Data-Stack to store the data in
///
/// Returns the Stack-ID. If it failed to create a stack, it will return -1
public int CreateStack() {
for (int i = 0; i < 256; i++) {
if (this._stack[i] == null) {
this._stack[i] = new ContainerStack();
this._stack[i].IsDone = false;
return i;
}
}
return -1;
}
///
/// Close the Stack, set the file data and write the stack
///
/// The Stack-ID
/// Returns the File-ID
public int CloseStack(int StackId) {
this._stack[StackId].IsDone = true;
this.WriteStack(StackId);
ContainerFile cf = new ContainerFile();
byte[] chunks = this._stack[StackId].getStackPositions();
cf.UploadDate = DateTime.Now;
cf.FileInfoPos = this.WriteFileInfo(new FileInfoX(this._stack[StackId].FileName));
cf.ChunkPos = this.WriteChunk(chunks);
cf.ChunkLen = chunks.Length;
cf.Size = this._stack[StackId].WrittenBytes;
cf.MediaType = Mime.getMediaType(this._stack[StackId].FileName);
this._stack[StackId] = null;
int fi = this._files.Count();
this._files.Add(cf);
return fi;
}
///
/// Write one chunk from the stack into a container
///
/// The Stack-ID
private void WriteStack(int stackId) {
// Get best container
this.WriteStack(this._stack[stackId]);
}
///
/// Write one chunk from the stack into a container
///
/// The Stack
private void WriteStack(ContainerStack stack) {
Container c = this.GetBestContainer(stack);
ushort pos = c.WriteChunk(stack.read(c.ChunkSize));
/*System.Diagnostics.Debug.WriteLine("Writ: " +
c.GroupId.ToString().PadLeft(3) + " | " +
c.ContainerId.ToString().PadLeft(3) + " | " +
pos.ToString().PadLeft(3) + " | "
);*/
stack.AddStackPosition(c, pos);
}
///
/// Writes all data from a stack into a container
///
/// The Stack-ID
private void WriteAllStack(int stackId) {
// Get best container
this.WriteAllStack(this._stack[stackId]);
}
///
/// Writes all data from a stack into a container
///
/// The Stack
private void WriteAllStack(ContainerStack stack) {
while (stack.Size > 0) {
Container c = this.GetBestContainer(stack);
ushort pos = c.WriteChunk(stack.read(c.ChunkSize));
System.Diagnostics.Debug.WriteLine("Writ: " +
c.GroupId.ToString().PadLeft(3) + " | " +
c.ContainerId.ToString().PadLeft(3) + " | " +
pos.ToString().PadLeft(3) + " | "
);
stack.AddStackPosition(c, pos);
}
}
}
}