binaryreader

Performance issue with reading integers from a binary file at specific locations in F#

我们两清 提交于 2020-01-02 10:13:48
问题 This morning I asked here why my Python code was (a lot) slower then my F# version but I'm wondering whether the F# version can be made faster. Any ideas how I could create a faster version of the below code that reads a sorted list of unique indexes from a binary file with 32-bit integers? Note that I tried 2 approaches, one based on a BinaryReader, the other one based on MemoryMappedFile (and some more on Github). module SimpleRead let readValue (reader:BinaryReader) cellIndex = // set

EndOfStream for BinaryReader

吃可爱长大的小学妹 提交于 2020-01-01 08:44:11
问题 BinaryReader does not have EndOfStream property. Is it safe to use following code to check if end of stream is reached? reader.BaseStream.Length>reader.BaseStream.Position 回答1: It depends. There are various stream types that do not implement the Length or Position property, you'd get a NotSupportedException. NetworkStream for example. Of course, if you'd use such a stream then you really do have to know up front how often to call the BinaryReader.Read() method. So, yes, it's fine. 回答2: The

Faster (unsafe) BinaryReader in .NET

荒凉一梦 提交于 2019-12-31 09:02:11
问题 I came across a situation where I have a pretty big file that I need to read binary data from. Consequently, I realized that the default BinaryReader implementation in .NET is pretty slow. Upon looking at it with .NET Reflector I came across this: public virtual int ReadInt32() { if (this.m_isMemoryStream) { MemoryStream stream = this.m_stream as MemoryStream; return stream.InternalReadInt32(); } this.FillBuffer(4); return (((this.m_buffer[0] | (this.m_buffer[1] << 8)) | (this.m_buffer[2] <<

Faster (unsafe) BinaryReader in .NET

佐手、 提交于 2019-12-31 09:00:09
问题 I came across a situation where I have a pretty big file that I need to read binary data from. Consequently, I realized that the default BinaryReader implementation in .NET is pretty slow. Upon looking at it with .NET Reflector I came across this: public virtual int ReadInt32() { if (this.m_isMemoryStream) { MemoryStream stream = this.m_stream as MemoryStream; return stream.InternalReadInt32(); } this.FillBuffer(4); return (((this.m_buffer[0] | (this.m_buffer[1] << 8)) | (this.m_buffer[2] <<

Unable to read beyond the end of the stream is caused when use readbinary

给你一囗甜甜゛ 提交于 2019-12-24 21:28:11
问题 I am trying to send a value by socket .So i have two parts in my project Client and server . The client sends a value to server using this code : NetworkStream networkStream = socketForServer.GetStream(); System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(networkStream); //------ int messageSource = 0; int messageDesitination = 0; int interlockingId = 0; int trackId = 0; int trainId = 2; int direction = 0; int messageType = 0; int informationType = 0; int dateTime = 0; foreach

loop for reading different data types & sizes off very large byte array from file

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 18:23:02
问题 I have a raw byte stream stored on a file (rawbytes.txt) that I need to parse and output to a CSV-style text file. The input of raw bytes (when read as characters/long/int etc.) looks something like this: A2401028475764B241102847576511001200C... Parsed it should look like: OutputA.txt (Field1,Field2,Field3) - heading A,240,1028475764 OutputB.txt (Field1,Field2,Field3,Field4,Field5) - heading B,241,1028475765,1100,1200 OutputC.txt C,...//and so on Essentially, it's a hex-dump-style input of

Binary Reader and Writer open at same time?

匆匆过客 提交于 2019-12-22 09:00:10
问题 I'm writing code that deals with a file that uses hashes. I need to read a chunk, then hash it, then write it, then read another chunk, etc. In other words, I need to do a lot of reading and writing. I'm sure this is really simple, but I just wanted to run it by the pros... Is it possible, and acceptable to do something like: BinaryReader br = new BinaryReader (File.OpenRead(path)); BinaryWriter bw = new BinaryWriter (File.OpenWrite(path)); br.dostuff(); bw.dostuff(); I remember running into

Is it safe to use Stream.Seek when a BinaryReader is open?

我是研究僧i 提交于 2019-12-19 01:25:30
问题 Because of the under the hood buffering strategy of BinaryReader, it is unclear to me whether is it ok or not to read an offset stored in a stream, then reposition the stream at this offset to resume the streaming. As an example, is the following code ok: using (var reader = new CustomBinaryReader(inputStream)) { var offset= reader.ReadInt32(); reader.BaseStream.Seek(offset, SeekOrigin.Begin); //Then resume reading the streaming } Or should I close the first binary reader before Seeking the

Efficient way to read big endian data in C#

断了今生、忘了曾经 提交于 2019-12-18 13:13:14
问题 I use the following code to read BigEndian information using BinaryReader but I'm not sure if it is the efficient way of doing it. Is there any better solution? Here is my code: // some code to initialize the stream value // set the length value to the Int32 size BinaryReader reader =new BinaryReader(stream); byte[] bytes = reader.ReadBytes(length); Array.Reverse(bytes); int result = System.BitConverter.ToInt32(temp, 0); 回答1: BitConverter.ToInt32 isn't very fast in the first place. I'd simply

Why does Code Analysis tell me, “Do not dispose objects multiple times” here:

[亡魂溺海] 提交于 2019-12-18 09:09:22
问题 On this code: public static string Base64FromFileName(string fileName) { try { FileInfo fInfo = new FileInfo(fileName); long numBytes = fInfo.Length; FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fStream); byte[] bdata = br.ReadBytes((int)numBytes); br.Close(); fStream.Close(); return Convert.ToBase64String(bdata); } catch(Exception e) { throw e; } } ...I get, courtesy of Visual Studio's Code Analysis tool, the warning, " Do