Skip to content

Commit d58b338

Browse files
matthidtonyroberts
authored andcommitted
fix demo stream reading (#225)
1 parent 6139989 commit d58b338

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

demo/wordpad.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,13 @@ def OpenDocument(self):
293293
stream = File.OpenRead(filename)
294294

295295
buff = System.Array.CreateInstance(System.Byte, 1024)
296+
buff.Initialize()
296297
data = []
298+
read = 1
297299

298-
while stream.Position < stream.Length:
299-
buff.Initialize()
300-
stream.Read(buff, 0, 1024)
301-
temp = Encoding.ASCII.GetString(buff, 0, 1024)
300+
while read > 0:
301+
read, _ = stream.Read(buff, 0, 1024)
302+
temp = Encoding.ASCII.GetString(buff, 0, read)
302303
data.append(temp)
303304

304305
data = ''.join(data)

src/tests/test_method.py

+22
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,28 @@ def test():
759759

760760
self.assertRaises(TypeError, test)
761761

762+
def testWeCanBindToEncodingGetString(self):
763+
"""Check that we can bind to the Encoding.GetString method with variables."""
764+
765+
from System.Text import Encoding
766+
from System.IO import MemoryStream
767+
myBytes = Encoding.UTF8.GetBytes('Some testing string')
768+
stream = MemoryStream()
769+
stream.Write(myBytes, 0, myBytes.Length)
770+
stream.Position = 0
771+
772+
buff = System.Array.CreateInstance(System.Byte, 3)
773+
buff.Initialize()
774+
data = []
775+
read = 1
776+
777+
while read > 0:
778+
read, _ = stream.Read(buff, 0, buff.Length)
779+
temp = Encoding.UTF8.GetString(buff, 0, read)
780+
data.append(temp)
781+
782+
data = ''.join(data)
783+
self.assertEqual(data, 'Some testing string')
762784

763785
def test_suite():
764786
return unittest.makeSuite(MethodTests)

0 commit comments

Comments
 (0)