/// <summary> /// Constructor /// </summary> private MultiPartFile(Stream stream) { this.stream = stream; this.partStart = 0; this.Position = 0; this.partEnd = stream.Length - 1; this.Length = partEnd + 1; this.next = null; }
/// <summary> /// Read from the MultiPartFile to a buffer /// </summary> public bool Read(byte[] x, int offset, int amount) { int bufpos; MultiPartFile mf = this; while (Position > mf.partEnd && mf.next != null) { mf = mf.next; } if (Position <= mf.partEnd) { mf.stream.Seek(Position - mf.partStart, SeekOrigin.Begin); if (mf.partEnd + 1 - Position >= amount) { mf.stream.Read(x, offset, amount); Position += amount; } else { byte[] buf = new byte[0xffff]; bufpos = 0; do { if (mf.partEnd + 1 < Position + amount - bufpos) { mf.stream.Read(buf, bufpos, (int)(mf.partEnd + 1 - Position)); bufpos += (int)(mf.partEnd + 1 - Position); Position = mf.partEnd + 1; mf = mf.next; } else { mf.stream.Read(buf, bufpos, amount - bufpos); Position += amount - bufpos; bufpos = amount; } }while (bufpos != amount); Array.ConstrainedCopy(buf, 0, x, offset, amount); } return(true); } return(false); }
/// <summary> /// Append a new MultiPartFile to the current one /// </summary> /// <param name="mpf">New MultiPartFile to append</param> private bool Append(MultiPartFile mpf) { // If the part is invalid, we can't append if (mpf == null) { return(false); } // Find the current last part var mf = this; while (next != null) { mf = mf.next; } // Assign the new part as the new end mf.next = mpf; mf.next.partStart = this.Length; mf.next.partEnd += this.Length; this.Length = mf.next.partEnd + 1; return(true); }