CSharpTest.Net
ToTextReader Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Crypto Namespace > SecureStringUtils Class : ToTextReader Method

data

Glossary Item Box

Returns a stream of Unicode bytes from the give SecureString instance

Syntax

Visual Basic (Declaration) 
Public Shared Function ToTextReader( _
   ByVal data As SecureString _
) As TextReader
C# 
public static TextReader ToTextReader( 
   SecureString data
)

Parameters

data

Example

Library/Library.Test/TestSecureString.cs

C#Copy Code
foreach (SecureString ss in MakeStrings())
{
    Assert.AreEqual(TEST_PASSWORD, SecureStringUtils.ToTextReader(ss).ReadToEnd());

    StringBuilder sb = new StringBuilder();
    using (TextReader r = SecureStringUtils.ToTextReader(ss))
    {
        while (r.Peek() != -1)
        {
            sb.Append((char)r.Read());
            int i = r.Read();
            if (i != -1)
                sb.Append((char)i);
        }
    }
    Assert.AreEqual(TEST_PASSWORD, sb.ToString());

    char[] buffer = new char[TEST_PASSWORD.Length];
    using (TextReader r = new UnicodeReader(SecureStringUtils.ToStream(ss)))
    {
        for (int i = 0; i < buffer.Length; i++)
            Assert.AreEqual(1, r.ReadBlock(buffer, i, buffer.Length - i));
        Assert.AreEqual(0, r.ReadBlock(buffer, 0, buffer.Length));
    }
    Assert.AreEqual(TEST_PASSWORD, new string(buffer));
}
VB.NETCopy Code
For Each ss As SecureString In MakeStrings()
    Assert.AreEqual(TEST_PASSWORD, SecureStringUtils.ToTextReader(ss).ReadToEnd())

    Dim sb As New StringBuilder()
    Using r As TextReader = SecureStringUtils.ToTextReader(ss)
        While r.Peek() <> -1
            sb.Append(DirectCast(r.Read(), Char))
            Dim i As Integer = r.Read()
            If i <> -1 Then
                sb.Append(DirectCast(i, Char))
            End If
        End While
    End Using
    Assert.AreEqual(TEST_PASSWORD, sb.ToString())

    Dim buffer As Char() = New Char(TEST_PASSWORD.Length) {}
    Using r As TextReader = New UnicodeReader(SecureStringUtils.ToStream(ss))
        Dim i As Integer = 0
        While i < buffer.Length
            Assert.AreEqual(1, r.ReadBlock(buffer, i, buffer.Length - i))
            System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
        End While
        Assert.AreEqual(0, r.ReadBlock(buffer, 0, buffer.Length))
    End Using
    Assert.AreEqual(TEST_PASSWORD, New String(buffer))
Next

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys