[Mono-list] BinaryReader class

Dick Porter dick@ximian.com
15 Aug 2002 11:13:34 +0100


--=-0MLcj8VPCGqVBHhfYxzA
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

This is a copy of a conversation I had with Serge a day or so ago, about
some problems I'm having with the BinaryReader class (I asked him,
because he was the last person to commit code changes here.)

Do you have any thoughts re. the buffering issue?  The .net docs for
this class are extremely terse, but don't mention any buffering (hence
my thoughts about leaving that to the underlying stream.)

- Dick





--=-0MLcj8VPCGqVBHhfYxzA
Content-Disposition: inline
Content-Type: message/rfc822

Subject: BinaryReader
From: Dick Porter <dick@ximian.com>
To: serge@wildwestsoftware.com
Content-Type: multipart/mixed; boundary="=-xP0OBPfSYY5tahJ3fMqS"
X-Mailer: Ximian Evolution 1.0.8 
Date: 13 Aug 2002 17:53:41 +0100
Message-Id: <1029257621.3247.56.camel@hagbard.apathetic.discordia.org.uk>
Mime-Version: 1.0
X-Evolution-Transport: sendmail:
X-Evolution-Account: dick@ximian.com
X-Evolution-Fcc: file:///home/dick/evolution/local/Sent/subfolders/Ximian
X-Evolution-Format: text/plain


--=-xP0OBPfSYY5tahJ3fMqS
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

I'm having problems with BinaryReader, and I think I've tracked it down
to a change you made a few months ago.  Specifically, in
BinaryReader.EnsureBuffered(), you added:



	if (m_buffer_used + needed > m_buffer.Length) {
		byte[] old_buffer = m_buffer;
		m_buffer = new byte[m_buffer_used + needed];
		Array.Copy(old_buffer, 0, m_buffer, 0, m_buffer_used);
+		m_buffer_pos = m_buffer_used;
	}

That extra line is moving the buffer read position to the end of the
newly grown buffer, skipping unread data whenever EnsureBuffered() is
called before the buffer is exhausted.

I'm also having trouble with the buffering when it pre-reads too much
from the underlying stream.  With a trivial test case (attached), I get
the following results on Windows and Linux:

Windows:
$ ./readtest.exe 
Read string "String 1"
Stream pos 9
Read int 1234
Stream pos 13
Read string "String 2"
Stream pos 22


Linux (with that m_buffer_pos line removed):
:; ./mono/interpreter/mint ../readtest.exe
Read string "String 1"
Stream pos 22
Read int 1234
Stream pos 22
Read string "String 2"
Stream pos 22


The buffer position issue is important for me at the moment because I'm
implementing System.Resources.ResourceReader, which reads with
BinaryReader but at one point must skip over a number of padding bytes
to 8-byte align the next field.


- Dick


--=-xP0OBPfSYY5tahJ3fMqS
Content-Disposition: attachment; filename=readtest.cs
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=readtest.cs; charset=ISO-8859-1


using System;
using System.IO;
using System.Text;

public class Test {
	public static int Main () {
		FileStream instr=3Dnew FileStream("binarywriter.data", FileMode.Open);
		BinaryReader reader=3Dnew BinaryReader(instr, Encoding.UTF8);

		string str;
		int i;

		str=3Dreader.ReadString();
		Console.WriteLine("Read string \"{0}\"", str);
		Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);

		i=3Dreader.ReadInt32();
		Console.WriteLine("Read int {0}", i);
		Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);

		str=3Dreader.ReadString();
		Console.WriteLine("Read string \"{0}\"", str);
		Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);

		return 0;
	}
}


--=-xP0OBPfSYY5tahJ3fMqS
Content-Disposition: attachment; filename=readtest.exe
Content-Type: application/x-ms-dos-executable; name=readtest.exe
Content-Transfer-Encoding: base64

TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1v
ZGUuDQ0KJAAAAAAAAABQRQAATAEDAGI0WT0AAAAAAAAAAOAADgELAQYAAAYAAAAGAAAAAAAA/iQA
AAAgAAAAQAAAAABAAAAgAAAAAgAABAAAAAAAAAAEAAAAAAAAAACAAAAAAgAAAAAAAAMAAAAAABAA
ABAAAAAAEAAAEAAAAAAAABAAAAAAAAAAAAAAAKwkAABPAAAAAEAAADgDAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=

--=-xP0OBPfSYY5tahJ3fMqS
Content-Disposition: attachment; filename=binarywriter.data
Content-Type: application/octet-stream; name=binarywriter.data
Content-Transfer-Encoding: base64

