예제 #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public int read(char cbuf[] , int offset, int length) throws IOException
            public override int Read(char[] cbuf, int offset, int length)
            {
                int off = offset;
                int end = offset + length;

                if (offset < 0 || offset > cbuf.Length || length < 0 || end < 0 || end > cbuf.Length)
                {
                    throw new IndexOutOfBoundsException();
                }
                lock (outerInstance.ReadLock)
                {
                    bool eof = false;
                    char c   = (char)0;
                    for (;;)
                    {
                        if (NextChar >= NChars)                         //fill
                        {
                            int n = 0;
                            do
                            {
                                n = @in.Read(Cb, 0, Cb.Length);
                            } while (n == 0);
                            if (n > 0)
                            {
                                NChars   = n;
                                NextChar = 0;
                                if (n < Cb.Length && Cb[n - 1] != '\n' && Cb[n - 1] != '\r')
                                {
                                    /*
                                     * we're in canonical mode so each "fill" should
                                     * come back with an eol. if there no lf or nl at
                                     * the end of returned bytes we reached an eof.
                                     */
                                    eof = true;
                                }
                            }                             //EOF
                            else
                            {
                                if (off - offset == 0)
                                {
                                    return(-1);
                                }
                                return(off - offset);
                            }
                        }
                        if (LeftoverLF && cbuf == outerInstance.Rcb && Cb[NextChar] == '\n')
                        {
                            /*
                             * if invoked by our readline, skip the leftover, otherwise
                             * return the LF.
                             */
                            NextChar++;
                        }
                        LeftoverLF = false;
                        while (NextChar < NChars)
                        {
                            c = cbuf[off++] = Cb[NextChar];
//JAVA TO C# CONVERTER TODO TASK: The following line could not be converted:
                            cb[nextChar++] = 0;
                            if (c == '\n')
                            {
                                return(off - offset);
                            }
                            else if (c == '\r')
                            {
                                if (off == end)
                                {
                                    /* no space left even the next is LF, so return
                                     * whatever we have if the invoker is not our
                                     * readLine()
                                     */
                                    if (cbuf == outerInstance.Rcb)
                                    {
                                        cbuf = outerInstance.Grow();
                                        end  = cbuf.Length;
                                    }
                                    else
                                    {
                                        LeftoverLF = true;
                                        return(off - offset);
                                    }
                                }
                                if (NextChar == NChars && @in.Ready())
                                {
                                    /*
                                     * we have a CR and we reached the end of
                                     * the read in buffer, fill to make sure we
                                     * don't miss a LF, if there is one, it's possible
                                     * that it got cut off during last round reading
                                     * simply because the read in buffer was full.
                                     */
                                    NChars   = @in.Read(Cb, 0, Cb.Length);
                                    NextChar = 0;
                                }
                                if (NextChar < NChars && Cb[NextChar] == '\n')
                                {
                                    cbuf[off++] = '\n';
                                    NextChar++;
                                }
                                return(off - offset);
                            }
                            else if (off == end)
                            {
                                if (cbuf == outerInstance.Rcb)
                                {
                                    cbuf = outerInstance.Grow();
                                    end  = cbuf.Length;
                                }
                                else
                                {
                                    return(off - offset);
                                }
                            }
                        }
                        if (eof)
                        {
                            return(off - offset);
                        }
                    }
                }
            }