Buffer a stream, flushing it to <code>log</code> and returning it as input

@param input
@param message
@param log
@return
428144
private static InputStream bufferToTraceLog(InputStream input, String message, Logger log) {
        InputStream output;
        try {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int read;
            byte[] data = new byte[16384];
            while ((read = input.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, read);
            }
            buffer.flush();
            log.trace(message + "\n\n\n" + new String(buffer.toByteArray(), CHARSET_OUTPUT) + "\n\n\n");
            output = new ByteArrayInputStream(buffer.toByteArray());
        } catch (Exception e) {
            throw new RuntimeException("could not buffer stream", e);
        }
        return output;
    }
727094
public static void queueMessage(final String level, final String message) {
		try {
			LogAppender<LogEvent> appender = LogManager.getAppender();

			if (appender != null) {
				LogEvent.Builder builder = LogEvent.newBuilder();
				builder.level(level);
				builder.message(message);
				
				if ((level != null) && ("ERROR".equals(level.toUpperCase()))) {
					StackTraceElement[] stackTrace = new Throwable().getStackTrace();

					if ((stackTrace != null) && (1 < stackTrace.length)) {
						StackTraceElement caller = stackTrace[1];
						builder.className(caller.getClassName());
						builder.methodName(caller.getMethodName());
						builder.lineNumber(caller.getLineNumber());						
					}
				}
				
				appender.append(builder.build());
			}
		} catch (Throwable t) {
			LOGGER.info("Unable to queue message to Stackify Log API service: {} {}", level, message, t);
		}
	}
1203632
private void flushBuffer() {
		// Do the actual logging:
		removes.ensureCapacity(buffer.size());
		for(Iterator<LoggingEvent> i = buffer.iterator(); i.hasNext();) {
			try {
				LoggingEvent event = (LoggingEvent)i.next();
				doLog(event);
				removes.add(event);
			} catch(Exception e) {
				errorHandler.error("Failed to flush log", e, ErrorCode.FLUSH_FAILURE);
			}
		}

		// Remove from the buffer any events that were reported:
		buffer.removeAll(removes);

		// Clear the buffer of reported events:
		removes.clear();
	}
531477
@Override
  public void writePayload(InputStream message) {
    verifyNotClosed();
    messagesBuffered++;
    currentMessageSeqNo++;
    currentMessageWireSize = 0;
    statsTraceCtx.outboundMessage(currentMessageSeqNo);
    boolean compressed = messageCompression && compressor != Codec.Identity.NONE;
    int written = -1;
    int messageLength = -2;
    try {
      messageLength = getKnownLength(message);
      if (messageLength != 0 && compressed) {
        written = writeCompressed(message, messageLength);
      } else {
        written = writeUncompressed(message, messageLength);
      }
    } catch (IOException e) {
      // This should not be possible, since sink#deliverFrame doesn't throw.
      throw Status.INTERNAL
          .withDescription("Failed to frame message")
          .withCause(e)
          .asRuntimeException();
    } catch (RuntimeException e) {
      throw Status.INTERNAL
          .withDescription("Failed to frame message")
          .withCause(e)
          .asRuntimeException();
    }

    if (messageLength != -1 && written != messageLength) {
      String err = String.format("Message length inaccurate %s != %s", written, messageLength);
      throw Status.INTERNAL.withDescription(err).asRuntimeException();
    }
    statsTraceCtx.outboundUncompressedSize(written);
    statsTraceCtx.outboundWireSize(currentMessageWireSize);
    statsTraceCtx.outboundMessageSent(currentMessageSeqNo, currentMessageWireSize, written);
  }
707136
public static void writeStreamToStream(InputStream input, OutputStream output)
  throws IOException {
    byte[] buffer = new byte[4096];
    while (true) {
      int len = input.read(buffer);
      if (len == -1) {
        break;
      }
      output.write(buffer, 0, len);
    }
  }
374894
public static void streamCopy(InputStream input, Writer output) throws IOException
	{
		if (input == null)
			throw new IllegalArgumentException("Must provide something to read from");
		if (output == null)
			throw new IllegalArgumentException("Must provide something to write to");

		streamCopy(new InputStreamReader(input), output);
	}
1037711
private void send(String message) {
    String scopeMessage = message.length() + " " + message;
    logger.finest("WRITE : " + scopeMessage);
    byte[] bytes = null;
    try {
      bytes = scopeMessage.getBytes("UTF-16BE");
    } catch (UnsupportedEncodingException e) {
      close();
      connectionHandler.onException(e);
      return;
    }
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    buffer.put(bytes);
    requests.add(buffer);
    monitor.modify(socketChannel, this, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
  }
637361
private void log(final String message) {
    if (this.optionalParams.isPresent()) {
      logger.log(logLevel, message, params);
    } else {
      logger.log(logLevel, message);
    }
  }
851631
public final T readBuffer(byte[] buffer) {
        if (buffer == null) {
            throw new InvalidArgument("buffer", buffer);
        } else if (buffer.length != recordSize) {
            final String fmt = "invalid buffer (%d bytes, expected %d)";
            final String msg = format(fmt, buffer.length, recordSize);
            throw new InvalidArgument(msg);
        }
        return read(buffer);
    }
1197399
protected void logSoapMessage(String logMessage, SoapMessage soapMessage, boolean incoming) throws TransformerException {
        Transformer transformer = createIndentingTransformer();
        StringWriter writer = new StringWriter();
        
        transformer.transform(soapMessage.getEnvelope().getSource(), new StreamResult(writer));
        logMessage(logMessage, XMLUtils.prettyPrint(writer.toString()), incoming);
    }
977160
protected void logWebServiceMessage(String logMessage, WebServiceMessage message, boolean incoming) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        
        try {
            message.writeTo(os);
            logMessage(logMessage, os.toString(), incoming);
        } catch (IOException e) {
            log.warn("Unable to log WebService message", e);
        }
    }
927944
public static void read(InputStream stream, byte[] buffer) throws IOException {
        int read = 0;
        while(read < buffer.length) {
            int newlyRead = stream.read(buffer, read, buffer.length - read);
            if(newlyRead == -1)
                throw new EOFException("Attempt to read " + buffer.length
                                       + " bytes failed due to EOF.");
            read += newlyRead;
        }
    }
1201935
void logDebug(final String message) {
      if(logger != null) {
         try {
            StringBuilder buf = new StringBuilder(name);
            buf.append(": ");
            buf.append(message);
            logger.debug(buf.toString());
         } catch(Throwable t) {
            //Ignore - logging should not kill anything
         }
      }
   }
78781
protected String readLineWithMessage(String message)
    {
        System.out.print(message + " ");
        try
        {
            BufferedReader rin = new BufferedReader(new InputStreamReader(System.in));
            return rin.readLine();
        }
        catch (Exception e)
        {
            return "";
        }
    }
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);
    }
  }
8906
public static long copy(final InputStream input,
                           final OutputStream output)
         throws IOException {
      return copy(input, output, DEFAULT_BUFFER_SIZE);
   }
804217
public static BufferedInputStream toBuffered(InputStream in) {
		return (in instanceof BufferedInputStream) ? (BufferedInputStream) in : new BufferedInputStream(in);
	}