CFN0cmluZyAx0gQAAAhTdHJpbmcgMg==

--=-xP0OBPfSYY5tahJ3fMqS--


--=-0MLcj8VPCGqVBHhfYxzA
Content-Disposition: inline
Content-Type: message/rfc822

Return-Path: <serge@wildwestsoftware.com>
Delivered-To: dick@hagbard.apathetic.discordia.org.uk
Received: from localhost (hagbard.apathetic.discordia.org.uk
	[194.168.151.161]) by hagbard.apathetic.discordia.org.uk (Postfix) with
	ESMTP id 04353AF11C for <dick@hagbard.apathetic.discordia.org.uk>; Tue, 13
	Aug 2002 18:22:22 +0100 (BST)
Received: from peabody.ximian.com [141.154.95.10] by localhost with POP3
	(fetchmail-5.9.11) for dick@hagbard.apathetic.discordia.org.uk
	(single-drop); Tue, 13 Aug 2002 18:22:22 +0100 (BST)
Received: (qmail 29362 invoked from network); 13 Aug 2002 17:21:51 -0000
Received: from trna.ximian.com (141.154.95.22) by peabody.ximian.com with
	SMTP; 13 Aug 2002 17:21:51 -0000
Received: from sheffield.cnchost.com (sheffield.concentric.net
	[207.155.252.12]) by trna.ximian.com (8.11.6/8.11.6) with ESMTP id
	g7DHLC305230 for <dick@ximian.com>; Tue, 13 Aug 2002 13:21:13 -0400
Received: from mainmachine (t57.Card.Odessa.UA [195.138.72.57]) by
	sheffield.cnchost.com id NAA28496; Tue, 13 Aug 2002 13:21:39 -0400 (EDT)
	[ConcentricHost SMTP Relay 1.14]
Errors-To: <serge@wildwestsoftware.com>
Message-ID: <002401c242ed$fbcd18c0$39488ac3@mainmachine>
From: "Serge" <serge@wildwestsoftware.com>
To: "Dick Porter" <dick@ximian.com>
References: <1029257621.3247.56.camel@hagbard.apathetic.discordia.org.uk>
Subject: Re: BinaryReader
Date: Tue, 13 Aug 2002 20:22:06 +0300
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2462.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2462.0000
X-Spam-Status: No, hits=0.0 required=5.0 tests=AWL version=2.20
X-Spam-Level: 
X-Evolution-Source: mbox:/var/mail/dick

Hi Dick,

> I'm having problems with BinaryReader, and I think I've tracked it down
> to a change you made a few months ago.

I see. The changes were made in a hurry, so all sorts of nasty errors possible :-)
I initially planned to rewrite the class, since almost everything was broken (the very
first attempt to read resulted in an exception), but just patched it here and there,
enough to make sure that my own code worked with it.
I will look into this.

Serge


----- Original Message ----- 
From: "Dick Porter" <dick@ximian.com>
To: <serge@wildwestsoftware.com>
Sent: Tuesday, August 13, 2002 7:53 PM
Subject: BinaryReader


> I'm having problems with BinaryReader, and I think I've tracked it down
> to a change you made a few months ago.  Specifically, in
> BinaryReader.EnsureBuffered(), you added:
> 
> 
> 
> if (m_buffer_used + needed > m_buffer.Length) {
> byte[] old_buffer = m_buffer;
> m_buffer = new byte[m_buffer_used + needed];
> Array.Copy(old_buffer, 0, m_buffer, 0, m_buffer_used);
> + m_buffer_pos = m_buffer_used;
> }
> 
> That extra line is moving the buffer read position to the end of the
> newly grown buffer, skipping unread data whenever EnsureBuffered() is
> called before the buffer is exhausted.
> 
> I'm also having trouble with the buffering when it pre-reads too much
> from the underlying stream.  With a trivial test case (attached), I get
> the following results on Windows and Linux:
> 
> Windows:
> $ ./readtest.exe 
> Read string "String 1"
> Stream pos 9
> Read int 1234
> Stream pos 13
> Read string "String 2"
> Stream pos 22
> 
> 
> Linux (with that m_buffer_pos line removed):
> :; ./mono/interpreter/mint ../readtest.exe
> Read string "String 1"
> Stream pos 22
> Read int 1234
> Stream pos 22
> Read string "String 2"
> Stream pos 22
> 
> 
> The buffer position issue is important for me at the moment because I'm
> implementing System.Resources.ResourceReader, which reads with
> BinaryReader but at one point must skip over a number of padding bytes
> to 8-byte align the next field.
> 
> 
> - Dick
> 
> 


