BGC Tools
Public Member Functions | Protected Member Functions | Properties | Private Member Functions | Private Attributes
BGC.IO.SubStream Class Reference

Wraps a stream and grants access only to a subsection Modified from code found here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/c409b63b-37df-40ca-9322-458ffe06ea48/how-to-access-part-of-a-filestream-or-memorystream?forum=netfxbcl More...

Inheritance diagram for BGC.IO.SubStream:
Inheritance graph
[legend]
Collaboration diagram for BGC.IO.SubStream:
Collaboration graph
[legend]

Public Member Functions

 SubStream (Stream baseStream, long startPosition, long length, bool ownsStream=false)
 
 SubStream (Stream baseStream, long length, bool ownsStream=false)
 
override int ReadByte ()
 
override int Read (byte[] buffer, int offset, int count)
 
override void Flush ()
 
override long Seek (long offset, SeekOrigin origin)
 
override void SetLength (long value)
 
override void Write (byte[] buffer, int offset, int count)
 

Protected Member Functions

override void Dispose (bool disposing)
 

Properties

override long Length [get]
 
override bool CanRead [get]
 
override bool CanWrite [get]
 
override bool CanSeek [get]
 
override long Position [get, set]
 

Private Member Functions

void CheckDisposed ()
 

Private Attributes

readonly bool ownsStream
 
readonly long length
 
readonly long startPosition
 
Stream baseStream = null
 
long position = 0
 
bool _disposed = false
 

Detailed Description

Wraps a stream and grants access only to a subsection Modified from code found here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/c409b63b-37df-40ca-9322-458ffe06ea48/how-to-access-part-of-a-filestream-or-memorystream?forum=netfxbcl

Definition at line 13 of file SubStream.cs.

Constructor & Destructor Documentation

◆ SubStream() [1/2]

BGC.IO.SubStream.SubStream ( Stream  baseStream,
long  startPosition,
long  length,
bool  ownsStream = false 
)
inline

Definition at line 68 of file SubStream.cs.

73  {
74  if (baseStream == null)
75  {
76  throw new ArgumentNullException(nameof(baseStream));
77  }
78 
79  if (!baseStream.CanRead)
80  {
81  throw new ArgumentException("Can't read BaseStream");
82  }
83 
84  if (startPosition < 0 || startPosition + length > baseStream.Length)
85  {
86  throw new ArgumentOutOfRangeException(nameof(startPosition));
87  }
88 
89  this.baseStream = baseStream;
90  this.length = length;
91  this.ownsStream = ownsStream;
93  position = 0;
94 
95  if (baseStream.Position != startPosition)
96  {
97  if (baseStream.CanSeek)
98  {
99  baseStream.Seek(startPosition, SeekOrigin.Begin);
100  }
101  else if (baseStream.Position > startPosition)
102  {
103  //Can't seek and we're past the start position
104  throw new NotSupportedException("BaseStream can't seek backwards to StartPosition");
105  }
106  else
107  {
108  // fast forward manually...
109  const int BUFFER_SIZE = 512;
110  byte[] buffer = new byte[BUFFER_SIZE];
111  long deltaPosition = startPosition - baseStream.Position;
112  while (deltaPosition > 0)
113  {
114  int read = baseStream.Read(
115  buffer: buffer,
116  offset: 0,
117  count: deltaPosition < BUFFER_SIZE ? (int)deltaPosition : BUFFER_SIZE);
118 
119  deltaPosition -= read;
120 
121  if (read == 0)
122  {
123  throw new Exception("Failed to read past required samples");
124  }
125  }
126  }
127  }
128  }
readonly long startPosition
Definition: SubStream.cs:17
Stream baseStream
Definition: SubStream.cs:19
readonly long length
Definition: SubStream.cs:16
readonly bool ownsStream
Definition: SubStream.cs:15

◆ SubStream() [2/2]

BGC.IO.SubStream.SubStream ( Stream  baseStream,
long  length,
bool  ownsStream = false 
)
inline

Definition at line 130 of file SubStream.cs.

134  {
135  if (baseStream == null)
136  {
137  throw new ArgumentNullException("baseStream");
138  }
139 
140  if (!baseStream.CanRead)
141  {
142  throw new ArgumentException("Can't read base stream");
143  }
144 
145  this.baseStream = baseStream;
146  this.length = length;
147  this.ownsStream = ownsStream;
148  startPosition = baseStream.Position;
149  position = 0;
150  }
readonly long startPosition
Definition: SubStream.cs:17
Stream baseStream
Definition: SubStream.cs:19
readonly long length
Definition: SubStream.cs:16
readonly bool ownsStream
Definition: SubStream.cs:15

Member Function Documentation

◆ CheckDisposed()

void BGC.IO.SubStream.CheckDisposed ( )
inlineprivate

Definition at line 225 of file SubStream.cs.

226  {
227  if (_disposed || baseStream == null)
228  {
229  throw new ObjectDisposedException(GetType().Name);
230  }
231  }
Stream baseStream
Definition: SubStream.cs:19

◆ Dispose()

override void BGC.IO.SubStream.Dispose ( bool  disposing)
inlineprotected

Definition at line 236 of file SubStream.cs.

