From bdced63928bf1ffe9bf151dfef79d873ae4c5b18 Mon Sep 17 00:00:00 2001 From: Matthias Dittrich Date: Sat, 4 Jun 2016 18:08:46 +0200 Subject: [PATCH] fix demo stream reading --- demo/wordpad.py | 9 +++++---- src/tests/test_method.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/demo/wordpad.py b/demo/wordpad.py index 2f60759ff..e9373b3eb 100644 --- a/demo/wordpad.py +++ b/demo/wordpad.py @@ -293,12 +293,13 @@ def OpenDocument(self): stream = File.OpenRead(filename) buff = System.Array.CreateInstance(System.Byte, 1024) + buff.Initialize() data = [] + read = 1 - while stream.Position < stream.Length: - buff.Initialize() - stream.Read(buff, 0, 1024) - temp = Encoding.ASCII.GetString(buff, 0, 1024) + while read > 0: + read, _ = stream.Read(buff, 0, 1024) + temp = Encoding.ASCII.GetString(buff, 0, read) data.append(temp) data = ''.join(data) diff --git a/src/tests/test_method.py b/src/tests/test_method.py index 4ddade755..4728d13e4 100644 --- a/src/tests/test_method.py +++ b/src/tests/test_method.py @@ -759,6 +759,28 @@ def test(): self.assertRaises(TypeError, test) + def testWeCanBindToEncodingGetString(self): + """Check that we can bind to the Encoding.GetString method with variables.""" + + from System.Text import Encoding + from System.IO import MemoryStream + myBytes = Encoding.UTF8.GetBytes('Some testing string') + stream = MemoryStream() + stream.Write(myBytes, 0, myBytes.Length) + stream.Position = 0 + + buff = System.Array.CreateInstance(System.Byte, 3) + buff.Initialize() + data = [] + read = 1 + + while read > 0: + read, _ = stream.Read(buff, 0, buff.Length) + temp = Encoding.UTF8.GetString(buff, 0, read) + data.append(temp) + + data = ''.join(data) + self.assertEqual(data, 'Some testing string') def test_suite(): return unittest.makeSuite(MethodTests)