首页 文章

如何从字符串生成流?

提问于
浏览
625

我需要为一个方法编写单元测试,该方法接受来自文本文件的流 . 我想做这样的事情:

Stream s = GenerateStreamFromString("a,b \n c,d");

12 回答

  • -1
    /// <summary>
    /// Get Byte[] from String
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static byte[] GetBytes(string str)
    {
      byte[] bytes = new byte[str.Length * sizeof(char)];
      System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
      return bytes;
    }
    
    /// <summary>
    /// Get Stream from String
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static Stream GetStream(string str)
    {
      return new MemoryStream(GetBytes(str));
    }
    
  • 37

    另一种方案:

    public static MemoryStream GenerateStreamFromString(string value)
    {
        return new MemoryStream(Encoding.UTF8.GetBytes(value ?? ""));
    }
    
  • 601

    将其添加到静态字符串实用程序类:

    public static Stream ToStream(this string str)
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(str);
        writer.Flush();
        stream.Position = 0;
        return stream;
    }
    

    这会添加一个扩展功能,您可以简单地:

    using (var stringStream = "My string".ToStream())
    {
        // use stringStream
    }
    
  • 20

    我想你可以从使用MemoryStream中受益 . 您可以使用Encoding classGetBytes方法填充您获得的字符串字节 .

  • 8
    public static Stream GenerateStreamFromString(string s)
    {
        var stream = new MemoryStream();
        var writer = new StreamWriter(stream);
        writer.Write(s);
        writer.Flush();
        stream.Position = 0;
        return stream;
    }
    

    别忘了使用:

    using (var stream = GenerateStreamFromString("a,b \n c,d"))
    {
        // ... Do stuff to stream
    }
    

    关于 StreamWriter 没有被处置 . StreamWriter 只是基本流的包装器,不使用任何需要处理的资源 . Dispose 方法将关闭 StreamWriter 正在写入的基础 Stream . 在这种情况下,我们想要返回 MemoryStream .

    在.NET 4.5中,现在有一个 StreamWriter 的重载,它在处理器被处理后保持底层流打开,但是这个代码也做同样的事情并且也适用于其他版本的.NET .

    Is there any way to close a StreamWriter without closing its BaseStream?

  • 9

    干得好:

    private Stream GenerateStreamFromString(String p)
    {
        Byte[] bytes = UTF8Encoding.GetBytes(p);
        MemoryStream strm = new MemoryStream();
        strm.Write(bytes, 0, bytes.Length);
        return strm;
    }
    
  • 807
    public Stream GenerateStreamFromString(string s)
    {
        return new MemoryStream(Encoding.UTF8.GetBytes(s));
    }
    
  • 11

    在@ JoelNet的回答和@Shaun Bowe回答的评论中提出的扩展方法的略微修改版本 . 因为我同意@Palec的评论 .

    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));
    
  • 8

    String扩展的良好组合:

    public static byte[] GetBytes(this string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }
    
    public static Stream ToStream(this string str)
    {
        Stream StringStream = new MemoryStream();
        StringStream.Read(str.GetBytes(), 0, str.Length);
        return StringStream;
    }
    
  • -1

    使用 MemoryStream 类,调用 Encoding.GetBytes 首先将字符串转换为字节数组 .

    你随后在流上需要 TextReader 吗?如果是这样,您可以直接提供 StringReader ,并绕过 MemoryStreamEncoding 步骤 .

  • 100

    我使用了这样的答案组合:

    public static Stream ToStream(this string str, Encoding enc = null)
    {
        enc = enc ?? Encoding.UTF8;
        return new MemoryStream(enc.GetBytes(str ?? ""));
    }
    

    然后我像这样使用它:

    String someStr="This is a Test";
    Encoding enc = getEncodingFromSomeWhere();
    using (Stream stream = someStr.ToStream(enc))
    {
        // Do something with the stream....
    }
    
  • 0

    我们使用下面列出的扩展方法 . 我认为您应该让开发人员对编码做出决定,因此涉及的魔法较少 .

    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 ?? ""));
        }
    }
    

相关问题