237  {
238  base.Dispose(disposing);
239 
240  if (_disposed)
241  {
242  return;
243  }
244 
245  if (disposing)
246  {
247  if (baseStream != null)
248  {
249  if (ownsStream)
250  {
251  try
252  {
253  baseStream.Dispose();
254  }
255  catch
256  {
257  //Dodge dispose exceptions
258  }
259  }
260  else
261  {
262  //Try advancing stream to end
263  if (baseStream.CanSeek)
264  {
265  baseStream.Seek(startPosition + length, SeekOrigin.Begin);
266  }
267  }
268 
269  baseStream = null;
270  }
271  }
272 
273  _disposed = true;
274  }
readonly long startPosition
Definition: SubStream.cs:17
Stream baseStream
Definition: SubStream.cs:19
readonly long length
Definition: SubStream.cs:16
readonly bool ownsStream
Definition: SubStream.cs:15

◆ Flush()

override void BGC.IO.SubStream.Flush ( )
inline

Definition at line 180 of file SubStream.cs.

181  {
182  CheckDisposed();
183  baseStream.Flush();
184  }
Stream baseStream
Definition: SubStream.cs:19
void CheckDisposed()
Definition: SubStream.cs:225

◆ Read()

override int BGC.IO.SubStream.Read ( byte []  buffer,
int  offset,
int  count 
)
inline

Definition at line 164 of file SubStream.cs.

165  {
166  CheckDisposed();
167 
168  count = (int)Math.Min(count, length - position);
169  if (count <= 0)
170  {
171  return 0;
172  }
173 
174  int read = baseStream.Read(buffer, offset, count);
175  position += read;
176 
177  return read;
178  }
Stream baseStream
Definition: SubStream.cs:19
void CheckDisposed()
Definition: SubStream.cs:225
readonly long length
Definition: SubStream.cs:16

◆ ReadByte()

override int BGC.IO.SubStream.ReadByte ( )
inline

Definition at line 152 of file SubStream.cs.

153  {
154  if (length - position <= 0)
155  {
156  return -1;
157  }
158 
159  position++;
160 
161  return baseStream.ReadByte();
162  }
Stream baseStream
Definition: SubStream.cs:19
readonly long length
Definition: SubStream.cs:16

◆ Seek()

override long BGC.IO.SubStream.Seek ( long  offset,
SeekOrigin  origin 
)
inline

Definition at line 186 of file SubStream.cs.

References BGC.Mathematics.GeneralMath.Clamp().

187  {
188  if (!CanSeek)
189  {
190  throw new NotSupportedException("BaseStream does not support seeking");
191  }
192 
193  switch (origin)
194  {
195  case SeekOrigin.Begin:
196  offset = GeneralMath.Clamp(offset, 0, length);
197  position = offset;
198  offset += startPosition;
199  break;
200 
201  case SeekOrigin.Current:
202  offset = GeneralMath.Clamp(offset, -position, length - position);
203  position += offset;
204  break;
205 
206  case SeekOrigin.End:
207  offset = GeneralMath.Clamp(offset, -length, 0);
208  position = length + offset;
209  offset -= baseStream.Length - (startPosition + Length);
210  break;
211 
212  default:
213  throw new NotSupportedException($"SeekOrigin not implemented: {origin}");
214  }
215 
216  baseStream.Seek(offset, origin);
217 
218  return position;
219  }
readonly long startPosition
Definition: SubStream.cs:17
Stream baseStream
Definition: SubStream.cs:19
readonly long length
Definition: SubStream.cs:16
override long Length
Definition: SubStream.cs:23
static decimal Clamp(decimal value, decimal min, decimal max)
override bool CanSeek
Definition: SubStream.cs:50
Here is the call graph for this function:

◆ SetLength()

override void BGC.IO.SubStream.SetLength ( long  value)

◆ Write()

override void BGC.IO.SubStream.Write ( byte []  buffer,
int  offset,
int  count 
)

Field Documentation

◆ _disposed

bool BGC.IO.SubStream._disposed = false
private

Definition at line 235 of file SubStream.cs.

◆ baseStream

Stream BGC.IO.SubStream.baseStream = null
private

Definition at line 19 of file SubStream.cs.

◆ length

readonly long BGC.IO.SubStream.length
private

Definition at line 16 of file SubStream.cs.

◆ ownsStream

readonly bool BGC.IO.SubStream.ownsStream
private

Definition at line 15 of file SubStream.cs.

◆ position

long BGC.IO.SubStream.position = 0
private

Definition at line 20 of file SubStream.cs.

◆ startPosition

readonly long BGC.IO.SubStream.startPosition
private

Definition at line 17 of file SubStream.cs.

Property Documentation

◆ CanRead

override bool BGC.IO.SubStream.CanRead
get

Definition at line 32 of file SubStream.cs.

◆ CanSeek

override bool BGC.IO.SubStream.CanSeek
get

Definition at line 50 of file SubStream.cs.

◆ CanWrite

override bool BGC.IO.SubStream.CanWrite
get

Definition at line 41 of file SubStream.cs.

◆ Length

override long BGC.IO.SubStream.Length
get

Definition at line 23 of file SubStream.cs.

◆ Position

override long BGC.IO.SubStream.Position
getset

Definition at line 59 of file SubStream.cs.


The documentation for this class was generated from the following file: