Run a Process, and read the various streams so there is not a buffer
overrun.

@param p      The Process to be executed
@param output The Stream to receive the Process' output stream
@return true if the Process returned 0, false otherwise
551861
public static boolean runCommand(final Process p, final OutputStream output) {
        return runCommand(p, output, output, new ArrayList<String>());
    }
1228213
@Override
    protected void outputPass() {
        if(!prototype.isOutputSupported())
            return;

        // take a snapshot of the current container state.
        final LinkedList<Object> toOutput = new LinkedList<Object>(instances.keySet());

        final Executor executorService = getOutputExecutorService();
        final Semaphore taskLock = (executorService != null) ? new Semaphore(outputConcurrency) : null;

        // This keeps track of the number of concurrently running
        // output tasks so that this method can wait until they're
        // all done to return.
        //
        // It's also used as a condition variable signaling on its
        // own state changes.
        final AtomicLong numExecutingOutputs = new AtomicLong(0);

        final MutRef<WorkingQueueHolder> mref = new MutRef<>();

        // keep going until all of the outputs have been invoked
        while(toOutput.size() > 0 && isRunning.get()) {
            for(final Iterator<Object> iter = toOutput.iterator(); iter.hasNext();) {
                final Object key = iter.next();

                final InstanceWrapper wrapper = instances.get(key);

                if(wrapper != null) { // if the MP still exists
                    final WorkingQueueHolder mailbox = setIfAbsent(wrapper.mailbox, () -> mref.set(new WorkingQueueHolder(true)));
                    if(mailbox == null) { // this means I got it.
                        // it was created locked so no one else will be able to drop messages in the mailbox.
                        final Semaphore taskSepaphore = taskLock;

                        // This task will release the wrapper's lock.
                        final Runnable task = new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    if(isRunning.get())
                                        invokeOperation(wrapper.instance, Operation.output, null);
                                } finally {
                                    wrapper.mailbox.set(null); // releases this back to the world

                                    // this signals that we're done.
                                    synchronized(numExecutingOutputs) {
                                        numExecutingOutputs.decrementAndGet();
                                        numExecutingOutputs.notifyAll();
                                    }
                                    if(taskSepaphore != null)
                                        taskSepaphore.release();
                                }
                            }
                        };

                        synchronized(numExecutingOutputs) {
                            numExecutingOutputs.incrementAndGet();
                        }

                        if(executorService != null) {
                            try {
                                taskSepaphore.acquire();
                                executorService.execute(task);
                            } catch(final RejectedExecutionException e) {
                                wrapper.mailbox.set(null); // we never got into the run so we need to release the lock
                                // this may happen because of a race condition between the
                                taskSepaphore.release();
                            } catch(final InterruptedException e) {
                                // this can happen while blocked in the semaphore.acquire.
                                // if we're no longer running we should just get out
                                // of here.
                                //
                                // Not releasing the taskSepaphore assumes the acquire never executed.
                                // if (since) the acquire never executed we also need to release the
                                // wrapper lock or that Mp will never be usable again.
                                wrapper.mailbox.set(null); // we never got into the run so we need to release the lock
                            }
                        } else
                            task.run();

                        iter.remove();

                    } // didn't get the lock
                } else { // end if mp exists. Otherwise the mp is already gone.
                    iter.remove();
                    LOGGER.warn("There was an attempt to output a non-existent Mp for key " + SafeString.objectDescription(key));
                }
            } // loop over every mp
        } // end while there are still Mps that haven't had output invoked.

        // =======================================================
        // now make sure all of the running tasks have completed
        synchronized(numExecutingOutputs) {
            while(numExecutingOutputs.get() > 0) {
                try {
                    numExecutingOutputs.wait();
                } catch(final InterruptedException e) {
                    // if we were interupted for a shutdown then just stop
                    // waiting for all of the threads to finish
                    if(!isRunning.get())
                        break;
                    // otherwise continue checking.
                }
            }
        }
        // =======================================================
    }
858368
private static void process(String input, String output) throws Exception
   {
      FileInputStream is = new FileInputStream(input);
      PrintWriter pw = new PrintWriter(new FileWriter(output));

      byte[] buffer = new byte[is.available()];
      is.read(buffer);
      pw.println(ByteArrayHelper.hexdump(buffer, 0, buffer.length, true, 16, ""));

      is.close();
      pw.flush();
      pw.close();
   }
1329840
private synchronized void print(String output) {
        if (generateOneDoc) 
            writer.print(output);
        else 
            writer.println(output);
    }
