0

I load stream into buffer, and consume that with DataReader.

private async Task InitializeDataReader()
{
    IBuffer buffer = await FileIO.ReadBufferAsync(_file).AsTask();
    _reader = DataReader.FromBuffer(buffer);
    _reader.UnicodeEncoding = _encoding;
}

But sometimes I need to seek 1 byte back from current position. this was actually possible for many readers,

_reader.BaseStream.Seek(-1, SeekOrigin.Current);

But it doesn't exist for DataReader. What is the alternative approach in UWP?

If I shouldn't use DataReader what is the alternative class in Windows.Storage.Streams

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118

2 Answers2

0

You can use the stream and set the stream seek.

Buffer 2 stream:Stream stream = buffer.AsStream();

Stream 2 IInputStream:IInputStream input = stream.AsInputStream();

DataReader d=new DataReader(input);

You can use stream.Seek to set seek.

lindexi
  • 4,182
  • 3
  • 19
  • 65
0

For UWP app, DataReader clas don't provide such method, as lindexi suggested, the possible way here is using IRandomAccessStream interface's Seek(UInt64) method

The Seek method supports random access to files. Seek allows the read/write position to be moved to any position within the file. This is done with byte offset reference point parameters.

For example:

// Create the input stream at position 0 so that the stream can be read 
// from the beginning.
using (var inputStream = stream.GetInputStreamAt(0))
{
        inputStream.Seek(1);//Seeking
        using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
        {
Community
  • 1
  • 1
Franklin Chen - MSFT
  • 4,845
  • 2
  • 17
  • 28