Click here to Skip to main content
14,740,560 members
Home » Discussions » C#
   

C#

 
QuestionWhat's Wrong With This??? Pin
Kevin Marois8-Nov-20 20:00
professionalKevin Marois8-Nov-20 20:00 
AnswerRe: What's Wrong With This??? Pin
Jörgen Andersson8-Nov-20 21:11
professionalJörgen Andersson8-Nov-20 21:11 
AnswerRe: What's Wrong With This??? Pin
Richard Deeming8-Nov-20 22:47
mveRichard Deeming8-Nov-20 22:47 
GeneralRe: What's Wrong With This??? Pin
Kevin Marois9-Nov-20 8:42
professionalKevin Marois9-Nov-20 8:42 
QuestionC# - Use app between 2 times and get the time from database Pin
Valakik8-Nov-20 13:16
MemberValakik8-Nov-20 13:16 
AnswerRe: C# - Use app between 2 times and get the time from database Pin
Dave Kreskowiak8-Nov-20 19:54
mveDave Kreskowiak8-Nov-20 19:54 
AnswerRe: C# - Use app between 2 times and get the time from database Pin
Gerry Schmitz9-Nov-20 2:51
mveGerry Schmitz9-Nov-20 2:51 
QuestionWeird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
pr1mem0ver6-Nov-20 17:24
Memberpr1mem0ver6-Nov-20 17:24 
I have made a custom filestream class that creates a copy of a file and uses the copy as the source to rewrite the file while making changes to values that match a specific criteria. Here is the basic setup:
public class MixedCompressionStreamRewriter : FileStream

It uses two modes: Parse mode reroutes the stream into a buffer to be analyzed and potentially rewritten before it gets sent to the output stream. When parse mode is false, it simply copies the base stream to the output stream. It also allows analysis of compressed data inside the stream because the source files will often have such data. Because of these use cases I had to override the Read method as such (commented so that one can trace where everything goes wrong in a debug session):
The issue occurs while executing the ReadUint32 call in the code below:
public static RecordHeaderBase FromFile(BinaryReader reader, uint internalIDMask = 0xFFFFFFFF)
{
	ModRecordConsolidator consolidator = reader.BaseStream as ModRecordConsolidator;
	if (consolidator != null)
		consolidator.EnableParseMode = true;
	RecordTypeInfo typeInfo = (RecordType)reader.ReadUInt32();
	if (consolidator != null)
		consolidator.EnableParseMode = false;
	if (typeInfo == RecordType.TES4)
		return new GameHeader(reader);
	else if (typeInfo.isContainer)
		return new GroupHeader(reader, typeInfo, internalIDMask);
	else
		return new RecordHeader(reader, typeInfo, internalIDMask);
}

This call leads to the execution of the overridden read method below which is where the actual problem occurs:
public override int Read(byte[] array, int offset, int count)
{
	int result;
	if (compressionHeaderStream == null) // condition is true when problem occurs
	{
		result = base.Read(array, offset, count); // ***THIS IS WHERE THE ISSUES IS ***//
		if (!parseMode)
		{
			long equivalentBufferLocation = MemoryBufferStart + MemoryBuffer.Length;
			if (Position > equivalentBufferLocation)
			{
				long bufferCopyBytes = Position - equivalentBufferLocation;
				if (bufferCopyBytes > int.MaxValue)
					throw new OverflowException();
				offset += (count - (int)bufferCopyBytes);
				MemoryBuffer.Write(array, offset, (int)bufferCopyBytes);
			}
			if (MaxBufferSize != 0)
				if (MemoryBuffer.Length >= MaxBufferSize)
					FlushToOutput();
		}
	}
	else if (MemoryBuffer == null)
		result = base.Read(array, offset, count);
	else
		result = MemoryBuffer.Read(array, offset, count);
	return result;
}

For some reason the *** starred line causes the file stream to read several thousand bytes rather than just four (which is confirmed by the value of count during debug). I have no idea why. During the base class read process, it accesses the following overridden methods:
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return true; } }

I have no idea why the base method (which is Microsoft provided) would do this.

EDIT: Even weirder: the base method returns that it only read 4 bytes. It might help to know that ModRecordConsolidator in the first code block inherits from MixedCompressionStreamRewriter (the class whose code is shown)

modified 6-Nov-20 22:35pm.

AnswerRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
Gerry Schmitz6-Nov-20 19:18
mveGerry Schmitz6-Nov-20 19:18 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
pr1mem0ver7-Nov-20 6:00
Memberpr1mem0ver7-Nov-20 6:00 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
Gerry Schmitz7-Nov-20 6:37
mveGerry Schmitz7-Nov-20 6:37 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
pr1mem0ver7-Nov-20 14:25
Memberpr1mem0ver7-Nov-20 14:25 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
Gerry Schmitz8-Nov-20 6:53
mveGerry Schmitz8-Nov-20 6:53 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
pr1mem0ver16-Nov-20 15:58
Memberpr1mem0ver16-Nov-20 15:58 
AnswerRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
OriginalGriff8-Nov-20 2:35
mveOriginalGriff8-Nov-20 2:35 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
pr1mem0ver16-Nov-20 16:10
Memberpr1mem0ver16-Nov-20 16:10 
AnswerRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
Richard Deeming8-Nov-20 22:39
mveRichard Deeming8-Nov-20 22:39 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
pr1mem0ver16-Nov-20 16:06
Memberpr1mem0ver16-Nov-20 16:06 
QuestionSystem.IO.File.WriteAllLines exception Pin
_Flaviu6-Nov-20 0:52
Member_Flaviu6-Nov-20 0:52 
AnswerRe: System.IO.File.WriteAllLines exception Pin
Richard MacCutchan6-Nov-20 1:01
mveRichard MacCutchan6-Nov-20 1:01 
AnswerRe: System.IO.File.WriteAllLines exception Pin
OriginalGriff6-Nov-20 1:18
mveOriginalGriff6-Nov-20 1:18 
GeneralRe: System.IO.File.WriteAllLines exception Pin
_Flaviu6-Nov-20 3:33
Member_Flaviu6-Nov-20 3:33 
AnswerRe: System.IO.File.WriteAllLines exception Pin
Richard MacCutchan6-Nov-20 6:21
mveRichard MacCutchan6-Nov-20 6:21 
GeneralRe: System.IO.File.WriteAllLines exception Pin
_Flaviu7-Nov-20 22:08
Member_Flaviu7-Nov-20 22:08 
QuestionHow to extract part of HTML source? Pin
Member 5697394-Nov-20 8:53
MemberMember 5697394-Nov-20 8:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.