869902
@SuppressWarnings("unchecked")
  public void run(RecordReader<K1, V1> input, OutputCollector<K2, V2> output,
                  Reporter reporter) throws IOException {
    Application<K1, V1, K2, V2> application = null;
    try {
      RecordReader<FloatWritable, NullWritable> fakeInput = 
        (!Submitter.getIsJavaRecordReader(job) && 
         !Submitter.getIsJavaMapper(job)) ? 
	  (RecordReader<FloatWritable, NullWritable>) input : null;
      application = new Application<K1, V1, K2, V2>(job, fakeInput, output, 
                                                    reporter,
          (Class<? extends K2>) job.getOutputKeyClass(), 
          (Class<? extends V2>) job.getOutputValueClass());
    } catch (InterruptedException ie) {
      throw new RuntimeException("interrupted", ie);
    }
    DownwardProtocol<K1, V1> downlink = application.getDownlink();
    boolean isJavaInput = Submitter.getIsJavaRecordReader(job);
    downlink.runMap(reporter.getInputSplit(), 
                    job.getNumReduceTasks(), isJavaInput);
    boolean skipping = job.getBoolean("mapred.skip.on", false);
    try {
      if (isJavaInput) {
        // allocate key & value instances that are re-used for all entries
        K1 key = input.createKey();
        V1 value = input.createValue();
        downlink.setInputTypes(key.getClass().getName(),
                               value.getClass().getName());
        
        while (input.next(key, value)) {
          // map pair to output
          downlink.mapItem(key, value);
          if(skipping) {
            //flush the streams on every record input if running in skip mode
            //so that we don't buffer other records surrounding a bad record.
            downlink.flush();
          }
        }
        downlink.endOfInput();
      }
      application.waitForFinish();
    } catch (Throwable t) {
      application.abort(t);
    } finally {
      application.cleanup();
    }
  }
309566
boolean process(InputStream inputStream) throws IOException {
        LOG.entering(CLASS_NAME, "process");
        int b;
        for (;;) {
            
            switch (state) {
            // handle alignment with start-of-frame boundary (after mark)
            case START_OF_FRAME:
                b = inputStream.read();
                if (b == -1) {
                    return false; //end of stream
                }
                fin = (b & 0x80) != 0;
                opcode = WsFrameEncodingSupport.Opcode.getById((b & 0x0f));
                state = DecodingState.READING_PAYLOADLENGTH; //start read mask & payload length byte 
            break;
            
            case READING_PAYLOADLENGTH:
            
                //reading mask bit and payload length (7)
                b = inputStream.read();
                if (b == -1) {
                    return false; //end of stream
                }
                
                masked = (b & 0x80) != 0;
                if (masked) {
                    maskkeyBuf = ByteBuffer.allocate(4); //4 byte mask key
                }
                dataLength = b & 0x7f;
                
                if (dataLength==126) {
                    //length is 16 bit long unsigned int - must fill first two bytes with 0,0 to handle unsigned
                    state = DecodingState.READING_PAYLOADLENGTH_EXT;
                    payLoadLengthBuf = ByteBuffer.allocate(4);
                    payLoadLengthBuf.put(new byte[] {0x00, 0x00}); //fill 2 bytes with 0
                } else if (dataLength==127) {
                    //length is 64 bit long
                    state = DecodingState.READING_PAYLOADLENGTH_EXT;
                    payLoadLengthBuf = ByteBuffer.allocate(8);
                }
                else {
                    state = DecodingState.READING_MASK_KEY;

                }
            break;
            
            case READING_PAYLOADLENGTH_EXT:
                byte[] bytes = new byte[payLoadLengthBuf.remaining()];
                int num = inputStream.read(bytes);
                if (num == -1) {
                    return false; //end of stream
                }
                payLoadLengthBuf.put(bytes, 0, num);
                if (!payLoadLengthBuf.hasRemaining()) {
                    //payload length bytes has been read
                    payLoadLengthBuf.flip();
                    if (payLoadLengthBuf.capacity() == 4) {
                        // 16 bit length
                        dataLength = payLoadLengthBuf.getInt();
                    }
                    else {
                        dataLength = (int) payLoadLengthBuf.getLong();
                    }
                    state = DecodingState.READING_MASK_KEY;
                    break;
                }
            break;

            case READING_MASK_KEY:
                if (!masked) {
                    //unmasked, skip READ_MASK_KEY
                    data = ByteBuffer.allocate(dataLength);
                    state = DecodingState.READING_PAYLOAD;
                    break;
                }
                bytes = new byte[maskkeyBuf.remaining()];
                num = inputStream.read(bytes);
                if (num == -1) {
                    return false; //end of stream
                }
                maskkeyBuf.put(bytes, 0, num);
                if (!maskkeyBuf.hasRemaining()) {
                    //4 bytes has been read, done with mask key
                    maskkeyBuf.flip(); //move postion to 0
                    maskkey = maskkeyBuf.getInt();
                    data = ByteBuffer.allocate(dataLength);
                    state = DecodingState.READING_PAYLOAD;
                }
            break;
            
            case READING_PAYLOAD:
                if (dataLength == 0) {
                    // empty message
                    state = DecodingState.END_OF_FRAME;
                    break;
                }
                bytes = new byte[data.remaining()];
                num = inputStream.read(bytes);
                if (num == -1) {
                    return false; //end of stream
                }
                data.put(bytes, 0, num);
                if (!data.hasRemaining()) {
                    //all payload has been read
                    data.flip();
                    state = DecodingState.END_OF_FRAME;
                    break;
                }
            break;
            
            case END_OF_FRAME:
                //finished load payload
                switch (opcode) {
                    case BINARY:
                        if (masked) {
                            WsFrameEncodingSupport.unmask(data, maskkey);
                        }
                        listener.messageReceived(data, "BINARY");
                        break;
                    case TEXT:
                        if (masked) {
                            WsFrameEncodingSupport.unmask(data, maskkey);
                        }
                        listener.messageReceived(data, "TEXT");
                        break;
                    case PING:
                        listener.messageReceived(data, "PING");
                    break;
                    case PONG:
                        //do nothing
                        break;
                    case CLOSE:
                        listener.messageReceived(data, "CLOSE");
                        break;
                }
                state = DecodingState.START_OF_FRAME;
                
            break;
            }
        }
        
    }
