forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInput.java
More file actions
161 lines (139 loc) · 4.78 KB
/
Copy pathInput.java
File metadata and controls
161 lines (139 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.json;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import org.openqa.selenium.internal.Require;
/**
* Similar to a {@link Reader} but with the ability to peek a single character ahead.
*
* <p>For the sake of providing a useful {@link #toString()} implementation, keeps the most recently
* read characters in the input buffer.
*/
class Input {
/**
* End-of-input sentinel returned by {@link #peek()} and {@link #read()}.
*
* <p>Value {@code -1} mirrors {@link java.io.Reader#read()} and — unlike a {@code char} sentinel
* — cannot collide with any valid UTF-16 code unit (including U+FFFF).
*/
public static final int EOF = -1;
/** the number of chars to buffer */
private static final int BUFFER_SIZE = 4096;
/** the number of chars to remember, safe to set to 0 */
private static final int MEMORY_SIZE = 128;
private final Reader source;
/** a buffer used to minimize read calls and to keep the chars to remember */
private final char[] buffer;
/** the filled area in the buffer */
private int filled;
/** the last position read in the buffer */
private int position;
/**
* Initialize a new instance of the {@link Input} class with the specified source.
*
* @param source {@link Reader} object that supplies the input to be processed
*/
public Input(Reader source) {
this.source = Require.nonNull("Source", source);
this.buffer = new char[BUFFER_SIZE + MEMORY_SIZE];
this.filled = 0;
this.position = -1;
}
/**
* Extract the next character from the input without consuming it.
*
* @return the next input character as an unsigned UTF-16 code unit (0-65535); {@link #EOF} if
* input is exhausted
*/
public int peek() {
return fill() ? buffer[position + 1] : EOF;
}
/**
* Read and consume the next character from the input.
*
* @return the next input character as an unsigned UTF-16 code unit (0-65535); {@link #EOF} if
* input is exhausted
*/
public int read() {
return fill() ? buffer[++position] : EOF;
}
/**
* Return a string containing the most recently consumed input characters.
*
* @return {@link String} with up to 128 consumed input characters
*/
@Override
public String toString() {
int offset;
int length;
if (position < MEMORY_SIZE) {
offset = 0;
length = position + 1;
} else {
offset = position + 1 - MEMORY_SIZE;
length = MEMORY_SIZE;
}
String last = "Last " + length + " characters read: " + new String(buffer, offset, length);
int next = Math.min(MEMORY_SIZE, filled - (offset + length));
if (next > 0) {
if (next > 128) {
next = 128;
}
return last
+ ", next "
+ next
+ " characters to read: "
+ new String(buffer, offset + length, next);
}
return last;
}
/**
* If all buffered input has been consumed, read the next chunk into the buffer.<br>
* <b>NOTE</b>: The last 128 character of consumed input is retained for debug output.
*
* @return {@code true} if new input is available; {@code false} if input is exhausted
* @throws UncheckedIOException if an I/O exception is encountered
*/
private boolean fill() {
// do we need to fill the buffer?
while (filled == position + 1) {
try {
// free the buffer, keep only the chars to remember
int shift = filled - MEMORY_SIZE;
if (shift > 0) {
position -= shift;
filled -= shift;
System.arraycopy(buffer, shift, buffer, 0, filled);
}
// try to fill the buffer
int n = source.read(buffer, filled, buffer.length - filled);
if (n == -1) {
// EOF reached
return false;
} else {
// n might be 0, the outer loop will handle this
filled += n;
}
} catch (IOException e) {
throw new UncheckedIOException(e.getMessage(), e);
}
}
return true;
}
}