Hi all.

How do I cutt, say, first 25 bytes from the stream?
Any help would be greatly appreciated!!

Re: Cutting off first several bytes from the stream by Jon

Jon
Thu Sep 15 14:15:57 CDT 2005

Kikoz <Kikoz@discussions.microsoft.com> wrote:
> How do I cutt, say, first 25 bytes from the stream?
> Any help would be greatly appreciated!!

If it's a seekable stream, just do:

stream.Position += 25;
or
stream.Seek (25, SeekOrigin.Current);

If it's not seekable, just read 25 bytes:

byte[] buffer = new byte[25];
int left = 25;
int read;
while (left > 0 && (read = stream.Read(buffer, 0, 25)) > 0)
{
left -= read;
}
if (read <= 0)
{
// You didn't manage to skip 25 bytes - do whatever you should
// here.
}

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Re: Cutting off first several bytes from the stream by Vlad

Vlad
Thu Sep 15 18:46:03 CDT 2005

Thanks!!

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1d93d8c4f952b4f98c725@msnews.microsoft.com...
> Kikoz <Kikoz@discussions.microsoft.com> wrote:
>> How do I cutt, say, first 25 bytes from the stream?
>> Any help would be greatly appreciated!!
>
> If it's a seekable stream, just do:
>
> stream.Position += 25;
> or
> stream.Seek (25, SeekOrigin.Current);
>
> If it's not seekable, just read 25 bytes:
>
> byte[] buffer = new byte[25];
> int left = 25;
> int read;
> while (left > 0 && (read = stream.Read(buffer, 0, 25)) > 0)
> {
> left -= read;
> }
> if (read <= 0)
> {
> // You didn't manage to skip 25 bytes - do whatever you should
> // here.
> }
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too