public void SkippingDoesNotPreventReading(int bufferSize) { var memStream = new SKMemoryStream(gAbcs); var bufferedStream = new SKFrontBufferedManagedStream(memStream, bufferSize); test_hasLength(bufferedStream, memStream); // Skip half the buffer. bufferedStream.Skip(bufferSize / 2); // Rewind, then read part of the buffer, which should have been read. test_rewind(bufferedStream, true); test_read(bufferedStream, gAbcs, bufferSize / 4); // Now skip beyond the buffered piece, but still within the total buffer. bufferedStream.Skip(bufferSize / 2); // Test that reading will still work. test_read(bufferedStream, gAbcs.Skip(memStream.Position).ToArray(), bufferSize / 4); test_rewind(bufferedStream, true); test_read(bufferedStream, gAbcs, bufferSize); }
public void StreamPeek3() { var gAbcsString = "abcdefghijklmnopqrstuvwxyz"; var gAbcs = StringUtilities.GetEncodedText(gAbcsString, SKTextEncoding.Utf8); for (var bufferSize = 1; bufferSize < gAbcs.Length; bufferSize++) { var memStream = new SKMemoryStream(gAbcs); var bufferedStream = new SKFrontBufferedManagedStream(memStream, bufferSize); var bytesToPeek = bufferSize + 1; var peekStorage = SKData.Create(bytesToPeek); var readStorage = SKData.Create(bytesToPeek); for (var start = 0; start <= bufferSize; start++) { // Skip to the starting point Assert.Equal(start, bufferedStream.Skip(start)); var bytesPeeked = bufferedStream.Peek(peekStorage.Data, bytesToPeek); if (0 == bytesPeeked) { // Peeking should only fail completely if we have read/skipped beyond the buffer. Assert.True(start >= bufferSize); break; } // Only read the amount that was successfully peeked. var bytesRead = bufferedStream.Read(readStorage.Data, bytesPeeked); Assert.Equal(bytesPeeked, bytesRead); Assert.Equal(peekStorage.ToArray().Take(bytesPeeked), readStorage.ToArray().Take(bytesPeeked)); // This should be safe to rewind. Assert.True(bufferedStream.Rewind()); } } }