|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.json; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.UncheckedIOException; |
| 22 | +import java.nio.CharBuffer; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +/** |
| 26 | + * Similar to a {@link Readable} but with the ability to peek a single character ahead. |
| 27 | + * <p> |
| 28 | + * For the sake of providing a useful {@link #toString()} implementation, keeps a small circular |
| 29 | + * buffer of the most recently read characters. |
| 30 | + */ |
| 31 | +class Input { |
| 32 | + |
| 33 | + public static final char EOF = (char) -1; |
| 34 | + private final Readable source; |
| 35 | + private boolean read; |
| 36 | + private char peekedChar; |
| 37 | + private char[] lastRead = new char[128]; |
| 38 | + private int insertAt = 0; |
| 39 | + private boolean filled = false; |
| 40 | + |
| 41 | + public Input(Readable source) { |
| 42 | + this.source = Objects.requireNonNull(source); |
| 43 | + } |
| 44 | + |
| 45 | + public char peek() { |
| 46 | + init(); |
| 47 | + return peekedChar; |
| 48 | + } |
| 49 | + |
| 50 | + public char read() { |
| 51 | + init(); |
| 52 | + read = false; |
| 53 | + append(); |
| 54 | + return peekedChar; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String toString() { |
| 59 | + String preamble = "Last " + (filled ? lastRead.length : insertAt) + " characters read: "; |
| 60 | + if (!filled) { |
| 61 | + return preamble + new String(lastRead, 0, insertAt); |
| 62 | + } |
| 63 | + |
| 64 | + // We filled the array. The insertion point would overwrite the first thing we should read. |
| 65 | + char[] buf = new char[lastRead.length]; |
| 66 | + int lengthToRead = lastRead.length - insertAt; |
| 67 | + System.arraycopy(lastRead, insertAt, buf, 0, lengthToRead); |
| 68 | + System.arraycopy(lastRead, 0, buf, lengthToRead, insertAt); |
| 69 | + |
| 70 | + return preamble + new String(buf); |
| 71 | + } |
| 72 | + |
| 73 | + private void init() { |
| 74 | + if (read) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + CharBuffer buf = CharBuffer.allocate(1); |
| 79 | + int charsRead; |
| 80 | + try { |
| 81 | + charsRead = source.read(buf); |
| 82 | + } catch (IOException e) { |
| 83 | + throw new UncheckedIOException(e.getMessage(), e); |
| 84 | + } |
| 85 | + if (charsRead != 1) { |
| 86 | + peekedChar = EOF; |
| 87 | + } else { |
| 88 | + peekedChar = buf.array()[0]; |
| 89 | + } |
| 90 | + read = true; |
| 91 | + } |
| 92 | + |
| 93 | + private void append() { |
| 94 | + if (peekedChar == Input.EOF) { |
| 95 | + return; |
| 96 | + } |
| 97 | + lastRead[insertAt] = peekedChar; |
| 98 | + insertAt = ++insertAt % lastRead.length; |
| 99 | + if (insertAt == 0) { |
| 100 | + filled = true; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments