Ejemplo n.º 1
0
 /// <summary>
 /// 写入数据,如果数据溢出,就
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="offset"></param>
 /// <param name="len"></param>
 public void Write(byte[] buffer, int offset, int len)
 {
     Trace.Assert(len > 0 && offset + len <= buffer.Length);
     this.m_nDataSize += len;
     while (len > 0)
     {
         PieceBuffer pieceBuffer = this.m_pieces.Last.Value;
         if (pieceBuffer.IsWriteOver)
         {
             if (this.m_abandonPiecesCache.Count > 0)
             {
                 LinkedListNode <PieceBuffer> first = this.m_abandonPiecesCache.First;
                 this.m_abandonPiecesCache.RemoveFirst();
                 this.m_pieces.AddLast(first);
                 pieceBuffer = first.Value;
             }
             else
             {
                 pieceBuffer = new PieceBuffer(this.m_nPieceSize);
                 this.m_pieces.AddLast(pieceBuffer);
             }
         }
         int num = pieceBuffer.Write(buffer, offset, len);
         offset += num;
         len    -= num;
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 读取数据,如果超出buffer就添加这个已经读取的数据到abandonpiecesCache里面
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="offset"></param>
 /// <param name="len"></param>
 public void Read(byte[] buffer, int offset, int len)
 {
     Trace.Assert(len > 0 && offset + len <= buffer.Length && len <= this.m_nDataSize);
     this.m_nDataSize -= len;
     while (len > 0)
     {
         PieceBuffer value = this.m_pieces.First.Value;
         if (value.IsReadOver)
         {
             LinkedListNode <PieceBuffer> first = this.m_pieces.First;
             this.m_pieces.RemoveFirst();
             if (this.m_pieces.Count + this.m_abandonPiecesCache.Count < this.m_nMinPieceNum)
             {
                 this.m_abandonPiecesCache.AddLast(first);
             }
         }
         else
         {
             int num = value.Read(buffer, offset, len);
             offset += num;
             len    -= num;
         }
     }
 }