WinRT中读取基础数据类型

来源:互联网 发布:angular route.min.js 编辑:程序博客网 时间:2024/05/16 10:32

可以用Windows.Storage.Streams.DataWriter 和 Windows.Storage.Streams.DataReader 类

DataWriter类如下:

public sealed class DataWriter : IDataWriter, IDisposable {// Constructs a DataWriter over a growable buffer (see DetachBuffer below)public DataWriter();// Constructs a DataWriter over an output stream and a growable bufferpublic DataWriter(IOutputStream outputStream);// All WriteXxx methods append data to the buffer (growing it if necessary)public void WriteBoolean(Boolean value);public void WriteByte(Byte value);public void WriteBytes(Byte[] value);public void WriteBuffer(IBuffer buffer);public void WriteBuffer(IBuffer buffer, UInt32 start, UInt32 count);public void WriteInt16(Int16 value);public void WriteUInt16(UInt16 value);public void WriteInt32(Int32 value);public void WriteUInt32(UInt32 value);public void WriteInt64(Int64 value);public void WriteUInt64(UInt64 value);public void WriteSingle(Single value);public void WriteDouble(Double value);public void WriteGuid(Guid value);public void WriteDateTime(DateTimeOffset value);public void WriteTimeSpan(TimeSpan value);// For WriteXxx methods, indicates how bytes append to buffer (big/little endian)public ByteOrder ByteOrder { get; set; } // Default=BigEndian// Strings are encoded via UnicodeEncoding (Utf8, Utf16LE, or Utf16BE) instead of ByteOrderpublic UnicodeEncoding UnicodeEncoding { get; set; } // Default=Utf8// Returns how many bytes a string requires when encoded via UnicodeEncodingpublic UInt32 MeasureString(String value);// Appends the encoded string's bytes to the bufferpublic UInt32 WriteString(String value);// Returns the current size of the bufferpublic UInt32 UnstoredBufferLength { get; }// Writes the buffer to the underlying stream & clears the internal bufferpublic DataWriterStoreOperation StoreAsync();// Returns the buffer the DataWriter was using and associates a new empty buffer with itpublic IBuffer DetachBuffer();// Disassociates stream; stream will NOT be closed when Dispose is calledpublic IOutputStream DetachStream();// Closes stream (if not detached); does NOT call StoreAsyncpublic void Dispose();// Calls FlushAsync on underlying streampublic IAsyncOperation<Boolean> FlushAsync();}

注意:只有XxxAsync执行IO操作,WriteXxx并不执行IO,而只是把字节送进内存的Buffer,必须定期调用StoreAsync让buffer写进下层的流,如果不调用StoreAsync就调用Dispose的话,内存中的buffer不会写入下层的流,会被扔掉。因为Dispose不是异步方法,不执行IO操作。

下面是DataWriter的例子:

private async void DataWriterSample(StorageFile file) {using (var dw = new DataWriter(await file.OpenAsync(FileAccessMode.ReadWrite))) {dw.WriteBytes(new Byte[] { 1, 2, 3, 4, 5 });const String text = "Some text";// Store the string length first followed by the string so we can read it back laterUInt32 encodedStringLength = dw.MeasureString(text);dw.WriteUInt32(encodedStringLength);dw.WriteString(text);UInt32 bytesStored = await dw.StoreAsync(); // Commit buffer to stream} // Close DataWriter & underlying stream}

下面是DataReader的例子:

public sealed class DataReader : IDataReader, IDisposable {// Constructs a DataReader over an existing buffer instead of loading a buffer from a streampublic static DataReader FromBuffer(IBuffer buffer);// Constructs a DataReader over an input stream and a growable bufferpublic DataReader(IInputStream inputStream);// Reads count bytes from stream appending them to bufferpublic DataReaderLoadOperation LoadAsync(UInt32 count);// Indicates whether LoadAsync can prefetch more bytes than requested to by 'count'public InputStreamOptions InputStreamOptions { get; set; }// Returns number of bytes in buffer yet to be readpublic UInt32 UnconsumedBufferLength { get; }// All ReadXxx methods read data from buffer (throwing Exception if buffer is empty)public Boolean ReadBoolean();public Byte ReadByte();public void ReadBytes(Byte[] value);public IBuffer ReadBuffer(UInt32 length);public Int16 ReadInt16();public UInt16 ReadUInt16();public Int32 ReadInt32();public UInt32 ReadUInt32();public Int64 ReadInt64();public UInt64 ReadUInt64();public Single ReadSingle();public Double ReadDouble();public Guid ReadGuid();public DateTimeOffset ReadDateTime();public TimeSpan ReadTimeSpan();// For ReadXxx methods, indicates how bytes get read from the buffer (big/little endian)public ByteOrder ByteOrder { get; set; } // Default=BigEndian// Strings are decoded via UnicodeEncoding (Utf8, Utf16LE, or Utf16BE) instead of ByteOrderpublic UnicodeEncoding UnicodeEncoding { get; set; } // Default=Utf8// Decodes codeUnitCount bytes from the buffer to a string via UnicodeEncodingpublic String ReadString(UInt32 codeUnitCount);// Returns the buffer the DataReader was using and associates a new empty buffer with itpublic IBuffer DetachBuffer();// Disassociates stream; stream will NOT be closed when Dispose is calledpublic IInputStream DetachStream();// Closes stream (if not detached)public void Dispose();}



0 0
原创粉丝点击