Exemplo n.º 1
0
        public override int ReadByte()
        {
            if (!CanRead)
            {
                throw new NotSupportedException("The stream does not support reading");
            }

            byte[] buffer = new byte[1];
            ulong  bytesRead;
            Result result;

            if (async)
            {
                Async.Read(handle, out buffer[0], 1, readCallback);
                Wait();
                result = asyncResult;
            }
            else
            {
                result = Sync.Read(handle, out buffer[0], 1UL, out bytesRead);
            }
            if (result == Result.ErrorEof)
            {
                return(-1);
            }

            Vfs.ThrowException(Uri, result);
            return(buffer[0]);
        }
Exemplo n.º 2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            else if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "Must be >= 0");
            }
            else if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "Must be >= 0");
            }
            else if (count > buffer.Length - offset)
            {
                throw new ArgumentException("Buffer too small, count/offset wrong");
            }
            else if (!CanRead)
            {
                throw new NotSupportedException("The stream does not support reading");
            }

            ulong  bytesRead;
            Result result;

            if (async)
            {
                Async.Read(handle, out buffer[offset], (uint)count, readCallback);
                Wait();
                result    = asyncResult;
                bytesRead = asyncBytesRead;
            }
            else
            {
                result = Sync.Read(handle, out buffer[offset], (ulong)count, out bytesRead);
            }
            if (result == Result.ErrorEof)
            {
                return(0);
            }

            Vfs.ThrowException(Uri, result);
            return((int)bytesRead);
        }
Exemplo n.º 3
0
        private void AsyncRead(Handle handle, Result result, byte[] buf,
                               ulong bytesRequested, ulong bytesRead)
        {
            if (result == Result.Ok)
            {
                Array.Copy(buf, 0, buffer, offset + count - bytesRemaining, (int)bytesRead);
                bytesRemaining -= (int)bytesRead;
                if (bytesRemaining > 0)
                {
                    buf = new byte[bytesRemaining];
                    Async.Read(handle, out buf[0], (uint)bytesRemaining,
                               new AsyncReadCallback(AsyncRead));
                }
                else if (cback != null)
                {
                    asyncResult.SetComplete(null, count);
                    cback(asyncResult);
                }
            }
            else if (result == Result.ErrorEof)
            {
                Array.Copy(buf, 0, buffer, offset + count - bytesRemaining, (int)bytesRead);
                bytesRemaining -= (int)bytesRead;
                asyncResult.SetComplete(null, count - bytesRemaining);

                if (cback != null)
                {
                    cback(asyncResult);
                }
            }
            else if (cback != null)
            {
                Exception e = new IOException(Vfs.ResultToString(result));
                asyncResult.SetComplete(e, -1);
                cback(asyncResult);
            }
        }
Exemplo n.º 4
0
 public VfsStreamAsyncResult BeginRead()
 {
     asyncResult = new VfsStreamAsyncResult(state);
     Async.Read(handle, out buffer[offset], (uint)count, new AsyncReadCallback(AsyncRead));
     return(asyncResult);
 }