简体   繁体   中英

Write from output stream to output stream

I have an outputstream, to which the client A is writing , I need to forward it in byte chuncks to client B.

I'd like to connect the output stream of client A with the output stream of client B. Is that possible? What are ways to do that? I don't need to fork/clone I rather need to take some of the data from stream A and move it to stream B(ie the data don't stay in stream A)

Note:A and B are processes and outputstream of client A can't be directly supplied to client B. Constraint:Limited memory

Try this approach; it transfers bytes ("Hello world") written to 'out' to 'out2' without use of an InputStream:

import java.io.ByteArrayOutputStream;

public class OutputStreamEx {

 public static void main(String[] args) {
    String content = "Hello world";
    byte[] bytes = content.getBytes();
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        out.write(bytes, 0, bytes.length);
        ByteArrayOutputStream out2 = new ByteArrayOutputStream();
        out.writeTo(out2);
        System.out.println(out2.toString());
     } catch (Exception ex) {
        ex.printStackTrace();
     }
  }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM