Ejemplo n.º 1
0
        /*
         * Receives a char and stores it into the PipedReader. This called by
         * PipedWriter.write() when writes occur.
         * <p/>
         * If the buffer is full and the thread sending #receive is interrupted, the
         * InterruptedIOException will be thrown.
         *
         * @param oneChar
         *            the char to store into the pipe.
         *
         * @throws IOException
         *             If the stream is already closed or another IOException
         *             occurs.
         */
        public void receive(char oneChar)
        {//throws IOException {// public modifier, because called from PipedWriter
            lock (lockJ)
            {
                if (data == null)
                {
                    throw new IOException("Pipe is closed"); //$NON-NLS-1$
                }
                if (lastReader != null && !lastReader.isAlive())
                {
                    throw new IOException("Pipe broken"); //$NON-NLS-1$
                }

                /*
                 * Set the last thread to be writing on this PipedWriter. If
                 * lastWriter dies while someone is waiting to read an IOException
                 * of "Pipe broken" will be thrown in read()
                 */
                lastWriter = java.lang.Thread.currentThread();
                try
                {
                    while (data != null && outJ == inJ)
                    {
                        lockJ.notifyAll();
                        this.wait(1000);
                        if (lastReader != null && !lastReader.isAlive())
                        {
                            throw new IOException("Pipe broken"); //$NON-NLS-1$
                        }
                    }
                }
                catch (java.lang.InterruptedException e)
                {
                    throw new InterruptedIOException();
                }
                catch (System.Threading.ThreadInterruptedException e)
                {
                    throw new InterruptedIOException();
                }
                if (data != null)
                {
                    if (inJ == -1)
                    {
                        inJ = 0;
                    }
                    data[inJ++] = oneChar;
                    if (inJ == data.Length)
                    {
                        inJ = 0;
                    }
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        /*
         * Receives a byte and stores it in this stream's {@code buffer}. This
         * method is called by {@link PipedOutputStream#write(int)}. The least
         * significant byte of the integer {@code oneByte} is stored at index
         * {@code in} in the {@code buffer}.
         * <p/>
         * This method blocks as long as {@code buffer} is full.
         *
         * @param oneByte
         *            the byte to store in this pipe.
         * @throws InterruptedIOException
         *             if the {@code buffer} is full and the thread that has called
         *             this method is interrupted.
         * @throws IOException
         *             if this stream is closed or the thread that has last read
         *             from this stream is no longer alive.
         */
        protected internal void receive(int oneByte)
        {//throws IOException {
            lock (this) { }
            if (buffer == null || isClosed)
            {
                throw new IOException("Pipe is closed"); //$NON-NLS-1$
            }
            if (lastReader != null && !lastReader.isAlive())
            {
                throw new IOException("Pipe broken"); //$NON-NLS-1$
            }

            /*
             * Set the last thread to be writing on this PipedInputStream. If
             * lastWriter dies while someone is waiting to read an IOException of
             * "Pipe broken" will be thrown in read()
             */
            lastWriter = java.lang.Thread.currentThread();
            try
            {
                while (buffer != null && outJ == inJ)
                {
                    this.notifyAll();
                    this.wait(1000);
                    if (lastReader != null && !lastReader.isAlive())
                    {
                        throw new IOException("Pipe broken"); //$NON-NLS-1$
                    }
                }
            }
            catch (java.lang.InterruptedException e)
            {
                throw new InterruptedIOException();
            }
            if (buffer != null)
            {
                if (inJ == -1)
                {
                    inJ = 0;
                }
                buffer[inJ++] = (byte)oneByte;
                if (inJ == buffer.Length)
                {
                    inJ = 0;
                }
                return;
            }
        }
Ejemplo n.º 3
0
        /*
         * Reads at most {@code count} characters from this reader and stores them
         * in the character array {@code buffer} starting at {@code offset}. If
         * there is no data in the pipe, this method blocks until at least one byte
         * has been read, the end of the reader is detected or an exception is
         * thrown.
         * <p>
         * Separate threads should be used to read from a {@code PipedReader} and to
         * write to the connected {@link PipedWriter}. If the same thread is used, a
         * deadlock may occur.
         *
         * @param buffer
         *            the character array in which to store the characters read.
         * @param offset
         *            the initial position in {@code bytes} to store the characters
         *            read from this reader.
         * @param count
         *            the maximum number of characters to store in {@code buffer}.
         * @return the number of characters read or -1 if the end of the reader has
         *         been reached.
         * @throws IndexOutOfBoundsException
         *             if {@code offset < 0} or {@code count < 0}, or if {@code
         *             offset + count} is greater than the size of {@code buffer}.
         * @throws InterruptedIOException
         *             if the thread reading from this reader is interrupted.
         * @throws IOException
         *             if this reader is closed or not connected to a writer, or if
         *             the thread writing to the connected writer is no longer
         *             alive.
         */

        public override int read(char[] buffer, int offset, int count)
        {// throws IOException {
            lock (lockJ)
            {
                if (!isConnected)
                {
                    throw new IOException("Pipe Not Connected"); //$NON-NLS-1$
                }
                if (data == null)
                {
                    throw new IOException("Pipe is closed"); //$NON-NLS-1$
                }
                // avoid int overflow
                if (offset < 0 || count > buffer.Length - offset || count < 0)
                {
                    throw new java.lang.IndexOutOfBoundsException();
                }
                if (count == 0)
                {
                    return(0);
                }

                /*
                 * Set the last thread to be reading on this PipedReader. If
                 * lastReader dies while someone is waiting to write an IOException
                 * of "Pipe broken" will be thrown in receive()
                 */
                lastReader = java.lang.Thread.currentThread();
                try
                {
                    bool first = true;
                    while (inJ == -1)
                    {
                        // Are we at end of stream?
                        if (isClosed)
                        {
                            return(-1);
                        }
                        if (!first && lastWriter != null && !lastWriter.isAlive())
                        {
                            throw new IOException("Pipe broken"); //$NON-NLS-1$
                        }
                        first = false;
                        // Notify callers of receive()
                        lockJ.notifyAll();
                        lockJ.wait(1000);
                    }
                }
                catch (java.lang.InterruptedException e)
                {
                    throw new InterruptedIOException();
                }
                catch (System.Threading.ThreadInterruptedException e)
                {
                    throw new InterruptedIOException();
                }

                int copyLength = 0;
                /* Copy chars from out to end of buffer first */
                if (outJ >= inJ)
                {
                    copyLength = count > data.Length - outJ ? data.Length - outJ
                            : count;
                    java.lang.SystemJ.arraycopy(data, outJ, buffer, offset, copyLength);
                    outJ += copyLength;
                    if (outJ == data.Length)
                    {
                        outJ = 0;
                    }
                    if (outJ == inJ)
                    {
                        // empty buffer
                        inJ  = -1;
                        outJ = 0;
                    }
                }

                /*
                 * Did the read fully succeed in the previous copy or is the buffer
                 * empty?
                 */
                if (copyLength == count || inJ == -1)
                {
                    return(copyLength);
                }

                int charsCopied = copyLength;
                /* Copy bytes from 0 to the number of available bytes */
                copyLength = inJ - outJ > count - copyLength ? count - copyLength
                        : inJ - outJ;
                java.lang.SystemJ.arraycopy(data, outJ, buffer, offset + charsCopied,
                                            copyLength);
                outJ += copyLength;
                if (outJ == inJ)
                {
                    // empty buffer
                    inJ  = -1;
                    outJ = 0;
                }
                return(charsCopied + copyLength);
            }
        }