InputStream
スロー ストリームをシミュレートするモックを次に示します。
class SlowInputStream extends InputStream{
private String internal = "5 6\nNext Line\n";
private int position = 0;
@Override
public int available(){
if(position==internal.length()) return 0;
else return 1;
}
@Override
public int read() throws IOException {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new IOException("Sleep Interrupted");
}
if(position==internal.length()) return -1;
return internal.charAt(position++);
}
}
そして、ここにテストコードがあります:
Scanner s = new Scanner(new SlowInputStream());
int i=s.nextInt();
System.out.println("i="+i);
int j=s.nextInt();
System.out.println("j="+j);
s.nextLine();
String line = s.nextLine();
System.out.println("line="+line);
s.close();
上記のコードの動作は、しばらく停止して 3 行を出力することです。同じことを出力するが、待ち時間を行間で分割できるコードはどれですか?