public void Write(string value, StringLengthPrefix lengthPrefix) { if (value == null) { if (lengthPrefix == StringLengthPrefix.Byte) { Write((byte)0); } else if (lengthPrefix == StringLengthPrefix.Int32) { Write(0); } } else { if (lengthPrefix == StringLengthPrefix.Byte) { Write((byte)Encoding.UTF8.GetByteCount(value)); } else if (lengthPrefix == StringLengthPrefix.Int32) { Write(Encoding.UTF8.GetByteCount(value)); } var bytes = Encoding.UTF8.GetBytes(value); Write(bytes, 0, bytes.Length); } }
private static byte[] CreateLengthPrefixedString(string value, StringLengthPrefix length, Encoding encoding) { var stringLength = value.Length; byte[] lengthData = null; switch (length) { case StringLengthPrefix.One: lengthData = new byte[] { (byte)stringLength }; break; case StringLengthPrefix.Two: lengthData = BitConverter.GetBytes((short)stringLength); break; case StringLengthPrefix.Four: lengthData = BitConverter.GetBytes(stringLength); break; } ; var stringData = encoding.GetBytes(value); var data = new byte[stringData.Length + lengthData.Length]; Buffer.BlockCopy(lengthData, 0, data, 0, lengthData.Length); Buffer.BlockCopy(stringData, 0, data, lengthData.Length, stringData.Length); return(data); }
/// <summary> /// Reads a <see cref="string"/> from the current stream with one of the prefix reading methods. /// </summary> /// <param name="readPrefix">The method to read the prefix.</param> /// <exception cref="EndOfStreamException"/> /// <exception cref="ObjectDisposedException"/> /// <exception cref="IOException"/> /// <exception cref="ArgumentException"/> /// <exception cref="ArgumentOutOfRangeException"/> /// <returns>The string being read.</returns> public string ReadString(StringLengthPrefix readPrefix) { int length; if (readPrefix == StringLengthPrefix.Byte) { length = ReadByte(); } else if (readPrefix == StringLengthPrefix.Int32) { length = ReadInt32(); } else { throw new ArgumentException("Can't read string without knowing its length."); } return(ReadString(length)); }
/// <summary>Reads a string that is length prefixed.</summary> /// <param name="source">The stream to use.</param> /// <param name="encoding">The encoding to use. If <see langword="null"/> is specified then the default is used.</param> /// <param name="length">The length.</param> /// <returns>The read value.</returns> /// <remarks> /// The method reads the 4-byte length followed by the string. The length is in characters. /// </remarks> /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is invalid.</exception> /// <exception cref="EndOfStreamException">The end of stream was reached.</exception> /// <exception cref="IOException">An IO error occurred.</exception> /// <exception cref="ObjectDisposedException">The stream is closed.</exception> public static string ReadLengthPrefixedString(this Stream source, StringLengthPrefix length, Encoding encoding) { //Read the length int len = 0; switch (length) { case StringLengthPrefix.One: len = ReadSByte(source); break; case StringLengthPrefix.Two: len = ReadInt16(source); break; case StringLengthPrefix.Four: len = ReadInt32(source); break; default: throw new ArgumentOutOfRangeException("length", "The length is invalid."); } ; //Read the (now) fixed size string return(ReadFixedString(source, len, encoding ?? Encoding.Default)); }
/// <exception cref="IOException">An I/O error occurs.</exception> /// <exception cref="ObjectDisposedException">The stream is closed.</exception> public void Write(string?value, StringLengthPrefix lengthPrefix) { var length = value is null ? 0 : Encoding.UTF8.GetByteCount(value); switch (lengthPrefix) { case StringLengthPrefix.Byte: Write((byte)length); break; case StringLengthPrefix.Int32: Write(length); break; } if (value is not null) { WriteBytes(Encoding.UTF8.GetBytes(value)); } }
/// <summary>Writes a length prefixed string.</summary> /// <param name="source">The stream to use.</param> /// <param name="value">The value to write.</param> /// <param name="length">The length prefix to use.</param> /// <param name="encoding">The encoding to use. If <see langword="null"/> is specified then the default is used.</param> /// <returns>The number of bytes written.</returns> /// <remarks> /// The string length is written followed by the string. The length is in characters. /// </remarks> /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is invalid.</exception> /// <exception cref="IOException">An IO error occurred.</exception> /// <exception cref="ObjectDisposedException">The stream is closed.</exception> public static int WriteLengthPrefixedString(this Stream source, string value, StringLengthPrefix length, Encoding encoding) { value = value ?? ""; int bytesWritten = 0; switch (length) { case StringLengthPrefix.One: source.Write((byte)value.Length); bytesWritten += 1; break; case StringLengthPrefix.Two: source.Write((short)value.Length); bytesWritten += 2; break; case StringLengthPrefix.Four: source.Write((int)value.Length); bytesWritten += 4; break; default: throw new ArgumentOutOfRangeException("length", "The length is invalid."); } ; return(WriteFixedString(source, value, value.Length, encoding ?? Encoding.Default) + bytesWritten); }
/// <summary>Writes a length prefixed string.</summary> /// <param name="source">The stream to use.</param> /// <param name="value">The value to write.</param> /// <param name="length">The length prefix to use.</param> /// <returns>The number of characters written.</returns> /// <remarks> /// The string length is written followed by the string. The length is in characters. /// </remarks> /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is invalid.</exception> /// <exception cref="IOException">An IO error occurred.</exception> /// <exception cref="ObjectDisposedException">The stream is closed.</exception> public static int WriteLengthPrefixedString(this Stream source, string value, StringLengthPrefix length) { return(WriteLengthPrefixedString(source, value, length, null)); }
/// <summary>Reads a string that is length prefixed..</summary> /// <param name="source">The stream to use.</param> /// <param name="length">The length.</param> /// <returns>The read value.</returns> /// <remarks> /// The method reads the length followed by the string. The length is in characters. /// </remarks> /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is invalid.</exception> /// <exception cref="EndOfStreamException">The end of stream was reached.</exception> /// <exception cref="IOException">An IO error occurred.</exception> /// <exception cref="ObjectDisposedException">The stream is closed.</exception> public static string ReadLengthPrefixedString(this Stream source, StringLengthPrefix length) { return(ReadLengthPrefixedString(source, length, null)); }