0

I have written a code to stub the System.out.println and passing the object of BuffredReader into it. My question is how to recover the BufferedReader object?

import java.io.*;

class Test {

  public static void main(String args[]) throws IOException {
    // stubbing the default print statement
    ByteArrayOutputStream outcontent = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outcontent);

      //createing a BufferedReader obj and passing to print
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
      System.out.println(br);

      //trying to get the value from the outcontent and 
      // but i need to serialise this to BufferedReader
      System.err.println(outContent.toString());

    }
  }

I have tried to use this answer but i am getting the error java.io.StreamCorruptedException: invalid stream header: 6A617661

1 Answers1

0

Since you already have a ByteArrayOutputStream, you should try something like this:

BufferedReader reader = new BuffererdBeader( new InputStreamReader(
                            new ByteArrayInputStream( outcontent.toByteArray() ) ) );
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33