Example #1
0
 /**
  * Creates a new named {@code Timer} which may be specified to be run as a
  * daemon thread.
  *
  * @param name the name of the {@code Timer}.
  * @param isDaemon true if {@code Timer}'s thread should be a daemon thread.
  * @throws NullPointerException is {@code name} is {@code null}
  */
 public Timer(String name, bool isDaemon) : base()
 {
     if (name == null)
     {
         throw new java.lang.NullPointerException("name is null");
     }
     this.impl      = new TimerImpl(name, isDaemon);
     this.finalizer = new FinalizerHelper(impl);
     java.lang.Thread t = new java.lang.Thread(new IAC_CallTimerCancelMethodRunnable(this.impl));
     java.lang.Runtime.getRuntime().addShutdownHook(t);
 }
Example #2
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;
                }
            }
        }
Example #3
0
        public void start()
        {
            java.net.ServerSocket server = new java.net.ServerSocket(this.port);
            // Create a receiver main loop...
            this.running = true;
            while (this.running)
            {
                // waiting for connect...
                java.net.Socket client = server.accept();

                // create new thread for work
                java.lang.Thread worker = new java.lang.Thread(new SampleSimpleHttpServerWorker(client));
                worker.start();
            }
        }
Example #4
0
        public void start()
        {
            java.net.ServerSocket server = new java.net.ServerSocket(this.port);
            // Create a receiver main loop...
            this.running = true;
            while (this.running)
            {
                // waiting for connect...
                java.net.Socket client = server.accept();

                // create new thread for work
                java.lang.Thread worker = new java.lang.Thread(new SampleSimpleHttpServerWorker(client));
                worker.start();
            }
        }
Example #5
0
 /// <summary>
 /// Return the current thread
 /// </summary>
 /// <returns></returns>
 public static java.lang.Thread currentThread()
 {
     java.lang.Thread result = new java.lang.Thread();
     result.delegateInstance = System.Threading.Thread.CurrentThread;
     return(result);
 }
Example #6
0
        /*
         * Reads at most {@code count} bytes from this stream and stores them in the
         * byte array {@code bytes} starting at {@code offset}. Blocks until at
         * least one byte has been read, the end of the stream is detected or an
         * exception is thrown.
         * <p/>
         * Separate threads should be used to read from a {@code PipedInputStream}
         * and to write to the connected {@link PipedOutputStream}. If the same
         * thread is used, a deadlock may occur.
         *
         * @param bytes
         *            the array in which to store the bytes read.
         * @param offset
         *            the initial position in {@code bytes} to store the bytes
         *            read from this stream.
         * @param count
         *            the maximum number of bytes to store in {@code bytes}.
         * @return the number of bytes actually read or -1 if the end of the stream
         *         has been reached.
         * @throws IndexOutOfBoundsException
         *             if {@code offset &lt; 0} or {@code count &lt; 0}, or if {@code
         *             offset + count} is greater than the size of {@code bytes}.
         * @throws InterruptedIOException
         *             if the thread reading from this stream is interrupted.
         * @throws IOException
         *             if this stream is closed or not connected to an output
         *             stream, or if the thread writing to the connected output
         *             stream is no longer alive.
         * @throws NullPointerException
         *             if {@code bytes} is {@code null}.
         */
        public override int read(byte[] bytes, int offset, int count)
        {// throws IOException {
            lock (this)
            {
                if (bytes == null)
                {
                    throw new java.lang.NullPointerException();
                }

                if (offset < 0 || offset > bytes.Length || count < 0 ||
                    count > bytes.Length - offset)
                {
                    throw new java.lang.IndexOutOfBoundsException();
                }

                if (count == 0)
                {
                    return(0);
                }

                if (!isConnected)
                {
                    // luni.CB=Not connected
                    throw new IOException("Not connected"); //$NON-NLS-1$
                }

                if (buffer == null)
                {
                    // luni.CC=InputStream is closed
                    throw new IOException("InputStream is closed"); //$NON-NLS-1$
                }

                if (lastWriter != null && !lastWriter.isAlive() && (inJ < 0))
                {
                    // luni.CD=Write end dead
                    throw new IOException("Write end dead"); //$NON-NLS-1$
                }

                /*
                 * Set the last thread to be reading on this PipedInputStream. 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
                {
                    int attempts = 3;
                    while (inJ == -1)
                    {
                        // Are we at end of stream?
                        if (isClosed)
                        {
                            return(-1);
                        }
                        if ((attempts-- <= 0) && lastWriter != null && !lastWriter.isAlive())
                        {
                            // luni.CE=Pipe broken
                            throw new IOException("Pipe broken"); //$NON-NLS-1$
                        }
                        // Notify callers of receive()
                        this.notifyAll();
                        this.wait(1000);
                    }
                }
                catch (java.lang.InterruptedException e)
                {
                    throw new InterruptedIOException();
                }

                int copyLength = 0;
                /* Copy bytes from out to end of buffer first */
                if (outJ >= inJ)
                {
                    copyLength = count > (buffer.Length - outJ) ? buffer.Length - outJ
                            : count;
                    java.lang.SystemJ.arraycopy(buffer, outJ, bytes, offset, copyLength);
                    outJ += copyLength;
                    if (outJ == buffer.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 bytesCopied = copyLength;
                /* Copy bytes from 0 to the number of available bytes */
                copyLength = inJ - outJ > (count - bytesCopied) ? count - bytesCopied
                        : inJ - outJ;
                java.lang.SystemJ.arraycopy(buffer, outJ, bytes, offset + bytesCopied, copyLength);
                outJ += copyLength;
                if (outJ == inJ)
                {
                    // empty buffer
                    inJ  = -1;
                    outJ = 0;
                }
                return(bytesCopied + copyLength);
            }
        }
Example #7
0
        /*
         * Reads a single byte from this stream and returns it as an integer in the
         * range from 0 to 255. Returns -1 if the end of this stream has been
         * reached. If there is no data in the pipe, this method blocks until data
         * is available, the end of the stream is detected or an exception is
         * thrown.
         * <p/>
         * Separate threads should be used to read from a {@code PipedInputStream}
         * and to write to the connected {@link PipedOutputStream}. If the same
         * thread is used, a deadlock may occur.
         *
         * @return the byte read or -1 if the end of the source stream has been
         *         reached.
         * @throws IOException
         *             if this stream is closed or not connected to an output
         *             stream, or if the thread writing to the connected output
         *             stream is no longer alive.
         */

        public override int read()
        {//throws IOException {
            lock (this)
            {
                if (!isConnected)
                {
                    // luni.CB=Not connected
                    throw new IOException("Not connected"); //$NON-NLS-1$
                }
                if (buffer == null)
                {
                    // luni.CC=InputStream is closed
                    throw new IOException("InputStream is closed"); //$NON-NLS-1$
                }

                if (lastWriter != null && !lastWriter.isAlive() && (inJ < 0))
                {
                    // luni.CD=Write end dead
                    throw new IOException("Write end dead"); //$NON-NLS-1$
                }

                /*
                 * Set the last thread to be reading on this PipedInputStream. 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
                {
                    int attempts = 3;
                    while (inJ == -1)
                    {
                        // Are we at end of stream?
                        if (isClosed)
                        {
                            return(-1);
                        }
                        if ((attempts-- <= 0) && lastWriter != null && !lastWriter.isAlive())
                        {
                            // luni.CE=Pipe broken
                            throw new IOException("Pipe broken"); //$NON-NLS-1$
                        }
                        // Notify callers of receive()
                        this.notifyAll();
                        this.wait(1000);
                    }
                }
                catch (java.lang.InterruptedException e)
                {
                    throw new InterruptedIOException();
                }

                byte result = buffer[outJ++];
                if (outJ == buffer.Length)
                {
                    outJ = 0;
                }
                if (outJ == inJ)
                {
                    // empty buffer
                    inJ  = -1;
                    outJ = 0;
                }
                return(result & 0xff);
            }
        }
Example #8
0
 /**
  * Receives a char array 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 chars
  *            the char array to store into the pipe.
  * @param offset
  *            offset to start reading from
  * @param count
  *            total characters to read
  *
  * @throws IOException
  *             If the stream is already closed or another IOException
  *             occurs.
  */
 public void receive(char[] chars, int offset, int count)
 {
     //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();
     while (count > 0) {
         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) {
             break;
         }
         if (inJ == -1) {
             inJ = 0;
         }
         if (inJ >= outJ) {
             int length = data.Length - inJ;
             if (count < length) {
                 length = count;
             }
             java.lang.SystemJ.arraycopy(chars, offset, data, inJ, length);
             offset += length;
             count -= length;
             inJ += length;
             if (inJ == data.Length) {
                 inJ = 0;
             }
         }
         if (count > 0 && inJ != outJ) {
             int length = outJ - inJ;
             if (count < length) {
                 length = count;
             }
             java.lang.SystemJ.arraycopy(chars, offset, data, inJ, length);
             offset += length;
             count -= length;
             inJ += length;
         }
     }
     if (count == 0) {
         return;
     }
     }
 }
Example #9
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;
     }
     }
 }
