简体   繁体   English

为什么StringWriter.ToString返回`System.Byte []`而不是数据?

[英]Why StringWriter.ToString return `System.Byte[]` and not the data?

UnZipFile method writes the data from inputStream to outputWriter . UnZipFile方法将数据从inputStream写入outputWriter Why sr.ToString() returns System.Byte[] and not the data? 为什么sr.ToString()返回System.Byte[]而不是数据?

using (var sr = new StringWriter())
{
    UnZipFile(response.GetResponseStream(), sr);
    var content = sr.ToString();
}


public static void UnZipFile(Stream inputStream, TextWriter outputWriter)
{
    using (var zipStream = new ZipInputStream(inputStream))
    {
        ZipEntry currentEntry;
        if ((currentEntry = zipStream.GetNextEntry()) != null)
        {
            var size = 2048;
            var data = new byte[size];
            while (true)
            {
                size = zipStream.Read(data, 0, size);
                if (size > 0)
                {
                    outputWriter.Write(data);                       
                }
                else
                {
                    break;
                }
            }
        }
    }
}

The problem is on the line: 问题在于:

outputWriter.Write(data); 

StringWriter.Write has no overload expecting a byte[] . StringWriter.Write没有期望byte[]重载。 Therefore, Write(Object) is called instead. 因此,改为调用Write(Object) And according to MSDN : 并根据MSDN

Writes the text representation of an object to the text string or stream by calling the ToString method on that object. 通过调用该对象上的ToString方法,将对象的文本表示写入文本字符串或流。

Calling ToString on a byte array returns System.byte[] , explaining how you get that string in your StringWriter . 在字节数组上调用ToString返回System.byte[] ,解释如何在StringWriter获取该字符串。

The reason is simple: 原因很简单:

data is of type byte[] . data的类型为byte[] There is no overload for byte[] on StringWriter so it uses the overload for object . StringWriter上的byte[]没有重载,因此它使用对象重载 And then calls ToString() on the boxed byte array which simply prints the type. 然后在盒装字节数组上调用ToString() ,它只是打印类型。

Your code is equivalent to this: 您的代码等同于:

outputWriter.Write(data.ToString());

theateist, theateist,

Looking at the other answers here, I am going to have to agree that the reason for the "ToString()" returning System.Byte[] is because that is what you are putting into it, and everything put into the StringWriter calls it's own "ToString" method when doing so. 看看这里的其他答案,我将不得不同意“ToString()”返回System.Byte []的原因是因为这就是你所投入的内容,并且放入StringWriter的所有东西都称它为自己的这样做时“ToString”方法。 (ie byte[].toString() = "System.byte[]"). (即byte [] .toString()=“System.byte []”)。 In fact the whole idea is that the StringWriter is only ment for writing into a string "buffer" (StringBuilder), so in theory if your file was large enough(bigger than 2048), your output would be "System.Byte[]System.Byte[]" (etc.). 事实上,整个想法是StringWriter仅用于写入字符串“buffer”(StringBuilder),所以理论上如果你的文件足够大(大于2048),你的输出将是“System.Byte [] System .Byte []“(等)。 Try this to deflate into a memory stream and then read from that stream, may be a better understanding of what you are looking at. 尝试将其放入内存流,然后从该流中读取,可能更好地了解您正在查看的内容。 (Code not tested, just example). (代码未经测试,只是示例)。

using (Stream ms = new MemoryStream())  
{
    UnZipFile(response.GetResponseStream(), ms);

    string content;

    ms.Position = 0;
    using(StreamReader s = new StreamReader(ms))
    {
        content = s.ReadToEnd();
    } 
}

public static void UnZipFile(Stream inputStream, Stream outputWriter)
{      
    using (var zipStream = new ZipInputStream(inputStream))
    {
        ZipEntry currentEntry;
        if ((currentEntry = zipStream.GetNextEntry()) != null)
        {
            int size = 2048;
            byte[] data = new byte[size]; 
            while (true)
            {
                size = zipStream.Read(data, 0, size);
                if (size > 0)
                {
                    outputWriter.Write(data);
                }
                else
                {
                    break;
                }
            }
        }
    }
}

Another idea would actually be to using the endcoding to get the string 另一个想法实际上是使用endcoding来获取字符串

public string UnZipFile(Stream inputStream)
{
    string tmp;

    using(Stream zipStream = new ZipInputStream(inputStream))
    {
        ZipEntry currentEntry;

        if(currentEntry = zipStream.GetNextEntry()) != null)
        {
            using(Stream ms = new MemoryStream())
            {
                int size = 2048;
                byte[] data = new byte[size];
                while(true)
                {
                    if((size = zipStream.Read(data,0,size)) > 0)
                        ms.Write(data);
                    else
                        break;
                }
                tmp = Encoding.Default.GetString(ms.ToByteArray());
                }
            }
        }
    }
    return tmp;
}

Or as one last idea, you could actually change your original code to have 或者作为最后一个想法,您实际上可以更改原始代码

outputWriter.Write(Encoding.Default.GetString(data));

Instead of 代替

outputWriter.Write(data);

By the way, please avoid the var keyword in posts, maybe just my pet peev, but code is less readable when utilizing weak types. 顺便说一句,请避免在帖子中使用var关键字,也许只是我的宠物peev,但在使用弱类型时代码可读性较差。

StringWriter.Write:MSDN StringWriter.ToString:MSDN StringWriter.Write:MSDN StringWriter.ToString:MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM