okio.BufferedSource.readUtf8LineStrict()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(703)

本文整理了Java中okio.BufferedSource.readUtf8LineStrict()方法的一些代码示例,展示了BufferedSource.readUtf8LineStrict()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedSource.readUtf8LineStrict()方法的具体详情如下:
包路径:okio.BufferedSource
类名称:BufferedSource
方法名:readUtf8LineStrict

BufferedSource.readUtf8LineStrict介绍

[英]Removes and returns characters up to but not including the next line break. A line break is either "\n" or "\r\n"; these characters are not included in the result.

On the end of the stream this method throws. Every call must consume either '\r\n' or '\n'. If these characters are absent in the stream, an java.io.EOFException is thrown. Use this for machine-generated data where a missing line break implies truncated input.
[中]删除并返回最多到但不包括下一个换行符的字符。换行符为“\n”或“\r\n”;这些字符不包括在结果中。
在流的末尾,此方法抛出。每个调用必须使用“\r\n”或“\n”。如果流中缺少这些字符,则会生成java。木卫一。抛出EOFEException。对于缺少换行符意味着输入被截断的机器生成的数据,使用此选项。

代码示例

代码示例来源:origin: square/okhttp

private String readHeaderLine() throws IOException {
 String line = source.readUtf8LineStrict(headerLimit);
 headerLimit -= line.length();
 return line;
}

代码示例来源:origin: square/okhttp

private void readEmptyLine(BufferedSource source) throws IOException {
 String line = source.readUtf8LineStrict();
 if (line.length() != 0) throw new IllegalStateException("Expected empty but was: " + line);
}

代码示例来源:origin: square/okhttp

static int readInt(BufferedSource source) throws IOException {
 try {
  long result = source.readDecimalLong();
  String line = source.readUtf8LineStrict();
  if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) {
   throw new IOException("expected an int but was \"" + result + line + "\"");
  }
  return (int) result;
 } catch (NumberFormatException e) {
  throw new IOException(e.getMessage());
 }
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

private String readHeaderLine() throws IOException {
 String line = source.readUtf8LineStrict(headerLimit);
 headerLimit -= line.length();
 return line;
}

代码示例来源:origin: square/okhttp

@Override public boolean hasNext() {
 if (nextUrl != null) return true;
 canRemove = false; // Prevent delegate.remove() on the wrong item!
 while (delegate.hasNext()) {
  try (DiskLruCache.Snapshot snapshot = delegate.next()) {
   BufferedSource metadata = Okio.buffer(snapshot.getSource(ENTRY_METADATA));
   nextUrl = metadata.readUtf8LineStrict();
   return true;
  } catch (IOException ignored) {
   // We couldn't read the metadata for this snapshot; possibly because the host filesystem
   // has disappeared! Skip it.
  }
 }
 return false;
}

代码示例来源:origin: square/okhttp

