Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(i.e 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

like image 911
Yakov Avatar asked Sep 11 '25 10:09

Yakov


1 Answers

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();
     }
  }
 }
like image 186
Matt Campbell Avatar answered Sep 13 '25 00:09

Matt Campbell