1 out of 4 rated this helpful - Rate this topic

DataReader class

Applies to Windows and Windows Phone

Reads data from an input stream.

Syntax


public sealed class DataReader : IDataReader,
    IDisposable

Attributes

[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]

Members

The DataReader class has these types of members:

Constructors

The DataReader class has these constructors.

ConstructorDescription
DataReader Creates and initializes a new instance of the data reader.

 

Methods

The DataReader class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
Close [C++, JavaScript]Closes the current stream and releases system resources.
DetachBuffer Detaches the buffer that is associated with the data reader.
DetachStream Detaches the stream that is associated with the data reader.
Dispose [C#, VB]Performs tasks associated with freeing, releasing, or resetting unmanaged resources.
FromBuffer Creates a new instance of the data reader with data from the specified buffer.
LoadAsync Loads data from the input stream.
ReadBoolean Reads a Boolean value from the input stream.
ReadBuffer Reads a buffer from the input stream.
ReadByte Reads a byte value from the input stream.
ReadBytes Reads an array of byte values from the input stream.
ReadDateTime Reads a date and time value from the input stream.
ReadDouble Reads a floating-point value from the input stream.
ReadGuid Reads a GUID value from the input stream.
ReadInt16 Reads a 16-bit integer value from the input stream.
ReadInt32 Reads a 32-bit integer value from the input stream.
ReadInt64 Reads a 64-bit integer value from the input stream.
ReadSingle Reads a floating-point value from the input stream.
ReadString Reads a string value from the input stream.
ReadTimeSpan Reads a time-interval value from the input stream.
ReadUInt16 Reads a 16-bit unsigned integer from the input stream.
ReadUInt32 Reads a 32-bit unsigned integer from the input stream.
ReadUInt64 Reads a 64-bit unsigned integer from the input stream.

 

Properties

The DataReader class has these properties.

PropertyAccess typeDescription

ByteOrder

Read/writeGets or sets the byte order of the data in the input stream.

InputStreamOptions

Read/writeGets or sets the read options for the input stream.

UnconsumedBufferLength

Read-onlyGets the size of the buffer that has not been read.

UnicodeEncoding

Read/writeGets or sets the Unicode character encoding for the input stream.

 

Examples

The following example shows how to write and read strings to an in-memory stream. For the full code sample, see Reading and writing data sample.



using System;
using System.Diagnostics;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

// This is the click handler for the 'Copy Strings' button.  Here we will parse the
// strings contained in the ElementsToWrite text block, write them to a stream using
// DataWriter, retrieve them using DataReader, and output the results in the
// ElementsRead text block.
private async void TransferData(object sender, RoutedEventArgs e)
{
    // Initialize the in-memory stream where data will be stored.
    using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
    {
        // Create the data writer object backed by the in-memory stream.
        using (var dataWriter = new Windows.Storage.Streams.DataWriter(stream))
        {
            dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
            dataWriter.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;

            // Parse the input stream and write each element separately.
            string[] inputElements = ElementsToWrite.Text.Split(';');
            foreach (string inputElement in inputElements)
            {
                uint inputElementSize = dataWriter.MeasureString(inputElement);
                dataWriter.WriteUInt32(inputElementSize);
                dataWriter.WriteString(inputElement);
            }

            // Send the contents of the writer to the backing stream.
            await dataWriter.StoreAsync();

            // For the in-memory stream implementation we are using, the flushAsync call 
            // is superfluous,but other types of streams may require it.
            await dataWriter.FlushAsync();

            // In order to prolong the lifetime of the stream, detach it from the 
            // DataWriter so that it will not be closed when Dispose() is called on 
            // dataWriter. Were we to fail to detach the stream, the call to 
            // dataWriter.Dispose() would close the underlying stream, preventing 
            // its subsequent use by the DataReader below.
            dataWriter.DetachStream();
        }

        // Create the input stream at position 0 so that the stream can be read 
        // from the beginning.
        using (var inputStream = stream.GetInputStreamAt(0))
        {
            using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
            {
                // The encoding and byte order need to match the settings of the writer 
                // we previously used.
                dataReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                dataReader.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;

                // Once we have written the contents successfully we load the stream.
                await dataReader.LoadAsync((uint)stream.Size);

                var receivedStrings = "";

                // Keep reading until we consume the complete stream.
                while (dataReader.UnconsumedBufferLength > 0)
                {
                    // Note that the call to readString requires a length of "code units" 
                    // to read. This is the reason each string is preceded by its length 
                    // when "on the wire".
                    uint bytesToRead = dataReader.ReadUInt32();
                    receivedStrings += dataReader.ReadString(bytesToRead) + "\n";
                }

                // Populate the ElementsRead text block with the items we read 
                // from the stream.
                ElementsRead.Text = receivedStrings;
            }
        }
    }
}

Requirements

Minimum supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Minimum supported phone

Windows Phone 8

Namespace

Windows.Storage.Streams
Windows::Storage::Streams [C++]

Metadata

Windows.winmd

See also

Reading and writing data sample
StreamSocket sample
DataReaderLoadOperation
DataWriter

 

 

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.