All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.jbang.util.ConsoleInputReadTask Maven / Gradle / Ivy

The newest version!
package dev.jbang.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;

public class ConsoleInputReadTask implements Callable {
	private final InputStream in;

	public ConsoleInputReadTask(InputStream in) {
		this.in = in;
	}

	public String call() throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String input;
		do {
			try {
				// wait until we have data to complete a readLine()
				while (!br.ready()) {
					Thread.sleep(200);
				}
				input = br.readLine();
			} catch (InterruptedException e) {
				return null;
			}
		} while ("".equals(input));
		return input;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy