mirror of
https://github.com/square/okhttp.git
synced 2026-01-27 04:22:07 +03:00
Original AOSP/libcore commit from Vladimir Marko: Add StrictLineReader for efficient reading of lines consistent with Streams.readAsciiLine(). Use this to improve DiskLruCache.readJournal() and initialization of HttpResponseCache$Entry from InputStream. (cherry-pick of e03b551079aae1204e505f1dc24f2b986ef82ec0.) Bug: 6739304 Change-Id: If3083031f1368a9bbbd405c91553d7a205fd4e39
83 lines
3.3 KiB
Java
83 lines
3.3 KiB
Java
/*
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
*
|
|
* Licensed 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 libcore.io;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.EOFException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import junit.framework.TestCase;
|
|
import libcore.util.Charsets;
|
|
|
|
public class StrictLineReaderTest extends TestCase {
|
|
|
|
public void testLineReaderConsistencyWithReadAsciiLine () {
|
|
try {
|
|
// Testing with LineReader buffer capacity 32 to check some corner cases.
|
|
StrictLineReader lineReader = new StrictLineReader(createTestInputStream(), 32,
|
|
Charsets.US_ASCII);
|
|
InputStream refStream = createTestInputStream();
|
|
while (true) {
|
|
try {
|
|
String refLine = Streams.readAsciiLine(refStream);
|
|
try {
|
|
String line = lineReader.readLine();
|
|
if (!refLine.equals(line)) {
|
|
fail("line (\""+line+"\") differs from expected (\""+refLine+"\").");
|
|
}
|
|
} catch (EOFException eof) {
|
|
fail("line reader threw EOFException too early.");
|
|
}
|
|
} catch (EOFException refEof) {
|
|
try {
|
|
lineReader.readLine();
|
|
fail("line reader didn't throw the expected EOFException.");
|
|
} catch (EOFException eof) {
|
|
// OK
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
refStream.close();
|
|
lineReader.close();
|
|
} catch (IOException ioe) {
|
|
fail("Unexpected IOException " + ioe.toString());
|
|
}
|
|
}
|
|
|
|
private InputStream createTestInputStream() {
|
|
return new ByteArrayInputStream((
|
|
/* each source lines below should represent 32 bytes, until the next comment */
|
|
"12 byte line\n18 byte line......\n" +
|
|
"pad\nline spanning two 32-byte bu" +
|
|
"ffers\npad......................\n" +
|
|
"pad\nline spanning three 32-byte " +
|
|
"buffers and ending with LF at th" +
|
|
"e end of a 32 byte buffer......\n" +
|
|
"pad\nLine ending with CRLF split" +
|
|
" at the end of a 32-byte buffer\r" +
|
|
"\npad...........................\n" +
|
|
/* end of 32-byte lines */
|
|
"line ending with CRLF\r\n" +
|
|
"this is a long line with embedded CR \r ending with CRLF and having more than " +
|
|
"32 characters\r\n" +
|
|
"unterminated line - should be dropped"
|
|
).getBytes());
|
|
}
|
|
}
|
|
|