コード例 #1
0
ファイル: FileStream.cs プロジェクト: ForNeVeR/pnet
        public FileStream(IntPtr handle, FileAccess access,
                          bool ownsHandle, int bufferSize,
                          bool isAsync)
        {
            // Validate the parameters.
            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException
                          ("bufferSize", _("ArgRange_BufferSize"));
            }
            if (access < FileAccess.Read ||
                access > FileAccess.ReadWrite)
            {
                throw new ArgumentOutOfRangeException
                          ("access", _("IO_FileAccess"));
            }
                        #if false
            if (!FileMethods.CheckHandleAccess(handle, access))
            {
                throw new UnauthorizedAccessException
                          (_("IO_IncorrectAccess"));
            }
                        #endif

            // Initialize the object state.
            this.handle             = handle;
            this.access             = access;
            this.ownsHandle         = ownsHandle;
            this.isAsync            = isAsync;
            this.bufferSize         = bufferSize;
            this.buffer             = new byte [bufferSize];
            this.bufferPosn         = 0;
            this.bufferLen          = 0;
            this.bufferOwnedByWrite = false;
            this.canSeek            = FileMethods.CanSeek(handle);
            if (canSeek)
            {
                this.position = FileMethods.Seek
                                    (handle, 0, SeekOrigin.Current);
            }
            else
            {
                this.position = 0;
            }
        }
コード例 #2
0
ファイル: FileStream.cs プロジェクト: ForNeVeR/pnet
        public FileStream(String path, FileMode mode,
                          FileAccess access, FileShare share,
                          int bufferSize, bool useAsync)
        {
            // Validate the parameters.
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (!FileMethods.ValidatePathname(path))
            {
                throw new ArgumentException(_("IO_InvalidPathname"));
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException
                          ("bufferSize", _("ArgRange_BufferSize"));
            }
            if (access < FileAccess.Read ||
                access > FileAccess.ReadWrite)
            {
                throw new ArgumentOutOfRangeException
                          ("access", _("IO_FileAccess"));
            }
            if (mode < FileMode.CreateNew ||
                mode > FileMode.Append)
            {
                throw new ArgumentOutOfRangeException
                          ("mode", _("IO_FileMode"));
            }
                        #if ECMA_COMPAT
            if ((((int)share) & ~0x03) != 0)
                        #else
            if ((((int)share) & ~0x13) != 0)
                        #endif
            {
                throw new ArgumentOutOfRangeException
                          ("share", _("IO_FileShare"));
            }

            // Attempt to open the file.
            if (!FileMethods.Open(path, mode, access, share, out handle))
            {
                Errno errno = FileMethods.GetErrno();
                if (errno == Errno.ENOENT)
                {
                    //
                    // Under UNIX ENOENT is returned if the
                    // directory the file lives in doesn't exist.
                    // ECMA requires DirectoryNotFountException
                    // in that case.
                    //
                    String dirname = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(path));
                    if (!System.IO.Directory.Exists(dirname))
                    {
                        throw new DirectoryNotFoundException();
                    }
                    throw new FileNotFoundException(null, path);
                }
                else if (errno == Errno.ENOTDIR)
                {
                    throw new DirectoryNotFoundException();
                }
                else if (errno == Errno.ENAMETOOLONG)
                {
                    throw new PathTooLongException();
                }
                else if (errno == Errno.EACCES)
                {
                    throw new UnauthorizedAccessException();
                }
                else
                {
                    throw new IOException(errno);
                }
            }

            // Initialize the object state.
            this.access             = access;
            this.ownsHandle         = true;
            this.isAsync            = useAsync;
            this.path               = path;
            this.bufferSize         = bufferSize;
            this.buffer             = new byte [bufferSize];
            this.bufferPosn         = 0;
            this.bufferLen          = 0;
            this.bufferOwnedByWrite = false;
            this.canSeek            = FileMethods.CanSeek(handle);
            this.position           = 0;
        }