private void readJournal() throws IOException {
 try (BufferedSource source = Okio.buffer(fileSystem.source(journalFile))) {
  String magic = source.readUtf8LineStrict();
  String version = source.readUtf8LineStrict();
  String appVersionString = source.readUtf8LineStrict();
  String valueCountString = source.readUtf8LineStrict();
  String blank = source.readUtf8LineStrict();
  if (!MAGIC.equals(magic)
    || !VERSION_1.equals(version)
  while (true) {
   try {
    readJournalLine(source.readUtf8LineStrict());
    lineCount++;
   } catch (EOFException endOfJournal) {

代码示例来源:origin: com.squareup.okhttp3/okhttp

static int readInt(BufferedSource source) throws IOException {
 try {
  long result = source.readDecimalLong();
  String line = source.readUtf8LineStrict();
  if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) {
   throw new IOException("expected an int but was \"" + result + line + "\"");
  }
  return (int) result;
 } catch (NumberFormatException e) {
  throw new IOException(e.getMessage());
 }
}

代码示例来源:origin: square/okhttp

private List<Certificate> readCertificateList(BufferedSource source) throws IOException {
 int length = readInt(source);
 if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null.
 try {
  CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
  List<Certificate> result = new ArrayList<>(length);
  for (int i = 0; i < length; i++) {
   String line = source.readUtf8LineStrict();
   Buffer bytes = new Buffer();
   bytes.write(ByteString.decodeBase64(line));
   result.add(certificateFactory.generateCertificate(bytes.inputStream()));
  }
  return result;
 } catch (CertificateException e) {
  throw new IOException(e.getMessage());
 }
}

代码示例来源:origin: apollographql/apollo-android

private static int readInt(BufferedSource source) throws IOException {
 try {
  long result = source.readDecimalLong();
  String line = source.readUtf8LineStrict();
  if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) {
   throw new IOException("expected an int but was \"" + result + line + "\"");
  }
  return (int) result;
 } catch (NumberFormatException e) {
  throw new IOException(e.getMessage());
 }
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

@Override public boolean hasNext() {
 if (nextUrl != null) return true;
 canRemove = false; // Prevent delegate.remove() on the wrong item!
 while (delegate.hasNext()) {
  try (DiskLruCache.Snapshot snapshot = delegate.next()) {
   BufferedSource metadata = Okio.buffer(snapshot.getSource(ENTRY_METADATA));
   nextUrl = metadata.readUtf8LineStrict();
   return true;
  } catch (IOException ignored) {
   // We couldn't read the metadata for this snapshot; possibly because the host filesystem
   // has disappeared! Skip it.
  }
 }
 return false;
}

代码示例来源:origin: square/okhttp

private void readChunkSize() throws IOException {
 // Read the suffix of the previous chunk.
 if (bytesRemainingInChunk != NO_CHUNK_YET) {
  source.readUtf8LineStrict();
 }
 try {
  bytesRemainingInChunk = source.readHexadecimalUnsignedLong();
  String extensions = source.readUtf8LineStrict().trim();
  if (bytesRemainingInChunk < 0 || (!extensions.isEmpty() && !extensions.startsWith(";"))) {
   throw new ProtocolException("expected chunk size and optional extensions but was \""
     + bytesRemainingInChunk + extensions + "\"");
  }
 } catch (NumberFormatException e) {
  throw new ProtocolException(e.getMessage());
 }
 if (bytesRemainingInChunk == 0L) {
  hasMoreChunks = false;
  trailers = readHeaders();
  HttpHeaders.receiveHeaders(client.cookieJar(), url, trailers);
  endOfInput(true, null);
 }
}

代码示例来源:origin: square/okhttp

String request;
try {
 request = source.readUtf8LineStrict();
} catch (IOException streamIsClosed) {
 return null; // no request because we closed the stream
boolean expectContinue = false;
String header;
while ((header = source.readUtf8LineStrict()).length() != 0) {
 Internal.instance.addLenient(headers, header);
 String lowercaseHeader = header.toLowerCase(Locale.US);
 hasBody = true;
 while (true) {
  int chunkSize = Integer.parseInt(source.readUtf8LineStrict().trim(), 16);
  if (chunkSize == 0) {
   readEmptyLine(source);

代码示例来源:origin: square/okio

@Test public void crDroppedPrecedingLf() throws IOException {
 data.writeUtf8("abc\r\ndef\r\nghi\rjkl\r\n");
 assertEquals("abc", source.readUtf8LineStrict());
 assertEquals("def", source.readUtf8LineStrict());
 assertEquals("ghi\rjkl", source.readUtf8LineStrict());
}

代码示例来源:origin: square/okio

@Test public void readLines() throws IOException {
 data.writeUtf8("abc\ndef\n");
 assertEquals("abc", source.readUtf8LineStrict());
 assertEquals("def", source.readUtf8LineStrict());
 try {
  source.readUtf8LineStrict();
  fail();
 } catch (EOFException expected) {
  assertEquals("\\n not found: limit=0 content=…", expected.getMessage());
 }
}

代码示例来源:origin: square/okio

@Test public void emptyLines() throws IOException {
 data.writeUtf8("\n\n\n");
 assertEquals("", source.readUtf8LineStrict());
 assertEquals("", source.readUtf8LineStrict());
 assertEquals("", source.readUtf8LineStrict());
 assertTrue(source.exhausted());
}

代码示例来源:origin: square/okio

@Test public void readUtf8LineStrictNoBytesConsumedOnFailure() throws IOException {
 data.writeUtf8("abc\n");
 try {
  source.readUtf8LineStrict(2);
  fail();
 } catch (EOFException expected) {
  assertTrue(expected.getMessage().startsWith("\\n not found: limit=2 content=61626"));
 }
 assertEquals("abc", source.readUtf8LineStrict(3));
}

代码示例来源:origin: square/okio

@Test public void readUtf8LineStrictNonPositive() throws IOException {
 data.writeUtf8("\r\n");
 try {
  source.readUtf8LineStrict(-1);
  fail("Expected failure: limit must be greater than 0");
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: square/okio

@Test public void readUtf8LineStrictEmptyString() throws IOException {
 data.writeUtf8("\r\nabc");
 assertEquals("", source.readUtf8LineStrict(0));
 assertEquals("abc", source.readUtf8());
}

代码示例来源:origin: square/okio

@Test public void eofExceptionProvidesLimitedContent() throws IOException {
 data.writeUtf8("aaaaaaaabbbbbbbbccccccccdddddddde");
 try {
  source.readUtf8LineStrict();
  fail();
 } catch (EOFException expected) {
  assertEquals("\\n not found: limit=33 content=616161616161616162626262626262626363636363636363"
    + "6464646464646464…", expected.getMessage());
 }
}

代码示例来源:origin: square/okio

@Test public void readUtf8LineStrictWithLimits() throws IOException {
 int[] lens = {1, SEGMENT_SIZE - 2, SEGMENT_SIZE - 1, SEGMENT_SIZE, SEGMENT_SIZE * 10};
 for (int len : lens) {
  data.writeUtf8(TestUtil.repeat('a', len)).writeUtf8("\n");
  assertEquals(len, source.readUtf8LineStrict(len).length());
  source.readUtf8();
  data.writeUtf8(TestUtil.repeat('a', len)).writeUtf8("\n").writeUtf8(TestUtil.repeat('a', len));
  assertEquals(len, source.readUtf8LineStrict(len).length());
  source.readUtf8();
  data.writeUtf8(TestUtil.repeat('a', len)).writeUtf8("\r\n");
  assertEquals(len, source.readUtf8LineStrict(len).length());
  source.readUtf8();
  data.writeUtf8(TestUtil.repeat('a', len)).writeUtf8("\r\n").writeUtf8(TestUtil.repeat('a', len));
  assertEquals(len, source.readUtf8LineStrict(len).length());
  source.readUtf8();
 }
}

相关文章

微信公众号

最新文章

更多