/// <summary> /// Decodes a BEncodedString from the supplied StreamReader /// </summary> /// <param name="reader">The StreamReader containing the BEncodedString</param> internal override void DecodeInternal(RawReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } int letterCount; var length = string.Empty; while ((reader.PeekByte() != -1) && (reader.PeekByte() != ':')) // read in how many characters { length += (char)reader.ReadByte(); // the string is } if (reader.ReadByte() != ':') // remove the ':' { throw new BEncodingException("Invalid data found. Aborting"); } if (!int.TryParse(length, out letterCount)) { throw new BEncodingException( string.Format("Invalid BEncodedString. Length was '{0}' instead of a number", length)); } TextBytes = new byte[letterCount]; if (reader.Read(TextBytes, 0, letterCount) != letterCount) { throw new BEncodingException("Couldn't decode string"); } }
public override void Decode(byte[] buffer, int offset, int length) { using (var reader = new RawReader(new MemoryStream(buffer, offset, length, false), false)) { var d = BEncodedValue.Decode<BEncodedDictionary>(reader); BEncodedValue val; if (d.TryGetValue(MessageTypeKey, out val)) MetadataMessageType = (eMessageType) ((BEncodedNumber) val).Number; if (d.TryGetValue(PieceKey, out val)) Piece = (int) ((BEncodedNumber) val).Number; if (d.TryGetValue(TotalSizeKey, out val)) { var totalSize = (int) ((BEncodedNumber) val).Number; MetadataPiece = new byte[Math.Min(totalSize - Piece*BlockSize, BlockSize)]; reader.Read(MetadataPiece, 0, MetadataPiece.Length); } } }