Example #10
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;
            }
        }
Example #11
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);
            }
        }
Example #12
0
 /// <summary>
 /// Return the current thread
 /// </summary>
 /// <returns></returns>
 public static java.lang.Thread currentThread()
 {
     java.lang.Thread result = new java.lang.Thread();
     result.delegateInstance = System.Threading.Thread.CurrentThread;
     return result;
 }
Example #13
0
        /*
         * Receives a char array 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 chars
         *            the char array to store into the pipe.
         * @param offset
         *            offset to start reading from
         * @param count
         *            total characters to read
         *
         * @throws IOException
         *             If the stream is already closed or another IOException
         *             occurs.
         */
        public void receive(char[] chars, int offset, int count)
        {//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();
                while (count > 0)
                {
                    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)
                    {
                        break;
                    }
                    if (inJ == -1)
                    {
                        inJ = 0;
                    }
                    if (inJ >= outJ)
                    {
                        int length = data.Length - inJ;
                        if (count < length)
                        {
                            length = count;
                        }
                        java.lang.SystemJ.arraycopy(chars, offset, data, inJ, length);
                        offset += length;
                        count  -= length;
                        inJ    += length;
                        if (inJ == data.Length)
                        {
                            inJ = 0;
                        }
                    }
                    if (count > 0 && inJ != outJ)
                    {
                        int length = outJ - inJ;
                        if (count < length)
                        {
                            length = count;
                        }
                        java.lang.SystemJ.arraycopy(chars, offset, data, inJ, length);
                        offset += length;
                        count  -= length;
                        inJ    += length;
                    }
                }
                if (count == 0)
                {
                    return;
                }
            }
        }
