/** * Peeks to see if there is a byte waiting in the input stream without * actually consuming the byte. * * @param timeout The amount of time to wait, 0 == forever * @return -1 on eof, -2 if the timeout expired with no available input * or the character that was read (without consuming it). * @throws IOException if anything wrong happens */ public int peek(long timeout) throws IOException { return read(timeout, true); }
/** * Attempts to read a character from the input stream for a specific * period of time. * * @param timeout The amount of time to wait for the character * @return The character read, -1 if EOF is reached, or -2 if the * read timed out. * @throws IOException if anything wrong happens */ public int read(long timeout) throws IOException { return read(timeout, false); }
@Override public int read() throws IOException { return read(0L, false); }
/** * This version of read() is very specific to jline's purposes, it * will always always return a single byte at a time, rather than filling * the entire buffer. * @param b the buffer * @param off the offset in the buffer * @param len the maximum number of chars to read * @throws IOException if anything wrong happens */ @Override public int read(char[] b, int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } int c = this.read(0L); if (c == EOF) { return EOF; } b[off] = (char) c; return 1; }
private int getUserAnswer(String question, int... allowedAnswers) throws IOException { final Set<Integer> allowedAnswerSet = IntStream.of(allowedAnswers).boxed().collect(Collectors.toSet()); final Terminal terminal = sqlLine.getLineReader().getTerminal(); final PrintWriter writer = terminal.writer(); writer.write(question); int c; // The logic to prevent reaction of SqlLineParser here do { c = terminal.reader().read(100); } while (c != -1 && !allowedAnswerSet.contains(c)); return c; }
start = System.currentTimeMillis(); int c = reader.read(timeout); if (c == EOF) { return EOF;
/** * Read a codepoint from the terminal. * * @return the character, or -1 if an EOF is received. */ public int readCharacter() { if (!pushBackChar.isEmpty()) { return pushBackChar.pop(); } try { int c = NonBlockingReader.READ_EXPIRED; int s = 0; while (c == NonBlockingReader.READ_EXPIRED) { c = reader.read(100L); if (c >= 0 && Character.isHighSurrogate((char) c)) { s = c; c = NonBlockingReader.READ_EXPIRED; } } return s != 0 ? Character.toCodePoint((char) s, (char) c) : c; } catch (ClosedException e) { throw new EndOfFileException(e); } catch (IOException e) { throw new IOError(e); } }
int start = 0; while (true) { int c = terminal.reader().read(); if (c < 0) { return null;
@Override public Object execute() throws Exception { if (arguments == null || arguments.length() == 0) { System.err.println("Argument expected"); } else { WatchTask watchTask = new WatchTask(); executorService.scheduleAtFixedRate(watchTask, 0, interval, TimeUnit.SECONDS); try { Terminal terminal = (Terminal) session.get(".jline.terminal"); Terminal.SignalHandler prev = terminal.handle(Terminal.Signal.INT, this::abort); Attributes attr = terminal.enterRawMode(); try { reading = Thread.currentThread(); while (terminal.reader().read(1) == NonBlockingReader.READ_EXPIRED); } finally { reading = null; terminal.setAttributes(attr); terminal.handle(Terminal.Signal.INT, prev); } } catch (InterruptedIOException e) { // Ignore } finally { abort = true; executorService.shutdownNow(); } } return null; }
@Override public Object execute() throws Exception { if (arguments == null || arguments.length() == 0) { System.err.println("Argument expected"); } else { WatchTask watchTask = new WatchTask(); executorService.scheduleAtFixedRate(watchTask, 0, interval, TimeUnit.SECONDS); try { Terminal terminal = (Terminal) session.get(".jline.terminal"); Terminal.SignalHandler prev = terminal.handle(Terminal.Signal.INT, this::abort); Attributes attr = terminal.enterRawMode(); try { reading = Thread.currentThread(); while (terminal.reader().read(1) == NonBlockingReader.READ_EXPIRED); } finally { reading = null; terminal.setAttributes(attr); terminal.handle(Terminal.Signal.INT, prev); } } catch (InterruptedIOException e) { // Ignore } finally { abort = true; executorService.shutdownNow(); } } return null; }
private void sillyLess(InputStream in) throws IOException { BufferedReader breader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); String man; int index = 0; while ((man = breader.readLine()) != null) { index++; sqlLine.output(man); // silly little pager if (index % (sqlLine.getOpts().getMaxHeight() - 1) == 0) { String prompt = sqlLine.loc("enter-for-more"); sqlLine.getLineReader().getTerminal().writer().write(prompt); int c; // The logic to prevent reaction of SqlLineParser here do { c = sqlLine.getLineReader().getTerminal().reader().read(100); } while (c != -1 && c != 13 && c != 'q'); if (c == -1 || c == 'q') { sqlLine.getLineReader().getTerminal().writer().write('\n'); break; } } } breader.close(); }
private static int readExt(Terminal terminal) { try { // The coordinates are encoded in UTF-8, so if that's not the input encoding, // we need to get around int c; if (terminal.encoding() != StandardCharsets.UTF_8) { c = new InputStreamReader(terminal.input(), StandardCharsets.UTF_8).read(); } else { c = terminal.reader().read(); } if (c < 0) { throw new EOFException(); } return c; } catch (IOException e) { throw new IOError(e); } }
executorService.scheduleAtFixedRate(task, 0, interval, TimeUnit.SECONDS); Attributes attr = terminal.enterRawMode(); terminal.reader().read(); terminal.setAttributes(attr); } finally {