--=-0MLcj8VPCGqVBHhfYxzA
Content-Disposition: inline
Content-Type: message/rfc822

Return-Path: <serge@wildwestsoftware.com>
Delivered-To: dick@hagbard.apathetic.discordia.org.uk
Received: from localhost (hagbard.apathetic.discordia.org.uk
	[194.168.151.161]) by hagbard.apathetic.discordia.org.uk (Postfix) with
	ESMTP id 0287AAF72C for <dick@hagbard.apathetic.discordia.org.uk>; Tue, 13
	Aug 2002 22:02:51 +0100 (BST)
Received: from peabody.ximian.com [141.154.95.10] by localhost with POP3
	(fetchmail-5.9.11) for dick@hagbard.apathetic.discordia.org.uk
	(single-drop); Tue, 13 Aug 2002 22:02:52 +0100 (BST)
Received: (qmail 2596 invoked from network); 13 Aug 2002 20:58:51 -0000
Received: from trna.ximian.com (141.154.95.22) by peabody.ximian.com with
	SMTP; 13 Aug 2002 20:58:51 -0000
Received: from tonnant.cnchost.com (tonnant.concentric.net
	[207.155.248.72]) by trna.ximian.com (8.11.6/8.11.6) with ESMTP id
	g7DKwC321900 for <dick@ximian.com>; Tue, 13 Aug 2002 16:58:12 -0400
Received: from mainmachine (t20-78.Card.Odessa.UA [195.138.78.20]) by
	tonnant.cnchost.com id QAA16108; Tue, 13 Aug 2002 16:58:41 -0400 (EDT)
	[ConcentricHost SMTP Relay 1.14]
Errors-To: <serge@wildwestsoftware.com>
Message-ID: <002b01c2430c$4dc01e20$144e8ac3@mainmachine>
From: "Serge" <serge@wildwestsoftware.com>
To: "Dick Porter" <dick@ximian.com>
References: <1029257621.3247.56.camel@hagbard.apathetic.discordia.org.uk>
	<002201c242f8$2a0ccdc0$3b488ac3@mainmachine>
	<1029266306.2959.63.camel@hagbard.apathetic.discordia.org.uk>
Subject: Re: BinaryReader
Date: Tue, 13 Aug 2002 23:59:15 +0300
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2462.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2462.0000
X-Spam-Status: No, hits=0.0 required=5.0 tests=AWL version=2.20
X-Spam-Level: 
X-Evolution-Source: mbox:/var/mail/dick

> I'm also having trouble with the buffering when it pre-reads too much
> from the underlying stream. With a trivial test case (attached), I get
> the following results on Windows and Linux:

Well, the string/char reading is broken and must be rewritten.
In Read(char[] buffer, int index, int count)
  EnsureBuffered(m_encoding_max_byte * count);
This call advances base stream position too far for multibyte encodings.
ReadChar(s) has the same side-effect.
And so is PeekChar, it also doesn't reset the base stream pos.
All in all, major cleanup is needed for this class.
What if we ask the original author, maybe he will be interested to look into this?

Serge





--=-0MLcj8VPCGqVBHhfYxzA
Content-Disposition: inline
Content-Type: message/rfc822

Subject: Re: BinaryReader
From: Dick Porter <dick@ximian.com>
To: Serge <serge@wildwestsoftware.com>
In-Reply-To: <002b01c2430c$4dc01e20$144e8ac3@mainmachine>
References: <1029257621.3247.56.camel@hagbard.apathetic.discordia.org.uk>
	<002201c242f8$2a0ccdc0$3b488ac3@mainmachine>
	<1029266306.2959.63.camel@hagbard.apathetic.discordia.org.uk> 
	<002b01c2430c$4dc01e20$144e8ac3@mainmachine>
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
X-Mailer: Ximian Evolution 1.0.8 
Date: 13 Aug 2002 22:25:36 +0100
Message-Id: <1029273936.2959.70.camel@hagbard.apathetic.discordia.org.uk>
Mime-Version: 1.0
X-Evolution-Transport: sendmail:
X-Evolution-Account: dick@ximian.com
X-Evolution-Fcc: file:///home/dick/evolution/local/Sent/subfolders/Ximian
X-Evolution-Format: text/plain

