// ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- /// <summary> /// Returns a <see cref="Boolean"/> instance read from the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="format">The <see cref="BooleanDataFormat"/> format in which the data is stored.</param> /// <returns>The value read from the current stream.</returns> public static Boolean ReadBoolean(this Stream stream, BooleanDataFormat format = BooleanDataFormat.Byte) { switch (format) { case BooleanDataFormat.Byte: return(stream.ReadByte() != 0); case BooleanDataFormat.Word: return(ReadInt16(stream) != 0); case BooleanDataFormat.Dword: return(ReadInt32(stream) != 0); default: throw new ArgumentException($"Invalid {nameof(BooleanDataFormat)}.", nameof(format)); } }
/// <summary> /// Returns an array of <see cref="Boolean"/> instances read from the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="count">The number of values to read.</param> /// <param name="format">The <see cref="BooleanDataFormat"/> format in which the data is stored.</param> /// <returns>The array of values read from the current stream.</returns> public static Boolean[] ReadBooleans(this Stream stream, int count, BooleanDataFormat format = BooleanDataFormat.Byte) { var values = new Boolean[count]; lock (stream) { switch (format) { case BooleanDataFormat.Byte: for (int i = 0; i < count; i++) { values[i] = stream.ReadByte() != 0; } break; case BooleanDataFormat.Word: for (int i = 0; i < count; i++) { values[i] = ReadInt16(stream) != 0; } break; case BooleanDataFormat.Dword: for (int i = 0; i < count; i++) { values[i] = ReadInt32(stream) != 0; } break; default: throw new ArgumentException($"Invalid {nameof(BooleanDataFormat)}.", nameof(format)); } } return(values); }
/// <summary> /// Writes an array of <see cref="Boolean"/> values to the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="values">The values to write.</param> /// <param name="format">The <see cref="BooleanDataFormat"/> format in which the data is stored.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> public static void Write(this Stream stream, IEnumerable <Boolean> values, BooleanDataFormat format = BooleanDataFormat.Byte, ByteConverter converter = null) { converter = converter ?? ByteConverter.System; lock (stream) { switch (format) { case BooleanDataFormat.Byte: foreach (var value in values) { stream.WriteByte((Byte)(value ? 1 : 0)); } break; case BooleanDataFormat.Word: foreach (var value in values) { converter.GetBytes((Int16)(value ? 1 : 0), _buffer, 0); stream.Write(_buffer, 0, sizeof(Int16)); } break; case BooleanDataFormat.Dword: foreach (var value in values) { converter.GetBytes(value ? 1 : 0, _buffer, 0); stream.Write(_buffer, 0, sizeof(Int32)); } break; default: throw new ArgumentException($"Invalid {nameof(BooleanDataFormat)}.", nameof(format)); } } }
/// <summary> /// Writes an enumeration of <see cref="Boolean"/> values to the current stream, with 0 representing /// <c>false</c> and 1 representing <c>true</c>. /// </summary> /// <param name="values">The <see cref="Boolean"/> values to write.</param> /// <param name="format">The binary format in which the <see cref="Boolean"/> will be written.</param> public void Write(IEnumerable <Boolean> values, BooleanDataFormat format = BooleanDataFormat.Byte) { BaseStream.Write(values, format, ByteConverter); }
/// <summary> /// Writes a <see cref="Boolean"/> value in the given format to the current stream, with 0 representing /// <c>false</c> and 1 representing <c>true</c>. /// </summary> /// <param name="value">The <see cref="Boolean"/> value to write.</param> /// <param name="format">The binary format in which the <see cref="Boolean"/> will be written.</param> public void Write(Boolean value, BooleanDataFormat format) { BaseStream.Write(value, format, ByteConverter); }
/// <summary> /// Writes a <see cref="T:System.Boolean" /> value in the given format to the current stream, with 0 representing /// <c>false</c> and 1 representing <c>true</c>. /// </summary> /// <param name="value">The <see cref="T:System.Boolean" /> value to write.</param> /// <param name="format">The binary format in which the <see cref="T:System.Boolean" /> will be written.</param> public void Write(bool value, BooleanDataFormat format) { this.BaseStream.Write(value, format, this.ByteConverter); }
/// <summary> /// Reads the specified number of <see cref="T:System.Boolean" /> values from the current stream into a /// <see cref="T:System.Boolean" /> array. The <see cref="T:System.Boolean" /> values are available in the specified binary format. /// </summary> /// <param name="count">The number of <see cref="T:System.Boolean" /> values to read.</param> /// <param name="format">The binary format, in which the <see cref="T:System.Boolean" /> values will be read.</param> /// <returns>The <see cref="T:System.Boolean" /> array read from the current stream.</returns> public bool[] ReadBooleans(int count, BooleanDataFormat format = BooleanDataFormat.Byte) { return(this.BaseStream.ReadBooleans(count, format)); }
/// <summary> /// Reads a <see cref="T:System.Boolean" /> value from the current stream. The <see cref="T:System.Boolean" /> is available in the /// specified binary format. /// </summary> /// <param name="format">The binary format, in which the <see cref="T:System.Boolean" /> will be read.</param> /// <returns>The <see cref="T:System.Boolean" /> read from the current stream.</returns> public bool ReadBoolean(BooleanDataFormat format) { return(this.BaseStream.ReadBoolean(format)); }
/// <summary> /// Reads a <see cref="Boolean"/> value from the current stream. The <see cref="Boolean"/> is available in the /// specified binary format. /// </summary> /// <param name="format">The binary format, in which the <see cref="Boolean"/> will be read.</param> /// <returns>The <see cref="Boolean"/> read from the current stream.</returns> public Boolean ReadBoolean(BooleanDataFormat format) { return(BaseStream.ReadBoolean(format)); }