Example #14
0
        /**
         * Reads a single byte from this stream and returns it as an integer in the
         * range from 0 to 255. Returns -1 if the end of this stream has been
         * reached. If there is no data in the pipe, this method blocks until data
         * is available, the end of the stream is detected or an exception is
         * thrown.
         * <p/>
         * Separate threads should be used to read from a {@code PipedInputStream}
         * and to write to the connected {@link PipedOutputStream}. If the same
         * thread is used, a deadlock may occur.
         *
         * @return the byte read or -1 if the end of the source stream has been
         *         reached.
         * @throws IOException
         *             if this stream is closed or not connected to an output
         *             stream, or if the thread writing to the connected output
         *             stream is no longer alive.
         */
        public override int read()
        {
            //throws IOException {
            lock (this)
            {
                if (!isConnected)
                {
                    // luni.CB=Not connected
                    throw new IOException("Not connected"); //$NON-NLS-1$
                }
                if (buffer == null)
                {
                    // luni.CC=InputStream is closed
                    throw new IOException("InputStream is closed"); //$NON-NLS-1$
                }

                if (lastWriter != null && !lastWriter.isAlive() && (inJ < 0))
                {
                    // luni.CD=Write end dead
                    throw new IOException("Write end dead"); //$NON-NLS-1$
                }
                /*
                 * Set the last thread to be reading on this PipedInputStream. 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
                {
                    int attempts = 3;
                    while (inJ == -1)
                    {
                        // Are we at end of stream?
                        if (isClosed)
                        {
                            return -1;
                        }
                        if ((attempts-- <= 0) && lastWriter != null && !lastWriter.isAlive())
                        {
                            // luni.CE=Pipe broken
                            throw new IOException("Pipe broken"); //$NON-NLS-1$
                        }
                        // Notify callers of receive()
                        this.notifyAll();
                        this.wait(1000);
                    }
                }
                catch (java.lang.InterruptedException e)
                {
                    throw new InterruptedIOException();
                }

                byte result = buffer[outJ++];
                if (outJ == buffer.Length)
                {
                    outJ = 0;
                }
                if (outJ == inJ)
                {
                    // empty buffer
                    inJ = -1;
                    outJ = 0;
                }
                return result & 0xff;
            }
        }
Example #15
0
 /**
     * Creates a new named {@code Timer} which may be specified to be run as a
     * daemon thread.
     *
     * @param name the name of the {@code Timer}.
     * @param isDaemon true if {@code Timer}'s thread should be a daemon thread.
     * @throws NullPointerException is {@code name} is {@code null}
     */
 public Timer(String name, bool isDaemon)
     : base()
 {
     if (name == null){
         throw new java.lang.NullPointerException("name is null");
     }
     this.impl = new TimerImpl(name, isDaemon);
     this.finalizer = new FinalizerHelper(impl);
     java.lang.Thread t = new java.lang.Thread (new IAC_CallTimerCancelMethodRunnable(this.impl));
     java.lang.Runtime.getRuntime().addShutdownHook(t);
 }
Example #16
0
        /**
         * Reads at most {@code count} bytes from this stream and stores them in the
         * byte array {@code bytes} starting at {@code offset}. Blocks until at
         * least one byte has been read, the end of the stream is detected or an
         * exception is thrown.
         * <p/>
         * Separate threads should be used to read from a {@code PipedInputStream}
         * and to write to the connected {@link PipedOutputStream}. If the same
         * thread is used, a deadlock may occur.
         *
         * @param bytes
         *            the array in which to store the bytes read.
         * @param offset
         *            the initial position in {@code bytes} to store the bytes
         *            read from this stream.
         * @param count
         *            the maximum number of bytes to store in {@code bytes}.
         * @return the number of bytes actually read or -1 if the end of the stream
         *         has been reached.
         * @throws IndexOutOfBoundsException
         *             if {@code offset &lt; 0} or {@code count &lt; 0}, or if {@code
         *             offset + count} is greater than the size of {@code bytes}.
         * @throws InterruptedIOException
         *             if the thread reading from this stream is interrupted.
         * @throws IOException
         *             if this stream is closed or not connected to an output
         *             stream, or if the thread writing to the connected output
         *             stream is no longer alive.
         * @throws NullPointerException
         *             if {@code bytes} is {@code null}.
         */
        public override int read(byte[] bytes, int offset, int count)
        {
            // throws IOException {
            lock (this)
            {
                if (bytes == null)
                {
                    throw new java.lang.NullPointerException();
                }

                if (offset < 0 || offset > bytes.Length || count < 0
                        || count > bytes.Length - offset)
                {
                    throw new java.lang.IndexOutOfBoundsException();
                }

                if (count == 0)
                {
                    return 0;
                }

                if (!isConnected)
                {
                    // luni.CB=Not connected
                    throw new IOException("Not connected"); //$NON-NLS-1$
                }

                if (buffer == null)
                {
                    // luni.CC=InputStream is closed
                    throw new IOException("InputStream is closed"); //$NON-NLS-1$
                }

                if (lastWriter != null && !lastWriter.isAlive() && (inJ < 0))
                {
                    // luni.CD=Write end dead
                    throw new IOException("Write end dead"); //$NON-NLS-1$
                }

                /*
                 * Set the last thread to be reading on this PipedInputStream. 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
                {
                    int attempts = 3;
                    while (inJ == -1)
                    {
                        // Are we at end of stream?
                        if (isClosed)
                        {
                            return -1;
                        }
                        if ((attempts-- <= 0) && lastWriter != null && !lastWriter.isAlive())
                        {
                            // luni.CE=Pipe broken
                            throw new IOException("Pipe broken"); //$NON-NLS-1$
                        }
                        // Notify callers of receive()
                        this.notifyAll();
                        this.wait(1000);
                    }
                }
                catch (java.lang.InterruptedException e)
                {
                    throw new InterruptedIOException();
                }

                int copyLength = 0;
                /* Copy bytes from out to end of buffer first */
                if (outJ >= inJ)
                {
                    copyLength = count > (buffer.Length - outJ) ? buffer.Length - outJ
                            : count;
                    java.lang.SystemJ.arraycopy(buffer, outJ, bytes, offset, copyLength);
                    outJ += copyLength;
                    if (outJ == buffer.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 bytesCopied = copyLength;
                /* Copy bytes from 0 to the number of available bytes */
                copyLength = inJ - outJ > (count - bytesCopied) ? count - bytesCopied
                        : inJ - outJ;
                java.lang.SystemJ.arraycopy(buffer, outJ, bytes, offset + bytesCopied, copyLength);
                outJ += copyLength;
                if (outJ == inJ)
                {
                    // empty buffer
                    inJ = -1;
                    outJ = 0;
                }
                return bytesCopied + copyLength;
            }
        }
Example #17
0
        /**
         * Read informations of a rpm file out of an input stream.
         *
         * @param rpmInputStream The input stream representing the rpm file
         * @throws IOException if an error occurs during read of the rpm file
         */
        private void readFromStream(java.io.InputStream rpmInputStream)
        {//throws IOException {
            ByteCountInputStream allCountInputStream = new ByteCountInputStream(
                rpmInputStream);

            java.io.InputStream inputStream = new java.io.DataInputStream(allCountInputStream);

            lead      = new RPMLead((java.io.DataInputStream)inputStream);
            signature = new RPMSignature((java.io.DataInputStream)inputStream, store);

            if (logger.isLoggable(java.util.logging.Level.FINER))
            {
                logger.finer("Signature Size: " + signature.getSize());
            }

            header = new RPMHeader((java.io.DataInputStream)inputStream, store);

            if (logger.isLoggable(java.util.logging.Level.FINER))
            {
                logger.finer("Header Size: " + header.getSize());
            }

            DataTypeIf payloadTag    = getTag("PAYLOADFORMAT");
            String     payloadFormat = payloadTag != null?payloadTag.toString()
                                           : "cpio";

            DataTypeIf payloadCompressionTag = getTag("PAYLOADCOMPRESSOR");
            String     payloadCompressor     = payloadCompressionTag != null?payloadCompressionTag
                                               .toString()
                                                   : "gzip";

            if (payloadFormat.equals("cpio"))
            {
                if (logger.isLoggable(java.util.logging.Level.FINER))
                {
                    logger.finer("PAYLOADCOMPRESSOR: " + payloadCompressor);
                }

                if (payloadCompressor.equals("gzip"))
                {
                    inputStream = new GzipCompressorInputStream(allCountInputStream);
                }
                else if (payloadCompressor.equals("bzip2"))
                {
                    inputStream = new BZip2CompressorInputStream(allCountInputStream);
                }
                else if (payloadCompressor.equals("lzma"))
                {
                    try
                    {
                        java.io.PipedOutputStream pout = new java.io.PipedOutputStream();
                        inputStream = new java.io.PipedInputStream(pout);
                        byte[] properties = new byte[5];
                        if (allCountInputStream.read(properties, 0, 5) != 5)
                        {
                            throw (new java.io.IOException("input .lzma is too short"));
                        }
                        SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
                        decoder.SetDecoderProperties(properties);
                        long outSize = 0;
                        for (int i = 0; i < 8; i++)
                        {
                            int v = allCountInputStream.read();
                            if (v < 0)
                            {
                                throw (new java.io.IOException("lzma error : Can't Read 1"));
                            }
                            outSize |= ((long)v) << (8 * i);
                        }
                        if (outSize == -1)
                        {
                            outSize = java.lang.Long.MAX_VALUE;
                        }

                        Decode decoderRunnable = new Decode(decoder,
                                                            allCountInputStream, pout, outSize);
                        java.lang.Thread t = new java.lang.Thread(decoderRunnable, "LZMA Decoder");
                        t.start();
                    }
                    catch (java.lang.NoClassDefFoundError)
                    {
                        String message = "No LZMA library found. Attach p7zip library to classpath (http://p7zip.sourceforge.net/)";
                        logger.severe(message);
                        throw new java.io.IOException(message);
                    }
                }
                else if (payloadCompressor.equals("none"))
                {
                    inputStream = allCountInputStream;
                }
                else
                {
                    throw new java.io.IOException("Unsupported compressor type "
                                                  + payloadCompressor);
                }

                ByteCountInputStream    countInputStream = new ByteCountInputStream(inputStream);
                CpioArchiveInputStream  cpioInputStream  = new CpioArchiveInputStream(countInputStream);
                CpioArchiveEntry        readEntry;
                java.util.List <String> fileNamesList = new java.util.ArrayList <String>();
                String fileEntry;
                while ((readEntry = cpioInputStream.getNextCPIOEntry()) != null)
                {
                    if (logger.isLoggable(java.util.logging.Level.FINER))
                    {
                        logger.finer("Read CPIO entry: " + readEntry.getName()
                                     + " ;mode:" + readEntry.getMode());
                    }
                    if (readEntry.isRegularFile() || readEntry.isSymbolicLink() ||
                        readEntry.isDirectory())
                    {
                        fileEntry = readEntry.getName();
                        if (fileEntry.startsWith("./"))
                        {
                            fileEntry = fileEntry.substring(1);
                        }
                        fileNamesList.add(fileEntry);
                    }
                }
                store.setTag("FILENAMES", TypeFactory.createSTRING_ARRAY((String[])fileNamesList.toArray(new String[fileNamesList.size()])));

                setHeaderTagFromSignature("ARCHIVESIZE", "PAYLOADSIZE");
                // check ARCHIVESIZE with countInputStream.getCount();
                Object archiveSizeObject = getTag("ARCHIVESIZE");
                if (archiveSizeObject != null)
                {
                    if (archiveSizeObject is INT32)
                    {
                        int archiveSize = ((INT32)archiveSizeObject).getData()[0];
                        if (archiveSize != countInputStream.getCount())
                        {
                            new java.io.IOException("ARCHIVESIZE not correct");
                        }
                    }
                }
                store.setTag("J_ARCHIVESIZE", TypeFactory
                             .createINT64(new long[] { countInputStream.getCount() }));
            }
            else
            {
                throw new java.io.IOException("Unsupported Payload type " + payloadFormat);
            }

            // filling in signatures
            // TODO: check signatures!
            setHeaderTagFromSignature("SIGSIZE", "SIZE");
            setHeaderTagFromSignature("SIGLEMD5_1", "LEMD5_1");
            setHeaderTagFromSignature("SIGPGP", "PGP");
            setHeaderTagFromSignature("SIGLEMD5_2", "LEMD5_2");
            setHeaderTagFromSignature("SIGMD5", "MD5");
            setHeaderTagFromSignature("SIGGPG", "GPG");
            setHeaderTagFromSignature("SIGPGP5", "PGP5");
            setHeaderTagFromSignature("DSAHEADER", "DSA");
            setHeaderTagFromSignature("RSAHEADER", "RSA");
            setHeaderTagFromSignature("SHA1HEADER", "SHA1");

            store.setTag("J_FILESIZE", TypeFactory
                         .createINT64(new long[] { allCountInputStream.getCount() }));

            rpmInputStream.close();
        }