960729
@Override
    public String output() {
        String output = process.output();
        publisher.publish(new Returned(command, output));
        return output;
    }
313911
private boolean run(final Namespace options, final Target target, final PrintStream out,
                      final PrintStream err, final String username, final boolean json,
                      final BufferedReader stdin)
      throws Exception {

    final HeliosClient client = Utils.getClient(target, err, username, options);
    if (client == null) {
      return false;
    }

    try {
      final int result = run(options, client, out, json, stdin);
      return result == 0;
    } catch (ExecutionException e) {
      final Throwable cause = e.getCause();
      // if target is a domain, print message like
      // "Request timed out to master in ash.spotify.net (http://ash2-helios-a4.ash2.spotify.net)",
      // otherwise "Request timed out to master http://ash2-helios-a4.ash2.spotify.net:5800"
      if (cause instanceof TimeoutException) {
        err.println("Request timed out to master in " + target);
      } else {
        throw new RuntimeException(cause);
      }
      return false;
    } finally {
      client.close();
    }
  }
1027283
public static void copy(InputStream input, OutputStream output) throws IOException {
		byte[] buf = new byte[BUFFER_SIZE];
		int num = 0;

		while ((num = input.read(buf, 0, buf.length)) != -1) {
			output.write(buf, 0, num);
		}
	}
815984
def multi_p_run(tot_num, _func, worker, params, n_process):
    
    from multiprocessing import Process, Queue
    out_q = Queue()
    procs = []

    split_num = split_seq(list(range(0, tot_num)), n_process)

    print(tot_num, ">>", split_num)

    split_len = len(split_num)
    if n_process > split_len:
        n_process = split_len

    for i in range(n_process):
        _p = Process(target=_func,
                     args=(worker, split_num[i][0], split_num[i][1],
                           params, out_q))
        _p.daemon = True
        procs.append(_p)
        _p.start()

    try:
        result = []
        for i in range(n_process):
            result.append(out_q.get())
        for i in procs:
            i.join()
    except KeyboardInterrupt:
        print('Killing all the children in the pool.')
        for i in procs:
            i.terminate()
            i.join()
        return -1

    while not out_q.empty():
        print(out_q.get(block=False))

    return result
700095
public static void pipe(InputStream is, OutputStream os, int buffersize) throws IOException {
        byte[] buffer = new byte[buffersize];
        while (is.read(buffer) != -1) {
            os.write(buffer);
        }
    }
