use of okio.BufferedSource in project okhttp by square.
the class Http2Test method maxLengthDataFrame.
@Test
public void maxLengthDataFrame() throws IOException {
final byte[] expectedData = new byte[Http2.INITIAL_MAX_FRAME_SIZE];
Arrays.fill(expectedData, (byte) 2);
writeMedium(frame, expectedData.length);
frame.writeByte(Http2.TYPE_DATA);
frame.writeByte(Http2.FLAG_NONE);
frame.writeInt(expectedStreamId & 0x7fffffff);
frame.write(expectedData);
// Check writer sends the same bytes.
assertEquals(frame, sendDataFrame(new Buffer().write(expectedData)));
reader.nextFrame(false, new BaseTestHandler() {
@Override
public void data(boolean inFinished, int streamId, BufferedSource source, int length) throws IOException {
assertFalse(inFinished);
assertEquals(expectedStreamId, streamId);
assertEquals(Http2.INITIAL_MAX_FRAME_SIZE, length);
ByteString data = source.readByteString(length);
for (byte b : data.toByteArray()) {
assertEquals(2, b);
}
}
});
}
use of okio.BufferedSource in project okhttp by square.
the class PublicSuffixDatabaseTest method allPublicSuffixes.
@Test
public void allPublicSuffixes() throws IOException {
InputStream resource = PublicSuffixDatabaseTest.class.getClassLoader().getResourceAsStream(PUBLIC_SUFFIX_RESOURCE);
BufferedSource source = Okio.buffer(new GzipSource(Okio.source(resource)));
int length = source.readInt();
Buffer buffer = new Buffer();
buffer.write(source, length);
resource.close();
while (!buffer.exhausted()) {
String publicSuffix = buffer.readUtf8LineStrict();
if (publicSuffix.contains("*")) {
// A wildcard rule, let's replace the wildcard with a value.
publicSuffix = publicSuffix.replaceAll("\\*", "square");
}
assertNull(publicSuffixDatabase.getEffectiveTldPlusOne(publicSuffix));
String test = "foobar." + publicSuffix;
assertEquals(test, publicSuffixDatabase.getEffectiveTldPlusOne(test));
}
}
use of okio.BufferedSource in project okhttp by square.
the class PublicSuffixDatabase method readTheList.
private void readTheList() {
byte[] publicSuffixListBytes = null;
byte[] publicSuffixExceptionListBytes = null;
InputStream is = PublicSuffixDatabase.class.getClassLoader().getResourceAsStream(PUBLIC_SUFFIX_RESOURCE);
if (is != null) {
BufferedSource bufferedSource = Okio.buffer(new GzipSource(Okio.source(is)));
try {
int totalBytes = bufferedSource.readInt();
publicSuffixListBytes = new byte[totalBytes];
bufferedSource.readFully(publicSuffixListBytes);
int totalExceptionBytes = bufferedSource.readInt();
publicSuffixExceptionListBytes = new byte[totalExceptionBytes];
bufferedSource.readFully(publicSuffixExceptionListBytes);
} catch (IOException e) {
Platform.get().log(Platform.WARN, "Failed to read public suffix list", e);
publicSuffixListBytes = null;
publicSuffixExceptionListBytes = null;
} finally {
closeQuietly(bufferedSource);
}
}
synchronized (this) {
this.publicSuffixListBytes = publicSuffixListBytes;
this.publicSuffixExceptionListBytes = publicSuffixExceptionListBytes;
}
readCompleteLatch.countDown();
}
use of okio.BufferedSource in project QLibrary by DragonsQC.
the class FastJsonResponseBodyConverter method convert.
/*
* 转换方法
*/
@Override
public T convert(ResponseBody value) throws IOException {
BufferedSource bufferedSource = Okio.buffer(value.source());
String tempStr = bufferedSource.readUtf8();
bufferedSource.close();
return JSON.parseObject(tempStr, type);
}
use of okio.BufferedSource in project weex-example by KalicyZhou.
the class DefaultWebSocketAdapter method connect.
@Override
public void connect(String url, @Nullable String protocol, EventListener listener) {
this.eventListener = listener;
OkHttpClient okHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder();
if (protocol != null) {
builder.addHeader(HEADER_SEC_WEBSOCKET_PROTOCOL, protocol);
}
builder.url(url);
WebSocketCall.create(okHttpClient, builder.build()).enqueue(new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Request request, Response response) throws IOException {
ws = webSocket;
eventListener.onOpen();
}
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
eventListener.onMessage(payload.readUtf8());
payload.close();
}
@Override
public void onPong(Buffer payload) {
}
@Override
public void onClose(int code, String reason) {
eventListener.onClose(code, reason, true);
}
@Override
public void onFailure(IOException e) {
e.printStackTrace();
if (e instanceof EOFException) {
eventListener.onClose(WebSocketCloseCodes.CLOSE_NORMAL.getCode(), WebSocketCloseCodes.CLOSE_NORMAL.name(), true);
} else {
eventListener.onError(e.getMessage());
}
}
});
}
Aggregations