On Tue, 2002-08-13 at 21:59, Serge wrote:
> > I'm also having trouble with the buffering when it pre-reads too much
> > from the underlying stream. With a trivial test case (attached), I get
> > the following results on Windows and Linux:
> 
> Well, the string/char reading is broken and must be rewritten.
> In Read(char[] buffer, int index, int count)
>   EnsureBuffered(m_encoding_max_byte * count);
> This call advances base stream position too far for multibyte encodings.
> ReadChar(s) has the same side-effect.
> And so is PeekChar, it also doesn't reset the base stream pos.

I'm wondering if we shouldn't just rip all of the buffering out of
BinaryReader and let the underlying stream do all that.

> All in all, major cleanup is needed for this class.
> What if we ask the original author, maybe he will be interested to look into this?

Looks like that will be the best option.  I'll post to mono-list
tomorrow.

- Dick



--=-0MLcj8VPCGqVBHhfYxzA
Content-Disposition: inline
Content-Type: message/rfc822

Subject: Re: BinaryReader
From: Dick Porter <dick@ximian.com>
To: Serge <serge@wildwestsoftware.com>
In-Reply-To: <001001c2438f$1a4355c0$25488ac3@mainmachine>
References:
	<1029257621.3247.56.camel@hagbard.apathetic.discordia.org.uk><002201c242f8$2
	 
	a0ccdc0$3b488ac3@mainmachine><1029266306.2959.63.camel@hagbard.apathetic.dis
	 cordia.org.uk> <002b01c2430c$4dc01e20$144e8ac3@mainmachine>
	<1029313844.3247.75.camel@hagbard.apathetic.discordia.org.uk> 
	<001001c2438f$1a4355c0$25488ac3@mainmachine>
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
X-Mailer: Ximian Evolution 1.0.8 
Date: 14 Aug 2002 14:31:59 +0100
Message-Id: <1029331919.7881.1.camel@hagbard.apathetic.discordia.org.uk>
Mime-Version: 1.0
X-Evolution-Transport: sendmail:
X-Evolution-Account: dick@ximian.com
X-Evolution-Fcc: file:///home/dick/evolution/local/Sent/subfolders/Ximian
X-Evolution-Format: text/plain

On Wed, 2002-08-14 at 13:35, Serge wrote:
> Hi Dick,
> 
> > Is it OK if I post some or all of yesterday's conversation to the list?
> 
> It's perfectly OK!

Thanks.

By the way, I think I know which of my changes is responsible for fixing
that unicode.c assert you saw:

--- mono/metadata/unicode.c	6 Aug 2002 09:22:07 -0000	1.17
+++ mono/metadata/unicode.c	14 Aug 2002 13:31:33 -0000
@@ -459,7 +459,7 @@
 	g_assert (chars);
 	g_assert (bytes);
 	g_assert (mono_array_length (bytes) > byteIndex);
-	g_assert (mono_array_length (chars) >= (byteIndex + byteCount));
+	g_assert (mono_array_length (bytes) >= (byteIndex + byteCount));
 	g_assert (mono_array_length (chars) > charIndex);


- Dick



--=-0MLcj8VPCGqVBHhfYxzA
Content-Disposition: attachment; filename=readtest.cs
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=readtest.cs; charset=ISO-8859-1


using System;
using System.IO;
using System.Text;

public class Test {
	public static int Main () {
		FileStream instr=3Dnew FileStream("binarywriter.data", FileMode.Open);
		BinaryReader reader=3Dnew BinaryReader(instr, Encoding.UTF8);

		string str;
		int i;

		str=3Dreader.ReadString();
		Console.WriteLine("Read string \"{0}\"", str);
		Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);

		i=3Dreader.ReadInt32();
		Console.WriteLine("Read int {0}", i);
		Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);

		str=3Dreader.ReadString();
		Console.WriteLine("Read string \"{0}\"", str);
		Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);

		return 0;
	}
}


--=-0MLcj8VPCGqVBHhfYxzA
Content-Disposition: attachment; filename=readtest.exe
Content-Type: application/x-ms-dos-executable; name=readtest.exe
Content-Transfer-Encoding: base64

TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1v
ZGUuDQ0KJAAAAAAAAABQRQAATAEDAGI0WT0AAAAAAAAAAOAADgELAQYAAAYAAAAGAAAAAAAA/iQA
AAAgAAAAQAAAAABAAAAgAAAAAgAABAAAAAAAAAAEAAAAA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AAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=

--=-0MLcj8VPCGqVBHhfYxzA
Content-Disposition: attachment; filename=binarywriter.data
Content-Type: application/octet-stream; name=binarywriter.data
Content-Transfer-Encoding: base64

CFN0cmluZyAx0gQAAAhTdHJpbmcgMg==

--=-0MLcj8VPCGqVBHhfYxzA--