/** * Connects to the given {@code address} and {@code port}. Data can be sent to the remote server through the * {@link OutputStream} returned by {@link #getOutputStream()}, and data can be read from the remote server * through the {@link InputStream} returned by {@link #getInputStream()}. */ public TcpClient(InetAddress address, int port) throws IOException { this.socket = new Socket(address, port); LOGGER.fine("Connected to " + this.socket.getRemoteSocketAddress()); InputStream in = this.socket.getInputStream(); OutputStream out = this.socket.getOutputStream(); boolean logData = LOGGER.isLoggable(FINEST); this.fromServer = ( logData ? IoUtil.wye(in, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, "<From Server< "))) : in ); this.toServer = ( logData ? IoUtil.tee(out, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, ">To Server> "))) : out ); }
private void openCurrentFile() throws IOException { this.byteCount = ConsumerUtil.store(); this.setOutputStream(IoUtil.tee( new FileOutputStream(this.currentFile, true), IoUtil.lengthWritten(ConsumerUtil.cumulate(this.byteCount, 0L)) )); }
/** * Writes the body of this message <em>synchronously</em> to the given {@link OutputStream}. */ private void writeBody(String prefix, OutputStream out) throws IOException { // Check "Content-Encoding: gzip" GZIPOutputStream finishable = null; if ("gzip".equalsIgnoreCase(this.getHeader("Content-Encoding"))) { LOGGER.fine(prefix + "GZIP-encoded contents"); out = (finishable = new GZIPOutputStream(out)); } final boolean isXml; { String ct = this.getHeader("Content-Type"); isXml = ct != null && ct.indexOf("text/xml") != -1; } if (LOGGER.isLoggable(FINE)) { LOGGER.fine(prefix + "Writing message body:"); Writer lw = LogUtil.logWriter(LOGGER, FINE, prefix); out = IoUtil.tee(out, ( isXml ? new WriterOutputStream(new XMLFormatterWriter(lw)) : new HexOutputStream(lw) )); } this.body.write(out); if (finishable != null) finishable.finish(); out.flush(); }
out = IoUtil.tee( out, new HexOutputStream(LogUtil.logWriter(LOGGER, FINEST, ">To Client> "))
@Override public void handleConnection( InputStream clientIn, OutputStream clientOut, InputStream serverIn, OutputStream serverOut, InetSocketAddress clientLocalSocketAddress, InetSocketAddress clientRemoteSocketAddress, InetSocketAddress serverLocalSocketAddress, InetSocketAddress serverRemoteSocketAddress, Stoppable stoppable ) throws IOException { ThreadUtil.parallel( IoUtil.copyRunnable(clientIn, IoUtil.tee( serverOut, new HexOutputStream(LogUtil.logWriter(LOGGER, FINER, "> ")) )), IoUtil.copyRunnable(serverIn, IoUtil.tee( clientOut, new HexOutputStream(LogUtil.logWriter(LOGGER, FINER, "< ")) )), stoppable ); } }
/** * Invokes <var>writeContents</var>{@code .consume()} with an output stream subject that writes the data through to * the given <var>outputStream</var>. * * @return The number of bytes that were written through */ public static long writeAndCount( ConsumerWhichThrows<? super OutputStream, ? extends IOException> writeContents, OutputStream outputStream ) throws IOException { Produmer<Long, Long> count = ConsumerUtil.store(); writeContents.consume(IoUtil.tee(outputStream, IoUtil.lengthWritten(ConsumerUtil.cumulate(count, 0L)))); Long result = count.produce(); return result == null ? 0L : result; }
responseSize = ConsumerUtil.store(); out = IoUtil.tee(out, IoUtil.lengthWritten(ConsumerUtil.cumulate(responseSize, 0))); in = IoUtil.wye(in, IoUtil.lengthWritten(ConsumerUtil.cumulate(requestSize, 0)));