| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import java.io.Closeable; |
| 20 |
import java.io.FileInputStream; |
| 21 |
import java.io.IOException; |
| 22 |
import java.io.InputStream; |
| 23 |
import java.net.ProtocolException; |
| 24 |
import java.nio.charset.StandardCharsets; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class ProcFileReader implements Closeable { |
| 36 |
private final InputStream mStream; |
| 37 |
private final byte[] mBuffer; |
| 38 |
|
| 39 |
|
| 40 |
private int mTail; |
| 41 |
|
| 42 |
private boolean mLineFinished; |
| 43 |
|
| 44 |
public ProcFileReader(InputStream stream) throws IOException { |
| 45 |
this(stream, 4096); |
| 46 |
} |
| 47 |
|
| 48 |
public ProcFileReader(InputStream stream, int bufferSize) throws IOException { |
| 49 |
mStream = stream; |
| 50 |
mBuffer = new byte[bufferSize]; |
| 51 |
if (stream.markSupported()) { |
| 52 |
mStream.mark(0); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
fillBuf(); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private int fillBuf() throws IOException { |
| 63 |
final int length = mBuffer.length - mTail; |
| 64 |
if (length == 0) { |
| 65 |
throw new IOException("attempting to fill already-full buffer"); |
| 66 |
} |
| 67 |
|
| 68 |
final int read = mStream.read(mBuffer, mTail, length); |
| 69 |
if (read != -1) { |
| 70 |
mTail += read; |
| 71 |
} |
| 72 |
return read; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
private void consumeBuf(int count) throws IOException { |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
while (count < mTail && mBuffer[count] == ' ') { |
| 85 |
count++; |
| 86 |
} |
| 87 |
System.arraycopy(mBuffer, count, mBuffer, 0, mTail - count); |
| 88 |
mTail -= count; |
| 89 |
if (mTail == 0) { |
| 90 |
fillBuf(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
private int nextTokenIndex() throws IOException { |
| 102 |
if (mLineFinished) { |
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
int i = 0; |
| 107 |
do { |
| 108 |
|
| 109 |
for (; i < mTail; i++) { |
| 110 |
final byte b = mBuffer[i]; |
| 111 |
if (b == '\n') { |
| 112 |
mLineFinished = true; |
| 113 |
return i; |
| 114 |
} |
| 115 |
if (b == ' ') { |
| 116 |
return i; |
| 117 |
} |
| 118 |
} |
| 119 |
} while (fillBuf() > 0); |
| 120 |
|
| 121 |
throw new ProtocolException("End of stream while looking for token boundary"); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public boolean hasMoreData() { |
| 128 |
return mTail > 0; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public void finishLine() throws IOException { |
| 135 |
|
| 136 |
if (mLineFinished) { |
| 137 |
mLineFinished = false; |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
int i = 0; |
| 142 |
do { |
| 143 |
|
| 144 |
for (; i < mTail; i++) { |
| 145 |
if (mBuffer[i] == '\n') { |
| 146 |
consumeBuf(i + 1); |
| 147 |
return; |
| 148 |
} |
| 149 |
} |
| 150 |
} while (fillBuf() > 0); |
| 151 |
|
| 152 |
throw new ProtocolException("End of stream while looking for line boundary"); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public String nextString() throws IOException { |
| 159 |
final int tokenIndex = nextTokenIndex(); |
| 160 |
if (tokenIndex == -1) { |
| 161 |
throw new ProtocolException("Missing required string"); |
| 162 |
} else { |
| 163 |
return parseAndConsumeString(tokenIndex); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public long nextLong() throws IOException { |
| 171 |
return nextLong(false); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public long nextLong(boolean stopAtInvalid) throws IOException { |
| 178 |
final int tokenIndex = nextTokenIndex(); |
| 179 |
if (tokenIndex == -1) { |
| 180 |
throw new ProtocolException("Missing required long"); |
| 181 |
} else { |
| 182 |
return parseAndConsumeLong(tokenIndex, stopAtInvalid); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public long nextOptionalLong(long def) throws IOException { |
| 191 |
final int tokenIndex = nextTokenIndex(); |
| 192 |
if (tokenIndex == -1) { |
| 193 |
return def; |
| 194 |
} else { |
| 195 |
return parseAndConsumeLong(tokenIndex, false); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
private String parseAndConsumeString(int tokenIndex) throws IOException { |
| 200 |
final String s = new String(mBuffer, 0, tokenIndex, StandardCharsets.US_ASCII); |
| 201 |
consumeBuf(tokenIndex + 1); |
| 202 |
return s; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
private long parseAndConsumeLong(int tokenIndex, boolean stopAtInvalid) throws IOException { |
| 209 |
final boolean negative = mBuffer[0] == '-'; |
| 210 |
|
| 211 |
|
| 212 |
long result = 0; |
| 213 |
for (int i = negative ? 1 : 0; i < tokenIndex; i++) { |
| 214 |
final int digit = mBuffer[i] - '0'; |
| 215 |
if (digit < 0 || digit > 9) { |
| 216 |
if (stopAtInvalid) { |
| 217 |
break; |
| 218 |
} else { |
| 219 |
throw invalidLong(tokenIndex); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
final long next = result * 10 - digit; |
| 226 |
if (next > result) { |
| 227 |
throw invalidLong(tokenIndex); |
| 228 |
} |
| 229 |
result = next; |
| 230 |
} |
| 231 |
|
| 232 |
consumeBuf(tokenIndex + 1); |
| 233 |
return negative ? result : -result; |
| 234 |
} |
| 235 |
|
| 236 |
private NumberFormatException invalidLong(int tokenIndex) { |
| 237 |
return new NumberFormatException( |
| 238 |
"invalid long: " + new String(mBuffer, 0, tokenIndex, StandardCharsets.US_ASCII)); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public int nextInt() throws IOException { |
| 245 |
final long value = nextLong(); |
| 246 |
if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) { |
| 247 |
throw new NumberFormatException("parsed value larger than integer"); |
| 248 |
} |
| 249 |
return (int) value; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public void nextIgnored() throws IOException { |
| 256 |
final int tokenIndex = nextTokenIndex(); |
| 257 |
if (tokenIndex == -1) { |
| 258 |
throw new ProtocolException("Missing required token"); |
| 259 |
} else { |
| 260 |
consumeBuf(tokenIndex + 1); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public void rewind() throws IOException { |
| 269 |
if (mStream instanceof FileInputStream) { |
| 270 |
((FileInputStream) mStream).getChannel().position(0); |
| 271 |
} else if (mStream.markSupported()) { |
| 272 |
mStream.reset(); |
| 273 |
} else { |
| 274 |
throw new IOException("The InputStream is NOT markable"); |
| 275 |
} |
| 276 |
|
| 277 |
mTail = 0; |
| 278 |
mLineFinished = false; |
| 279 |
fillBuf(); |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public void close() throws IOException { |
| 284 |
mStream.close(); |
| 285 |
} |
| 286 |
} |