我需要为一个方法编写一个单元测试,该方法采用来自文本文件的流。我想做这样的事情:
Stream s = GenerateStreamFromString("a,b \n c,d");
我需要为一个方法编写一个单元测试,该方法采用来自文本文件的流。我想做这样的事情:
Stream s = GenerateStreamFromString("a,b \n c,d");
当前回答
给你:
private Stream GenerateStreamFromString(String p)
{
Byte[] bytes = UTF8Encoding.GetBytes(p);
MemoryStream strm = new MemoryStream();
strm.Write(bytes, 0, bytes.Length);
return strm;
}
其他回答
ToStream扩展方法的现代化和轻微修改版本:
public static Stream ToStream(this string value) => ToStream(value, Encoding.UTF8);
public static Stream ToStream(this string value, Encoding encoding)
=> new MemoryStream(encoding.GetBytes(value ?? string.Empty));
修改建议在@Palec的评论@肖恩鲍威的回答。
或者作为一行语句(由@satnhak建议):
public static Stream ToStream(this string value, Encoding encoding = null)
=> new MemoryStream((encoding ?? Encoding.UTF8).GetBytes(value ?? string.Empty));
使用MemoryStream类,调用Encoding。GetBytes首先将字符串转换为字节数组。
您随后是否需要流上的TextReader ?如果是这样,您可以直接提供一个StringReader,并绕过MemoryStream和Encoding步骤。
我们使用下面列出的扩展方法。我认为您应该让开发人员对编码做出决定,这样就不会有什么神奇的事情发生。
public static class StringExtensions {
public static Stream ToStream(this string s) {
return s.ToStream(Encoding.UTF8);
}
public static Stream ToStream(this string s, Encoding encoding) {
return new MemoryStream(encoding.GetBytes(s ?? ""));
}
}
如果你需要改变编码,我投票给@ShaunBowe的解决方案。但是这里的每个答案都至少将整个字符串复制到内存中一次。ToCharArray + BlockCopy组合的答案做了两次。
如果这很重要的话,这里有一个用于原始UTF-16字符串的简单流包装器。如果与StreamReader一起使用,请选择Encoding。Unicode:
public class StringStream : Stream
{
private readonly string str;
public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => str.Length * 2;
public override long Position { get; set; } // TODO: bounds check
public StringStream(string s) => str = s ?? throw new ArgumentNullException(nameof(s));
public override long Seek(long offset, SeekOrigin origin)
{
switch (origin)
{
case SeekOrigin.Begin:
Position = offset;
break;
case SeekOrigin.Current:
Position += offset;
break;
case SeekOrigin.End:
Position = Length - offset;
break;
}
return Position;
}
private byte this[int i] => (i & 1) == 0 ? (byte)(str[i / 2] & 0xFF) : (byte)(str[i / 2] >> 8);
public override int Read(byte[] buffer, int offset, int count)
{
// TODO: bounds check
var len = Math.Min(count, Length - Position);
for (int i = 0; i < len; i++)
buffer[offset++] = this[(int)(Position++)];
return (int)len;
}
public override int ReadByte() => Position >= Length ? -1 : this[(int)Position++];
public override void Flush() { }
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override string ToString() => str; // ;)
}
这里有一个更完整的解决方案,包含必要的绑定检查(派生自MemoryStream,因此它也有ToArray和WriteTo方法)。
public Stream GenerateStreamFromString(string s)
{
return new MemoryStream(Encoding.UTF8.GetBytes(s));
}