internal static string ReadString(this BinaryReader br, BinaryStringFormat format, Encoding encoding) { if (format == BinaryStringFormat.VariableLengthPrefix) { return(br.ReadString()); } if (format == BinaryStringFormat.WordLengthPrefix) { int count = br.ReadInt32(); return(encoding.GetString(br.ReadBytes(count))); } if (format == BinaryStringFormat.ZeroTerminated) { List <byte> list = new List <byte>(); for (byte i = br.ReadByte(); i != 0; i = br.ReadByte()) { list.Add(i); } return(encoding.GetString(list.ToArray())); } if (format == BinaryStringFormat.NoPrefixOrTermination) { throw new ArgumentException("NoPrefixOrTermination cannot be used for read operations. Specify the length of the string instead to read strings with no prefix or terminator."); } throw new ArgumentOutOfRangeException("The specified binary string format is invalid."); }
/// <summary> /// Writes a string to this stream with the given encoding and advances the current position of the stream in /// accordance with the encoding used and the specific characters being written to the stream. The string will /// be available in the specified binary format. /// </summary> /// <param name="value">The value to write.</param> /// <param name="format">The binary format in which the string will be written.</param> /// <param name="encoding">The encoding used for converting the string.</param> public void Write(String value, BinaryStringFormat format, Encoding encoding) { switch (format) { case BinaryStringFormat.ByteLengthPrefix: WriteByteLengthPrefixString(value, encoding); break; case BinaryStringFormat.WordLengthPrefix: WriteWordLengthPrefixString(value, encoding); break; case BinaryStringFormat.DwordLengthPrefix: WriteDwordLengthPrefixString(value, encoding); break; case BinaryStringFormat.ZeroTerminated: WriteZeroTerminatedString(value, encoding); break; case BinaryStringFormat.NoPrefixOrTermination: WriteNoPrefixOrTerminationString(value, encoding); break; default: throw new ArgumentOutOfRangeException("format", "The specified binary string format is invalid"); } }
/// <summary> /// Writes an enumeration of <see cref="String"/> values to this stream with the given encoding. The strings /// will be available in the specified binary format. /// </summary> /// <param name="values">The <see cref="String"/> values to write.</param> /// <param name="format">The binary format in which the strings will be written.</param> /// <param name="encoding">The encoding used for converting the strings.</param> public void Write(IEnumerable <String> values, BinaryStringFormat format, Encoding encoding) { foreach (String value in values) { Write(value, format, encoding); } }
/// <summary> /// Reads a string from the current stream. The string is available in the specified binary format and encoding. /// </summary> /// <param name="format">The binary format, in which the string will be read.</param> /// <param name="encoding">The encoding used for converting the string.</param> /// <returns>The string read from the current stream.</returns> public String ReadString(BinaryStringFormat format, Encoding encoding) { switch (format) { case BinaryStringFormat.ByteLengthPrefix: return(ReadStringInternal(ReadByte(), encoding)); case BinaryStringFormat.WordLengthPrefix: return(ReadStringInternal(ReadInt16(), encoding)); case BinaryStringFormat.DwordLengthPrefix: return(ReadStringInternal(ReadInt32(), encoding)); case BinaryStringFormat.VariableLengthPrefix: return(ReadStringInternal(Read7BitEncodedInt(), encoding)); case BinaryStringFormat.ZeroTerminated: return(ReadZeroTerminatedString(encoding)); case BinaryStringFormat.NoPrefixOrTermination: throw new ArgumentException("NoPrefixOrTermination cannot be used for read operations if no length " + "has been specified.", nameof(format)); default: throw new ArgumentOutOfRangeException(nameof(format), "The specified binary string format is invalid."); } }
/// <summary> /// Reads the specified number of <see cref="String"/> from the current stream into a <see cref="String"/> /// array. The strings are available in the specified binary format and encoding. /// </summary> /// <param name="count">The number of <see cref="String"/> values to read.</param> /// <param name="format">The binary format, in which the string will be read.</param> /// <param name="encoding">The encoding used for converting the string.</param> /// <returns>The <see cref="String"/> array read from the current stream.</returns> public String[] ReadStrings(int count, BinaryStringFormat format, Encoding encoding) { String[] values = new String[count]; for (int i = 0; i < values.Length; i++) { values[i] = ReadString(format, encoding); } return(values); }
internal static void Write(this BinaryWriter bw, string value, BinaryStringFormat format, Encoding encoding) { if (format == BinaryStringFormat.VariableLengthPrefix) { bw.Write(value); } else if (format == BinaryStringFormat.WordLengthPrefix) { bw.Write(value.Length); bw.Write(encoding.GetBytes(value)); } else if (format == BinaryStringFormat.ZeroTerminated) { bw.Write(encoding.GetBytes(value)); bw.Write((byte)0); } else if (format == BinaryStringFormat.NoPrefixOrTermination) { bw.Write(encoding.GetBytes(value)); } }
/// <summary> /// Reads a string from the current stream. The string is available in the specified binary format and encoding. /// </summary> /// <param name="format">The binary format, in which the string will be read.</param> /// <param name="encoding">The encoding used for converting the string.</param> /// <returns>The string read from the current stream.</returns> public String ReadString(BinaryStringFormat format, Encoding encoding) { switch (format) { case BinaryStringFormat.ByteLengthPrefix: return(ReadByteLengthPrefixString(encoding)); case BinaryStringFormat.WordLengthPrefix: return(ReadWordLengthPrefixString(encoding)); case BinaryStringFormat.DwordLengthPrefix: return(ReadDwordLengthPrefixString(encoding)); case BinaryStringFormat.ZeroTerminated: return(ReadZeroTerminatedString(encoding)); case BinaryStringFormat.NoPrefixOrTermination: throw new ArgumentException("NoPrefixOrTermination cannot be used for read operations if no length " + "has been specified.", "format"); default: throw new ArgumentOutOfRangeException("format", "The specified binary string format is invalid"); } }
/// <summary> /// Writes a string to this stream in the current encoding of the <see cref="BinaryDataWriter"/> and advances /// the current position of the stream in accordance with the encoding used and the specific characters being /// written to the stream. The string will be available in the specified binary format. /// </summary> /// <param name="value">The value to write.</param> /// <param name="format">The binary format in which the string will be written.</param> public void Write(String value, BinaryStringFormat format) { Write(value, format, Encoding); }
/// <summary> /// Reads a string from the current stream. The string is available in the specified binary format. /// </summary> /// <param name="format">The binary format, in which the string will be read.</param> /// <returns>The string read from the current stream.</returns> public String ReadString(BinaryStringFormat format) { return(ReadString(format, Encoding)); }
internal static string ReadString(this BinaryReader br, BinaryStringFormat format) { return(br.ReadString(format, new ASCIIEncoding())); }
internal static void Write(this BinaryWriter bw, string value, BinaryStringFormat format) { bw.Write(value, format, new ASCIIEncoding()); }