本文整理了Java中okio.BufferedSource.readByteString()
方法的一些代码示例,展示了BufferedSource.readByteString()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedSource.readByteString()
方法的具体详情如下:
包路径:okio.BufferedSource
类名称:BufferedSource
方法名:readByteString
[英]Removes all bytes bytes from this and returns them as a byte string.
[中]从中删除所有字节并将其作为字节字符串返回。
代码示例来源:origin: square/okhttp
@Override public void onResponse(Call call, Response response) throws IOException {
try (ResponseBody body = response.body()) {
// Consume and discard the response body.
body.source().readByteString();
}
}
});
代码示例来源:origin: square/okhttp
@Override public void onResponse(Call call, Response response) throws IOException {
try (ResponseBody body = response.body()) {
// Consume and discard the response body.
body.source().readByteString();
}
}
});
代码示例来源:origin: square/okhttp
public void run() throws Exception {
Request request = new Request.Builder()
.url("https://publicobject.com/helloworld.txt")
.build();
System.out.println("REQUEST 1 (new connection)");
try (Response response = client.newCall(request).execute()) {
// Consume and discard the response body.
response.body().source().readByteString();
}
System.out.println("REQUEST 2 (pooled connection)");
try (Response response = client.newCall(request).execute()) {
// Consume and discard the response body.
response.body().source().readByteString();
}
}
代码示例来源:origin: square/okio
public ByteString readByteString(File file) throws IOException {
try (BufferedSource source = Okio.buffer(Okio.source(file))) {
return source.readByteString();
}
}
代码示例来源:origin: k9mail/k-9
private static ByteString decodeQ(String encodedWord) {
/*
* Replace _ with =20
*/
StringBuilder sb = new StringBuilder();
for (int i = 0; i < encodedWord.length(); i++) {
char c = encodedWord.charAt(i);
if (c == '_') {
sb.append("=20");
} else {
sb.append(c);
}
}
byte[] bytes = sb.toString().getBytes(Charset.forName("US-ASCII"));
QuotedPrintableInputStream is = new QuotedPrintableInputStream(new ByteArrayInputStream(bytes));
try {
return Okio.buffer(Okio.source(is)).readByteString();
} catch (IOException e) {
return null;
}
}
代码示例来源:origin: square/okhttp
private List<InetAddress> readResponse(String hostname, Response response) throws Exception {
if (response.cacheResponse() == null && response.protocol() != Protocol.HTTP_2) {
Platform.get().log(Platform.WARN, "Incorrect protocol: " + response.protocol(), null);
}
try {
if (!response.isSuccessful()) {
throw new IOException("response: " + response.code() + " " + response.message());
}
ResponseBody body = response.body();
if (body.contentLength() > MAX_RESPONSE_SIZE) {
throw new IOException("response size exceeds limit ("
+ MAX_RESPONSE_SIZE
+ " bytes): "
+ body.contentLength()
+ " bytes");
}
ByteString responseBytes = body.source().readByteString();
return DnsRecordCodec.decodeAnswers(hostname, responseBytes);
} finally {
response.close();
}
}
代码示例来源:origin: square/wire
/**
* Reads a {@code bytes} field value from the stream. The length is read from the
* stream prior to the actual data.
*/
public ByteString readBytes() throws IOException {
long byteCount = beforeLengthDelimitedScalar();
source.require(byteCount); // Throws EOFException if insufficient bytes are available.
return source.readByteString(byteCount);
}
代码示例来源:origin: square/okhttp
public void readConnectionPreface(Handler handler) throws IOException {
if (client) {
// The client reads the initial SETTINGS frame.
if (!nextFrame(true, handler)) {
throw ioException("Required SETTINGS preface not received");
}
} else {
// The server reads the CONNECTION_PREFACE byte string.
ByteString connectionPreface = source.readByteString(CONNECTION_PREFACE.size());
if (logger.isLoggable(FINE)) logger.fine(format("<< CONNECTION %s", connectionPreface.hex()));
if (!CONNECTION_PREFACE.equals(connectionPreface)) {
throw ioException("Expected a connection header but was %s", connectionPreface.utf8());
}
}
}
代码示例来源:origin: square/okhttp
/** Reads a potentially Huffman encoded byte string. */
ByteString readByteString() throws IOException {
int firstByte = readByte();
boolean huffmanDecode = (firstByte & 0x80) == 0x80; // 1NNNNNNN
int length = readInt(firstByte, PREFIX_7_BITS);
if (huffmanDecode) {
return ByteString.of(Huffman.get().decode(source.readByteArray(length)));
} else {
return source.readByteString(length);
}
}
}
代码示例来源:origin: square/okhttp
private void readGoAway(Handler handler, int length, byte flags, int streamId)
throws IOException {
if (length < 8) throw ioException("TYPE_GOAWAY length < 8: %s", length);
if (streamId != 0) throw ioException("TYPE_GOAWAY streamId != 0");
int lastStreamId = source.readInt();
int errorCodeInt = source.readInt();
int opaqueDataLength = length - 8;
ErrorCode errorCode = ErrorCode.fromHttp2(errorCodeInt);
if (errorCode == null) {
throw ioException("TYPE_GOAWAY unexpected error code: %d", errorCodeInt);
}
ByteString debugData = EMPTY;
if (opaqueDataLength > 0) { // Must read debug data in order to not corrupt the connection.
debugData = source.readByteString(opaqueDataLength);
}
handler.goAway(lastStreamId, errorCode, debugData);
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
public void readConnectionPreface(Handler handler) throws IOException {
if (client) {
// The client reads the initial SETTINGS frame.
if (!nextFrame(true, handler)) {
throw ioException("Required SETTINGS preface not received");
}
} else {
// The server reads the CONNECTION_PREFACE byte string.
ByteString connectionPreface = source.readByteString(CONNECTION_PREFACE.size());
if (logger.isLoggable(FINE)) logger.fine(format("<< CONNECTION %s", connectionPreface.hex()));
if (!CONNECTION_PREFACE.equals(connectionPreface)) {
throw ioException("Expected a connection header but was %s", connectionPreface.utf8());
}
}
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
/** Reads a potentially Huffman encoded byte string. */
ByteString readByteString() throws IOException {
int firstByte = readByte();
boolean huffmanDecode = (firstByte & 0x80) == 0x80; // 1NNNNNNN
int length = readInt(firstByte, PREFIX_7_BITS);
if (huffmanDecode) {
return ByteString.of(Huffman.get().decode(source.readByteArray(length)));
} else {
return source.readByteString(length);
}
}
}
代码示例来源:origin: square/okio
@Test public void multipleSegments() throws Exception {
HashingSource hashingSource = HashingSource.sha256(source);
BufferedSource bufferedSource = Okio.buffer(hashingSource);
source.write(r32k);
assertEquals(r32k, bufferedSource.readByteString());
assertEquals(SHA256_r32k, hashingSource.hash());
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
private void readGoAway(Handler handler, int length, byte flags, int streamId)
throws IOException {
if (length < 8) throw ioException("TYPE_GOAWAY length < 8: %s", length);
if (streamId != 0) throw ioException("TYPE_GOAWAY streamId != 0");
int lastStreamId = source.readInt();
int errorCodeInt = source.readInt();
int opaqueDataLength = length - 8;
ErrorCode errorCode = ErrorCode.fromHttp2(errorCodeInt);
if (errorCode == null) {
throw ioException("TYPE_GOAWAY unexpected error code: %d", errorCodeInt);
}
ByteString debugData = EMPTY;
if (opaqueDataLength > 0) { // Must read debug data in order to not corrupt the connection.
debugData = source.readByteString(opaqueDataLength);
}
handler.goAway(lastStreamId, errorCode, debugData);
}
代码示例来源:origin: square/okio
@Test public void readByteStringTooShortThrows() throws IOException {
sink.writeUtf8("abc");
sink.emit();
try {
source.readByteString(4);
fail();
} catch (EOFException expected) {
}
assertEquals("abc", source.readUtf8()); // The read shouldn't consume any data.
}
代码示例来源:origin: square/tape
@Test public void removingElementZeroesData() throws IOException {
QueueFile queueFile = newQueueFile(true);
queueFile.add(values[4]);
queueFile.remove();
queueFile.close();
BufferedSource source = Okio.buffer(Okio.source(file));
source.skip(headerLength);
source.skip(Element.HEADER_LENGTH);
assertThat(source.readByteString(4).hex()).isEqualTo("00000000");
}
代码示例来源:origin: square/tape
@Test public void removingElementDoesNotZeroData() throws IOException {
QueueFile queueFile = newQueueFile(false);
queueFile.add(values[4]);
queueFile.remove();
queueFile.close();
BufferedSource source = Okio.buffer(Okio.source(file));
source.skip(headerLength);
source.skip(Element.HEADER_LENGTH);
assertThat(source.readByteString(4).hex()).isEqualTo("04030201");
source.close();
}
代码示例来源:origin: square/okio
bufferedSource.readByteString(10);
fail();
} catch (IllegalStateException expected) {
代码示例来源:origin: square/okio
@Test public void readByteString() throws IOException {
sink.writeUtf8("abcd").writeUtf8(repeat('e', SEGMENT_SIZE));
sink.emit();
assertEquals("abcd" + repeat('e', SEGMENT_SIZE), source.readByteString().utf8());
}
代码示例来源:origin: square/okio
@Test public void readByteStringPartial() throws IOException {
sink.writeUtf8("abcd").writeUtf8(repeat('e', SEGMENT_SIZE));
sink.emit();
assertEquals("abc", source.readByteString(3).utf8());
assertEquals("d", source.readUtf8(1));
}
内容来源于网络,如有侵权,请联系作者删除!