/// <summary> /// Returns a <see cref="String"/> instance read from the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="format">The <see cref="StringDataFormat"/> format determining how the length of the string is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> /// <returns>The value read from the current stream.</returns> public static String ReadString(this Stream stream, StringDataFormat format = StringDataFormat.DynamicByteCount, Encoding encoding = null, ByteConverter converter = null) { encoding = encoding ?? Encoding.UTF8; converter = converter ?? ByteConverter.System; switch (format) { case StringDataFormat.DynamicByteCount: return(ReadStringWithLength(stream, Read7BitEncodedInt32(stream), false, encoding)); case StringDataFormat.ByteCharCount: return(ReadStringWithLength(stream, stream.ReadByte(), true, encoding)); case StringDataFormat.Int16CharCount: return(ReadStringWithLength(stream, ReadInt16(stream, converter), true, encoding)); case StringDataFormat.Int32CharCount: return(ReadStringWithLength(stream, ReadInt32(stream, converter), true, encoding)); case StringDataFormat.ZeroTerminated: return(ReadStringZeroPostfix(stream, encoding)); default: throw new ArgumentException($"Invalid {nameof(StringDataFormat)}.", nameof(format)); } }
/// <summary> /// Returns an array of <see cref="String"/> 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="StringDataFormat"/> format determining how the length of the strings is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> /// <returns>The array of values read from the current stream.</returns> public static String[] ReadStrings(this Stream stream, int count, StringDataFormat format = StringDataFormat.DynamicByteCount, Encoding encoding = null, ByteConverter converter = null) { encoding = encoding ?? Encoding.UTF8; converter = converter ?? ByteConverter.System; var values = new String[count]; lock (stream) { switch (format) { case StringDataFormat.DynamicByteCount: for (int i = 0; i < count; i++) { values[i] = ReadStringWithLength(stream, Read7BitEncodedInt32(stream), false, encoding); } break; case StringDataFormat.ByteCharCount: for (int i = 0; i < count; i++) { values[i] = ReadStringWithLength(stream, stream.ReadByte(), true, encoding); } break; case StringDataFormat.Int16CharCount: for (int i = 0; i < count; i++) { values[i] = ReadStringWithLength(stream, ReadInt16(stream, converter), true, encoding); } break; case StringDataFormat.Int32CharCount: for (int i = 0; i < count; i++) { values[i] = ReadStringWithLength(stream, ReadInt32(stream, converter), true, encoding); } break; case StringDataFormat.ZeroTerminated: for (int i = 0; i < count; i++) { values[i] = ReadStringZeroPostfix(stream, encoding); } break; default: throw new ArgumentException($"Invalid {nameof(StringDataFormat)}.", nameof(format)); } } return(values); }
/// <summary> /// Writes a <see cref="String"/> value to the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="value">The value to write.</param> /// <param name="format">The <see cref="StringDataFormat"/> format determining how the length of the string is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> public static void Write(this Stream stream, String value, StringDataFormat format = StringDataFormat.DynamicByteCount, Encoding encoding = null, ByteConverter converter = null) { encoding = encoding ?? Encoding.UTF8; converter = converter ?? ByteConverter.System; byte[] buffer = encoding.GetBytes(value); switch (format) { case StringDataFormat.DynamicByteCount: Write7BitEncodedInt(stream, buffer.Length); stream.Write(buffer, 0, buffer.Length); break; case StringDataFormat.ByteCharCount: stream.WriteByte((byte)value.Length); stream.Write(buffer, 0, buffer.Length); break; case StringDataFormat.Int16CharCount: converter.GetBytes((Int16)value.Length, _buffer, 0); stream.Write(_buffer, 0, sizeof(Int16)); stream.Write(buffer, 0, buffer.Length); break; case StringDataFormat.Int32CharCount: converter.GetBytes(value.Length, _buffer, 0); stream.Write(_buffer, 0, sizeof(Int32)); stream.Write(buffer, 0, buffer.Length); break; case StringDataFormat.ZeroTerminated: stream.Write(buffer, 0, buffer.Length); switch (encoding.GetByteCount("A")) { case sizeof(Byte): stream.WriteByte(0); break; case sizeof(Int16): stream.WriteByte(0); stream.WriteByte(0); break; } break; default: throw new ArgumentException($"Invalid {nameof(StringDataFormat)}.", nameof(format)); } }
public void initializeDataFormats() { var queryStr = from r in rawDataFormats where selectedFileTypes.Any(x => x == r.FileType) && r.DataType == "C" select r; foreach (var q in queryStr) { var toAdd = new StringDataFormat() { key = q.ColumnName, position = q.Postion, length = q.Lengths, start_year = q.start_year, end_year = q.end_year, FileType = q.FileType }; stringDataFormats.Add(toAdd); } var queryNum = from r in rawDataFormats where selectedFileTypes.Any(x => x == r.FileType) && r.DataType == "N" select r; foreach (var q in queryNum) { var toAdd = new NumberDataFormat() { key = q.ColumnName, position = q.Postion, length = q.Lengths, start_year = q.start_year, end_year = q.end_year, FileType = q.FileType }; numberDataFormats.Add(toAdd); } }
/// <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, StringDataFormat format = StringDataFormat.DynamicByteCount, Encoding encoding = null) { BaseStream.Write(values, format, encoding); }
/// <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 <see cref="String"/> 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, StringDataFormat format, Encoding encoding = null) { BaseStream.Write(value, format, encoding, ByteConverter); }
/// <summary> /// Writes an enumeration of <see cref="T:System.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="T:System.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, StringDataFormat format = StringDataFormat.DynamicByteCount, Encoding encoding = null) { this.BaseStream.Write(values, format, encoding, (ByteConverter)null); }
/// <summary> /// Reads the specified number of <see cref="T:System.String" /> values from the current stream into a /// <see cref="T:System.String" /> array. The strings are available in the specified binary format and encoding. /// </summary> /// <param name="count">The number of <see cref="T:System.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 or <c>null</c> to use the encoding /// configured for this instance.</param> /// <returns>The <see cref="T:System.String" /> array read from the current stream.</returns> public string[] ReadStrings(int count, StringDataFormat format, Encoding encoding = null) { return(this.BaseStream.ReadStrings(count, format, encoding ?? this.Encoding, this.ByteConverter)); }
/// <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 or <c>null</c> to use the encoding /// configured for this instance.</param> /// <returns>The string read from the current stream.</returns> public string ReadString(StringDataFormat format, Encoding encoding = null) { return(this.BaseStream.ReadString(format, encoding ?? this.Encoding, this.ByteConverter)); }
/// <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 or <c>null</c> to use the encoding /// configured for this instance.</param> /// <returns>The string read from the current stream.</returns> public String ReadString(StringDataFormat format, Encoding encoding = null) { return(BaseStream.ReadString(format, encoding ?? Encoding, ByteConverter)); }
/// <summary> /// Writes an array of <see cref="String"/> 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="StringDataFormat"/> format determining how the length of the strings is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> public static void Write(this Stream stream, IEnumerable <String> values, StringDataFormat format = StringDataFormat.DynamicByteCount, Encoding encoding = null, ByteConverter converter = null) { }