import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;

public class Main {
    private static class AppendableInputStream extends InputStream {
        private final List<Byte> bytes = new ArrayList<>();

        public synchronized void append(byte[] bytes) {
            for (byte b : bytes) {
                this.bytes.add(b);
            }
            notifyAll();
        }

        @Override
        public synchronized int read() {
            while (available() == 0) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            return bytes.remove(0);
        }

        @Override
        public synchronized int read(byte[] b, int off, int len) {
            len = Math.max(1, Math.min(len, available()));
            for (int i = off; i < off + len; i++) {
                b[i] = (byte) read();
            }
            return len;
        }

        @Override
        public synchronized int available() {
            return bytes.size();
        }
    }

    public static void main(String[] args) throws IOException {
        try (AppendableInputStream appendableInputStream = new AppendableInputStream()) {
            appendableInputStream.append(("a".repeat(8191) + "\r\n").getBytes(UTF_8));
            printNextLine(appendableInputStream);
            appendableInputStream.append(("b\r\n").getBytes(UTF_8));
            printNextLine(appendableInputStream);
        }
    }

    private static void printNextLine(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = bufferedReader.readLine();
        System.out.println(line);
    }
} 