c# – 使用TextReader读取字节
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用TextReader读取字节,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2047字,纯文字阅读大概需要3分钟。
内容图文
![c# – 使用TextReader读取字节](/upload/InfoBanner/zyjiaocheng/777/c00f8f8e529d4521a4a49ae4b2d623d8.jpg)
在过去,我一直在使用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()
因此,我决定使用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));
}
}
但是,返回的值(它几乎以专有格式读取图像)是不正确的. “图像”略带蓝色色调,我有一种感觉,这可能是由于TextReader读取文本(并使用编码读取字符而不是仅读取字节值).
有没有“线程安全”的方式,如TextReader的Synchronized()来读取二进制文件?
解决方法:
您应该能够使用BiReader并使用TextReader.只需确保将数组锁定在您访问它的线程上(写入).
Object locker = new Object;
lock (locker) {
//BinaryReader here
}
从其他线程使用相同的:
lock (locker) {
//read from array (read is thread-safe though)
}
如果正在进行写操作,则另一个线程将等待对象解锁.
如果您不需要读取块,也可以使用File.ReadAllBytes.
或者使用Textreader的ASCII或UTF8编码.
内容总结
以上是互联网集市为您收集整理的c# – 使用TextReader读取字节全部内容,希望文章能够帮你解决c# – 使用TextReader读取字节所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。