本文整理了Java中okio.BufferedSource.select()
方法的一些代码示例,展示了BufferedSource.select()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedSource.select()
方法的具体详情如下:
包路径:okio.BufferedSource
类名称:BufferedSource
方法名:select
暂无
代码示例来源:origin: square/okhttp
public static Charset bomAwareCharset(BufferedSource source, Charset charset) throws IOException {
switch (source.select(UNICODE_BOMS)) {
case 0: return UTF_8;
case 1: return UTF_16BE;
case 2: return UTF_16LE;
case 3: return UTF_32BE;
case 4: return UTF_32LE;
case -1: return charset;
default: throw new AssertionError();
}
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
public static Charset bomAwareCharset(BufferedSource source, Charset charset) throws IOException {
switch (source.select(UNICODE_BOMS)) {
case 0: return UTF_8;
case 1: return UTF_16BE;
case 2: return UTF_16LE;
case 3: return UTF_32BE;
case 4: return UTF_32LE;
case -1: return charset;
default: throw new AssertionError();
}
}
代码示例来源:origin: square/moshi
@Override public int selectString(Options options) throws IOException {
int p = peeked;
if (p == PEEKED_NONE) {
p = doPeek();
}
if (p < PEEKED_SINGLE_QUOTED || p > PEEKED_BUFFERED) {
return -1;
}
if (p == PEEKED_BUFFERED) {
return findString(peekedString, options);
}
int result = source.select(options.doubleQuoteSuffix);
if (result != -1) {
peeked = PEEKED_NONE;
pathIndices[stackSize - 1]++;
return result;
}
String nextString = nextString();
result = findString(nextString, options);
if (result == -1) {
peeked = PEEKED_BUFFERED;
peekedString = nextString;
pathIndices[stackSize - 1]--;
}
return result;
}
代码示例来源:origin: square/moshi
@Override public int selectName(Options options) throws IOException {
int p = peeked;
if (p == PEEKED_NONE) {
p = doPeek();
}
if (p < PEEKED_SINGLE_QUOTED_NAME || p > PEEKED_BUFFERED_NAME) {
return -1;
}
if (p == PEEKED_BUFFERED_NAME) {
return findName(peekedString, options);
}
int result = source.select(options.doubleQuoteSuffix);
if (result != -1) {
peeked = PEEKED_NONE;
pathNames[stackSize - 1] = options.strings[result];
return result;
}
// The next name may be unnecessary escaped. Save the last recorded path name, so that we
// can restore the peek state in case we fail to find a match.
String lastPathName = pathNames[stackSize - 1];
String nextName = nextName();
result = findName(nextName, options);
if (result == -1) {
peeked = PEEKED_BUFFERED_NAME;
peekedString = nextName;
// We can't push the path further, make it seem like nothing happened.
pathNames[stackSize - 1] = lastPathName;
}
return result;
}
代码示例来源:origin: square/okio
@Test public void selectNoByteStringsFromEmptySource() throws IOException {
Options options = Options.of();
assertEquals(-1, source.select(options));
}
代码示例来源:origin: square/okio
@Test public void selectValuesHaveCommonPrefix() throws IOException {
Options options = Options.Companion.of(
ByteString.encodeUtf8("abcd"),
ByteString.encodeUtf8("abce"),
ByteString.encodeUtf8("abcc"));
sink.writeUtf8("abcc").writeUtf8("abcd").writeUtf8("abce");
sink.emit();
assertEquals(2, source.select(options));
assertEquals(0, source.select(options));
assertEquals(1, source.select(options));
}
代码示例来源:origin: square/okio
@Test public void selectFromEmptySource() throws IOException {
Options options = Options.Companion.of(
ByteString.encodeUtf8("abc"),
ByteString.encodeUtf8("def"));
assertEquals(-1, source.select(options));
}
代码示例来源:origin: square/okio
@Test public void selectSpanningMultipleSegments() throws IOException {
ByteString commonPrefix = TestUtil.randomBytes(SEGMENT_SIZE + 10);
ByteString a = new Buffer().write(commonPrefix).writeUtf8("a").readByteString();
ByteString bc = new Buffer().write(commonPrefix).writeUtf8("bc").readByteString();
ByteString bd = new Buffer().write(commonPrefix).writeUtf8("bd").readByteString();
Options options = Options.Companion.of(a, bc, bd);
sink.write(bd);
sink.write(a);
sink.write(bc);
sink.emit();
assertEquals(2, source.select(options));
assertEquals(0, source.select(options));
assertEquals(1, source.select(options));
assertTrue(source.exhausted());
}
代码示例来源:origin: square/okio
@Test public void select() throws IOException {
Options options = Options.Companion.of(
ByteString.encodeUtf8("ROCK"),
ByteString.encodeUtf8("SCISSORS"),
ByteString.encodeUtf8("PAPER"));
sink.writeUtf8("PAPER,SCISSORS,ROCK");
sink.emit();
assertEquals(2, source.select(options));
assertEquals(',', source.readByte());
assertEquals(1, source.select(options));
assertEquals(',', source.readByte());
assertEquals(0, source.select(options));
assertTrue(source.exhausted());
}
代码示例来源:origin: square/okio
@Test public void selectLongerThanSource() throws IOException {
Options options = Options.Companion.of(
ByteString.encodeUtf8("abcd"),
ByteString.encodeUtf8("abce"),
ByteString.encodeUtf8("abcc"));
sink.writeUtf8("abc");
sink.emit();
assertEquals(-1, source.select(options));
assertEquals("abc", source.readUtf8());
}
代码示例来源:origin: square/okio
@Test public void selectNotFound() throws IOException {
Options options = Options.Companion.of(
ByteString.encodeUtf8("ROCK"),
ByteString.encodeUtf8("SCISSORS"),
ByteString.encodeUtf8("PAPER"));
sink.writeUtf8("SPOCK");
sink.emit();
assertEquals(-1, source.select(options));
assertEquals("SPOCK", source.readUtf8());
}
代码示例来源:origin: square/okio
@Test public void selectReturnsFirstByteStringThatMatches() throws IOException {
Options options = Options.Companion.of(
ByteString.encodeUtf8("abcd"),
ByteString.encodeUtf8("abc"),
ByteString.encodeUtf8("abcde"));
sink.writeUtf8("abcdef");
sink.emit();
assertEquals(0, source.select(options));
assertEquals("ef", source.readUtf8());
}
代码示例来源:origin: com.squareup.moshi/moshi
@Override public int selectString(Options options) throws IOException {
int p = peeked;
if (p == PEEKED_NONE) {
p = doPeek();
}
if (p < PEEKED_SINGLE_QUOTED || p > PEEKED_BUFFERED) {
return -1;
}
if (p == PEEKED_BUFFERED) {
return findString(peekedString, options);
}
int result = source.select(options.doubleQuoteSuffix);
if (result != -1) {
peeked = PEEKED_NONE;
pathIndices[stackSize - 1]++;
return result;
}
String nextString = nextString();
result = findString(nextString, options);
if (result == -1) {
peeked = PEEKED_BUFFERED;
peekedString = nextString;
pathIndices[stackSize - 1]--;
}
return result;
}
代码示例来源:origin: com.squareup.moshi/moshi
@Override public int selectName(Options options) throws IOException {
int p = peeked;
if (p == PEEKED_NONE) {
p = doPeek();
}
if (p < PEEKED_SINGLE_QUOTED_NAME || p > PEEKED_BUFFERED_NAME) {
return -1;
}
if (p == PEEKED_BUFFERED_NAME) {
return findName(peekedString, options);
}
int result = source.select(options.doubleQuoteSuffix);
if (result != -1) {
peeked = PEEKED_NONE;
pathNames[stackSize - 1] = options.strings[result];
return result;
}
// The next name may be unnecessary escaped. Save the last recorded path name, so that we
// can restore the peek state in case we fail to find a match.
String lastPathName = pathNames[stackSize - 1];
String nextName = nextName();
result = findName(nextName, options);
if (result == -1) {
peeked = PEEKED_BUFFERED_NAME;
peekedString = nextName;
// We can't push the path further, make it seem like nothing happened.
pathNames[stackSize - 1] = lastPathName;
}
return result;
}
内容来源于网络,如有侵权,请联系作者删除!