简体   繁体   English

使用TextReader读取字节

[英]Using TextReader to read bytes

In the past, I have been using a BinaryReader to read a few bytes, but recently, I have gotten this error: 过去,我一直在使用BinaryReader读取一些字节,但是最近,我遇到了以下错误:

An error has occurred: Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods. This also applies to classes like StreamWriter and StreamReader.    at System.Buffer.InternalBlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count)
   at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.BinaryReader.FillBuffer(Int32 numBytes)
   at System.IO.BinaryReader.ReadUInt16()

So, as a result I have decided to use TextReader's Synchronized methods like the following: 因此,因此,我决定使用TextReader的Synchronized方法,如下所示:

public class SafeReader
{
    private Stream m_Stream;
    private TextReader m_TextReader;
    public SafeReader(Stream stream)
    {
        m_TextReader = TextReader.Synchronized(new StreamReader(m_Stream = stream));
    }
    public Stream BaseStream
    {
        get { return m_Stream; }
    }
    public int ReadInt32()
    {
        // this doesn't even need to do anything (just has to read 4 bytes and it gets disposed of anyway);
        ReadUInt16();
        ReadUInt16();
        return -1;
    }
    public short ReadInt16()
    {
        return (short)(this.ReadUInt16());
    }
    public ushort ReadUInt16()
    {
        return BitConverter.ToUInt16(new byte[] { (byte)(m_TextReader.Read() & 0xFF), (byte)(m_TextReader.Read() & 0xFF) }, 0);
        //return (ushort)(((m_TextReader.Read() & 0xFF)) | ((m_TextReader.Read() & 0xFF) << 8));
    }
}

However, the values returned (it pretty much reads an image in a proprietary format) is incorrect. 但是,返回的值(它几乎以专有格式读取图像)不正确。 The "images" have a slight bluish hue and I have a feeling this could be caused by the fact that TextReader reads text (and reads chars with an encoding instead of just reading the byte values). “图像”的色调略带蓝色,我感觉这可能是由于TextReader读取文本(并使用编码读取字符而不是仅读取字节值)引起的。

Is there a "thread-safe" way like TextReader's Synchronized() to read binary files? 是否有像TextReader的Synchronized()这样的“线程安全”方式来读取二进制文件?

Try File.ReadAllBytes() . 尝试File.ReadAllBytes() Then you could use a MemoryStream to extract values out of the buffer. 然后,您可以使用MemoryStream从缓冲区中提取值。

You should be able to use BinaryReader instead of TextReader . 您应该能够使用BinaryReader而不是TextReader Just make sure you lock the array on the thread you access it from (writing). 只要确保将数组lock在您从中访问它的线程上即可(写入)。

Object locker = new Object;

lock (locker) {
    //BinaryReader here
}

From the other thread(s) use the same: 从其他线程使用相同:

lock (locker) {
    //read from array (read is thread-safe though)
}

If there is a write-operation going on the other thread will wait until the object is unlocked. 如果正在进行写操作,则另一个线程将等待,直到对象被解锁。

You can also use the File.ReadAllBytes if you don't need to read in chunks. 如果不需要分块读取,也可以使用File.ReadAllBytes

Alternatively use ASCII or UTF8 encoding with the Textreader. 或者,将Textreader与ASCII或UTF8编码一起使用。

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

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