691856
public void run(List<Record> input, List<Record> output) {
        if (input.size() < 0) {
            fail("input size is 0");
        }
        driver.setConfiguration(conf);

        Key key = input.get(0).getKey();
        boolean groupingNothing = input.get(0).isGroupingNothing();

        List<Writable> values = new ArrayList<Writable>();
        for (Record record : input) {
            values.add(record.isValueNothing() ? NullWritable.get() : record.getValue());
        }

        driver.withInput(groupingNothing ? NullWritable.get() : key,
                         values);

        if (output != null) {
            for (Record r : output) {
                driver.withOutput(r.isGroupingNothing() ? NullWritable.get() : r.getKey(),
                                  r.isValueNothing() ? NullWritable.get() : r.getValue());
            }
        }

        driver.runTest();
    }
884421
public static int run(String[] args, PrintWriter out) {
        JdepsTask t = new JdepsTask();
        t.setLog(out);
        return t.run(args);
    }
376561
public static void process(InputStream inputStream, StreamHandler handler, byte[] buffer) throws IOException {
    LOGGER.trace("process(InputStream, StreamHandler, byte[])");
    LOGGER.debug("buffer size: {} bytes", buffer != null ? buffer.length : "null");
    int read = -1;
    while( (read = inputStream.read(buffer)) != -1 ) {
      LOGGER.debug("{} bytes read from stream", read);
      handler.handleStreamBuffer(buffer, 0, read);
    }
  }
655961
protected void process(StringBuffer buf, Object p) {
		buf.append('<');
		buf.append(p);
		buf.append('>');
		buf.append(this.get(p));
		buf.append("</");
		buf.append(p);
		buf.append('>');
	}
360103
@Override
  public void run() {
    synchronized (this) {
      started = true;
    }
    finished = false;

    final byte[] buf = new byte[bufferSize];

    try {
      int length;
      while (true) {
        waitForInput(is);

        if (finish || Thread.interrupted()) {
          break;
        }

        length = is.read(buf);
        if (length <= 0 || Thread.interrupted()) {
          break;
        }
        os.write(buf, 0, length);
        if (autoflush) {
          os.flush();
        }
        if (finish) { //NOSONAR
          break;
        }
      }
      // On completion, drain any available data (which might be the first data available for quick executions)
      if (finish) {
        while((length = is.available()) > 0) {
          if (Thread.interrupted()) {
            break;
          }
          length = is.read(buf, 0, Math.min(length, buf.length));
          if (length <= 0) {
            break;
          }
          os.write(buf, 0, length);
        }
      }
      os.flush();
    } catch (InterruptedException ie) {
      // likely PumpStreamHandler trying to stop us
    } catch (Exception e) {
      synchronized (this) {
        exception = e;
      }
    } finally {
      if (closeWhenExhausted) {
        Closeables.close(os);
      }
      finished = true;
      finish = false;
      synchronized (this) {
        notifyAll();
      }
    }
  }
1303209
public boolean decode(ChannelBuffer buffer, CommandOutput<K, V, ?> output) {
        int length, end;
        ByteBuffer bytes;

        if (stack.isEmpty()) {
            stack.add(new State());
        }

        loop:

        while (!stack.isEmpty()) {
            State state = stack.peek();

            if (state.type == null) {
                if (!buffer.readable()) break;
                state.type = readReplyType(buffer);
                buffer.markReaderIndex();
            }

            switch (state.type) {
                case SINGLE:
                    if ((bytes = readLine(buffer)) == null) break loop;
                    if (!QUEUED.equals(bytes)) {
                        output.set(bytes);
                    }
                    break;
                case ERROR:
                    if ((bytes = readLine(buffer)) == null) break loop;
                    output.setError(bytes);
                    break;
                case INTEGER:
                    if ((end = findLineEnd(buffer)) == -1) break loop;
                    output.set(readLong(buffer, buffer.readerIndex(), end));
                    break;
                case BULK:
                    if ((end = findLineEnd(buffer)) == -1) break loop;
                    length = (int) readLong(buffer, buffer.readerIndex(), end);
                    if (length == -1) {
                        output.set(null);
                    } else {
                        state.type = BYTES;
                        state.count = length + 2;
                        buffer.markReaderIndex();
                        continue loop;
                    }
                    break;
                case MULTI:
                    if (state.count == -1) {
                        if ((end = findLineEnd(buffer)) == -1) break loop;
                        length = (int) readLong(buffer, buffer.readerIndex(), end);
                        state.count = length;
                        buffer.markReaderIndex();
                    }

                    if (state.count <= 0) break;

                    state.count--;
                    stack.addFirst(new State());
                    continue loop;
                case BYTES:
                    if ((bytes = readBytes(buffer, state.count)) == null) break loop;
                    output.set(bytes);
            }

            buffer.markReaderIndex();
            stack.remove();
            output.complete(stack.size());
        }

        return stack.isEmpty();
    }