DataWriter Class

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

구문


public sealed class DataWriter : IDataWriter,
    IDisposable

특성

[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.

 

예제

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



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;
            }
        }
    }
}

요구 사항

지원되는 최소 클라이언트

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