IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
SkipStream.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2008-2015 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.IO;
26
28{
29
33 sealed class SkipStream : Stream
34 {
35
36 readonly Stream stream;
37
38 long skipOffset;
39 long skipLength;
40
48 internal SkipStream(Stream stream, long skipOffset, long skipLength)
49 {
50 if (skipOffset < 0)
51 throw new ArgumentOutOfRangeException(nameof(skipOffset));
52 if (skipLength < 0)
53 throw new ArgumentOutOfRangeException(nameof(skipLength));
54
55 this.stream = stream;
56 this.skipOffset = skipOffset;
57 this.skipLength = skipLength;
58 }
59
60 public override bool CanRead => stream.CanRead;
61
62 public override bool CanSeek => false;
63
64 public override bool CanWrite => false;
65
66 public override int Read(byte[] buffer, int offset, int count)
67 {
68 if (skipLength != 0 && skipOffset < count)
69 {
70 if (skipOffset != 0)
71 {
72 count = (int)skipOffset;
73 }
74 else
75 {
76 // note that we loop forever if the skipped part lies beyond EOF
77 while (skipLength != 0)
78 {
79 // use the output buffer as scratch space
80 skipLength -= stream.Read(buffer, offset, (int)Math.Min(count, skipLength));
81 }
82 }
83 }
84
85 var totalBytesRead = stream.Read(buffer, offset, count);
86 skipOffset -= totalBytesRead;
87 return totalBytesRead;
88 }
89
90 public override long Length => throw new NotSupportedException();
91
92 public override long Position
93 {
94 get => throw new NotSupportedException();
95 set => throw new NotSupportedException();
96 }
97
98 public override void Flush() => throw new NotSupportedException();
99
100 public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
101
102 public override void SetLength(long value) => throw new NotSupportedException();
103
104 public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
105
106 protected override void Dispose(bool disposing)
107 {
108 if (disposing)
109 stream.Dispose();
110 }
111
112 }
113
114}
Read-only stream that presents a windowed view of another stream.
Definition SkipStream.cs:34
override long Seek(long offset, SeekOrigin origin)
override void Dispose(bool disposing)
override void SetLength(long value)
override int Read(byte[] buffer, int offset, int count)
Definition SkipStream.cs:66
override void Write(byte[] buffer, int offset, int count)