当BaseStream.Position<时,BinaryReader EndOfStreamException BaseStream.Length

时间:2018-04-15 17:25:08

标签: c# binaryreader

在使用BinaryReader时,当ReadUInt16()等于20且BaseStream.Position等于174时,如何在BaseStream.Length上获得EndOfStreamException?我在文档中找不到任何指示如何发生这种情况的内容。

EndOfStreamException: Failed to read past end of stream.
System.IO.BinaryReader.FillBuffer (Int32 numBytes) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:119)
System.IO.BinaryReader.ReadUInt16 () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:513)
Chunk.SaveChunk () (at Assets/Scripts/Chunk.cs:293)

以下是我认为在流中的确切数据。此数据是从程序Hex Editor Neo中检索的,而Visual Studio在异常行的断点处暂停。

01 00 00 00 00 00 00 00 01 00 a0 00 00 00 00 00
ff ff 00 00 13 00 00 00 00 10 0c 10 02 00 00 0c
00 0b 01 03 01 00 00 0c 03 04 02 0d 02 00 00 0e
03 01 01 0d 01 00 01 0e 03 03 01 09 01 00 01 0e
0c 0f 02 04 02 00 04 0c 03 06 01 01 01 00 04 0c
04 0c 02 0c 02 00 04 0e 04 0a 01 05 01 00 04 0e
09 07 02 03 02 00 0a 0c 03 06 02 01 02 00 0a 0e
03 06 01 01 01 00 0b 0c 00 05 02 03 02 00 0b 0e
00 05 01 03 01 00 0b 0e 09 01 01 01 01 00 0b 0e
0a 05 02 02 02 00 0c 0e 09 04 02 01 02 00 0e 0e
04 02 01 04 01 00 0e 0e 08 02 02 01 02 00

编辑:打开流直到异常的代码。

public void SaveChunk()
{
    lock (chunkDataLock)
    {
        FileStream readStream = File.Open("GameData/chunkdata.bin", FileMode.Open, FileAccess.Read, FileShare.Write);
        FileStream writeStream = File.Open("GameData/chunkdata.bin", FileMode.Open, FileAccess.Write, FileShare.Read);
        using (BinaryReader reader = new BinaryReader(readStream))
        using (BinaryWriter writer = new BinaryWriter(writeStream))
        {
            //reader.BaseStream.Length == 174 here
            if (reader.BaseStream.Length == 0)
            {
                writer.Write((ushort)0);
                writer.Flush();
                writer.BaseStream.Position = 0;
                reader.BaseStream.Position = 0;
            }

            // 0, 0, 0
            int[] regionPos = GetRegionFromChunkPosition(chunkPosX, chunkPosY, chunkPosZ);

            // numRegions = 1
            ushort numRegions = reader.ReadUInt16();
            writer.BaseStream.Position += 2;
            for (int i = 0; i < numRegions; i++)
            {
                // 0, 0, 0
                short regXPos = reader.ReadInt16(), regYPos = reader.ReadInt16(), regZPos = reader.ReadInt16();
                // numChunksPosition = 8
                long numChunksPosition = reader.BaseStream.Position;
                // numChunks = 1
                ushort numChunks = reader.ReadUInt16();
                // regionLengthPosition = 10
                long regionLengthPosition = reader.BaseStream.Position;
                // regionLength = 160
                uint regionLength = reader.ReadUInt32();

                uint newRegionLength = 0;
                writer.BaseStream.Position += 12;

                // At this point reader and writer both have BaseStream.Position of 14

                // True
                if (regXPos == regionPos[0] && regYPos == regionPos[1] && regZPos == regionPos[2])
                {
                    bool chunkFound = false;
                    for (ushort j = 0; j < numChunks; j++)
                    {
                        // 0, -1, 0
                        short xPos = reader.ReadInt16(), yPos = reader.ReadInt16(), zPos = reader.ReadInt16();
                        writer.BaseStream.Position += 6;

                        // Exception occurs on this line.
                        // at this point, reader.BaseStream.Position = 20
                        // and reader.BaseStream.Length = 174
                        ushort numVoxelGroups = reader.ReadUInt16();
                        ...
                    }
                    ...
                }
                ...
            }
            ...
        }
    }
}

0 个答案:

没有答案