导出 (0) 打印
全部展开
信息
您所需的主题如下所示。但此主题未包含在此库中。

DataReader Class

从输入流读取数据。

语法


var dataReader = new Windows.Storage.Streams.DataReader();

特性

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

成员

DataReader类 具有这些类型的成员:

构造函数

DataReader类 具有这些构造函数。

构造函数说明
DataReader Creates and initializes a new instance of the data reader.

 

方法

The DataReader 类 具有这些方法。 对于 C#、Visual Basic 和 C++,它还继承方法于Object 类.

方法说明
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.

 

属性

DataReader类 具有这些属性。

属性访问类型说明

ByteOrder

读/写Gets or sets the byte order of the data in the input stream.

InputStreamOptions

读/写Gets or sets the read options for the input stream.

UnconsumedBufferLength

只读Gets the size of the buffer that has not been read.

UnicodeEncoding

读/写Gets or sets the Unicode character encoding for the input stream.

 

示例

下面的示例显示如何向内存流写入并读取字符串。有关完整的代码示例,请参见读取和写入数据示例



(function () {
    "use strict";
    var page = WinJS.UI.Pages.define("/html/write-read-stream.html", {
        ready: function (element, options) {
            var sourceElement = document.getElementById("ElementsToSend");
            sourceElement.innerHTML = "Hello;World;1 2 3 4 5;Très bien!;Goodbye";
            var sendButton = document.getElementById("SendButton");
            sendButton.addEventListener("click", transferData);
        }
    });

function transferData() {
        var sourceElement = document.getElementById("ElementsToSend");
        var destinationElement = document.getElementById("scenario1Output");

        // First a DataWriter object is created, backed by an in-memory stream where 
        // the data will be stored.
        var writer = Windows.Storage.Streams.DataWriter(
            new Windows.Storage.Streams.InMemoryRandomAccessStream());
        writer.unicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
        writer.byteOrder = Windows.Storage.Streams.ByteOrder.littleEndian;

        // We separate the contents of the sourceElement div in multiple strings 
        // using ';' as the separator. Each string will be written separately.
        var elements = sourceElement.innerHTML.split(";");
        elements.forEach(function (element) {
            var codeUnits = writer.measureString(element);
            writer.writeInt32(codeUnits);
            writer.writeString(element);
        });

        var reader;
        var stream;

        // The call to store async sends the actual contents of the writer 
        // to the backing stream.
        writer.storeAsync().then(function () {
            // For the in-memory stream implementation we are using, the flushAsync call 
            // is superfluous, but other types of streams may require it.
            return writer.flushAsync();
        }).then(function () {
            // We detach the stream to prolong its useful lifetime. Were we to fail 
            // to detach the stream, the call to writer.close() would close the underlying 
            // stream, preventing its subsequent use by the DataReader below. Most clients 
            // of DataWriter will have no reason to use the underlying stream after 
            // writer.close() is called, and will therefore have no reason to call
            // writer.detachStream(). Note that once we detach the stream, we assume 
            // responsibility for closing the stream subsequently; after the stream 
            // has been detached, a call to writer.close() will have no effect on the stream.
            stream = writer.detachStream();
            // Make sure the stream is read from the beginning in the reader 
            // we are creating below.
            stream.seek(0);
            // Most DataWriter clients will not call writer.detachStream(), 
            // and furthermore will be working with a file-backed or network-backed stream, 
            // rather than an in-memory-stream. In such cases, it would be particularly 
            // important to call writer.close(). Doing so is always a best practice.
            writer.close();

            reader = new Windows.Storage.Streams.DataReader(stream);
            // The encoding and byte order need to match the settings of the writer 
            // we previously used.
            reader.unicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
            reader.byteOrder = Windows.Storage.Streams.ByteOrder.littleEndian;
            // Once we have written the contents successfully we load the stream, 
            // this is also an asynchronous operation
            return reader.loadAsync(stream.size);
        }).done(function () {
            var receivedStrings = "";
            // Keep reading until we consume the complete stream
            while (reader.unconsumedBufferLength > 0) {
                // Note that the call to readString requires a length of "code units" 
                // to read. This is the reason each string is preceeded by its length 
                // when "on the wire".
                var codeUnitsToRead = reader.readInt32();
                receivedStrings += reader.readString(codeUnitsToRead) + "<br/>";
            }
            // Calling reader.close() closes the underlying stream. It would be particularly important
            // to call reader.close() if the underlying stream were file-backed or 
            // network-backed. Note that this call to reader.close() satisfies 
            // our obligation to close the stream previously detached from DataReader.
            reader.close();
            destinationElement.innerHTML = receivedStrings;
        });
    };
})();


要求

最低支持客户端

Windows 8 [仅 Windows 应用商店应用程序]

最低支持服务器

Windows Server 2012 [仅 Windows 应用商店应用程序]

最低支持电话

Windows Phone 8

命名空间

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

元数据

Windows.winmd

另请参见

读取和写入数据示例
StreamSocket 示例
DataReaderLoadOperation
DataWriter

 

 

显示:
© 2014 Microsoft