模拟Entry密钥(通过在Java中创建InputStream子类)
我正在尝试创建一个模拟类,以模拟在给定此InputStream的线程工作2秒后按Enter键。 从该流读取的线程使用BufferedReader.readLine()-等待按下Enter键。 在重写的InputStream.read方法中返回什么是正确的,以使其在任何平台上正常工作? System.lineSeparator().getBytes()[0]是否是要返回的正确值模拟Entry密钥(通过在Java中创建InputStream子类),java,mocking,inputstream,enter,Java,Mocking,Inputstream,Enter,我正在尝试创建一个模拟类,以模拟在给定此InputStream的线程工作2秒后按Enter键。 从该流读取的线程使用BufferedReader.readLine()-等待按下Enter键。 在重写的InputStream.read方法中返回什么是正确的,以使其在任何平台上正常工作? System.lineSeparator().getBytes()[0]是否是要返回的正确值 private static class MockEnterStream extends InputStream
private static class MockEnterStream extends InputStream {
private static final long DELAY = 2000;
public static boolean firstCall = true;
@Override
public int read() throws IOException {
if (firstCall) {
try {
Thread.currentThread().sleep(DELAY);
firstCall = false;
return System.lineSeparator().getBytes()[0]; //Enter key
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return -1; // no data
}
}