DataWriter Class

출력 스트림에 데이터를 씁니다.

구문


var dataWriter = new Windows.Storage.Streams.DataWriter(iOutputStream);
var dataWriter = new Windows.Storage.Streams.DataWriter();

특성

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

멤버

DataWriter클래스에는 다음과 같은 형식의 멤버가 있습니다.

생성자

DataWriter클래스에는 다음과 같은 생성자가 있습니다.

생성자Description
DataWriter() Creates and initializes a new instance of the data writer.
DataWriter(IOutputStream) Creates and initializes a new instance of the data writer to an output stream.

 

메서드

The DataWriter 클래스 - 이러한 메서드가 있습니다. C#, Visual Basic 및 C++에서는 다음 위치의 메서드도 상속합니다. Object 클래스.

메서드Description
Close [C++, JavaScript]Closes the current stream and releases system resources.
DetachBuffer Detaches the buffer that is associated with the data writer.
DetachStream Detaches the stream that is associated with the data writer.
Dispose [C#, VB]Performs tasks associated with freeing, releasing, or resetting unmanaged resources.
FlushAsync Flushes data asynchronously.
MeasureString Gets the size of a string.
StoreAsync Commits data in the buffer to a backing store.
WriteBoolean Writes a Boolean value to the output stream.
WriteBuffer(IBuffer) Writes the contents of the specified buffer to the output stream.
WriteBuffer(IBuffer, UInt32, UInt32) Writes the specified bytes from a buffer to the output stream.
WriteByte Writes a byte value to the output stream.
WriteBytes Writes an array of byte values to the output stream.
WriteDateTime Writes a date and time value to the output stream.
WriteDouble Writes a floating-point value to the output stream.
WriteGuid Writes a GUID value to the output stream.
WriteInt16 Writes a 16-bit integer value to the output stream.
WriteInt32 Writes a 32-bit integer value to the output stream.
WriteInt64 Writes a 64-bit integer value to the output stream.
WriteSingle Writes a floating-point value to the output stream.
WriteString Writes a string value to the output stream.
WriteTimeSpan Writes a time-interval value to the output stream.
WriteUInt16 Writes a 16-bit unsigned integer value to the output stream.
WriteUInt32 Writes a 32-bit unsigned integer value to the output stream.
WriteUInt64 Writes a 64-bit unsigned integer value to the output stream.

 

속성

DataWriter클래스 - 이러한 속성이 있습니다.

속성사용 권한Description

ByteOrder

읽기/쓰기Gets or sets the byte order of the data in the output stream.

UnicodeEncoding

읽기/쓰기Gets or sets the Unicode character encoding for the output stream.

UnstoredBufferLength

읽기 전용Gets the size of the buffer that has not been used.

 

예제

다음 예제에서는 문자열을 메모리 내 스트림에 쓰고 읽는 방법을 보여 줍니다. 전체 코드 샘플은 데이터 샘플 읽기 및 쓰기를 참조하십시오.



(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

Namespace

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

Metadata

Windows.winmd

참고 항목

데이터 읽기 및 쓰기 샘플
StreamSocket 샘플
DataReader
DataWriterStoreOperation

 

 

표시:
© 2014 Microsoft