Search in sources :

Example 81 with BufferedSource

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);
            }
        }
    });
}
Also used : Buffer(okio.Buffer) ByteString(okio.ByteString) IOException(java.io.IOException) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 82 with BufferedSource

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));
    }
}
Also used : GzipSource(okio.GzipSource) Buffer(okio.Buffer) InputStream(java.io.InputStream) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 83 with BufferedSource

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();
}
Also used : GzipSource(okio.GzipSource) InputStream(java.io.InputStream) IOException(java.io.IOException) BufferedSource(okio.BufferedSource)

Example 84 with BufferedSource

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);
}
Also used : BufferedSource(okio.BufferedSource)

Example 85 with BufferedSource

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());
            }
        }
    });
}
Also used : Buffer(okio.Buffer) WebSocketListener(com.squareup.okhttp.ws.WebSocketListener) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) WebSocket(com.squareup.okhttp.ws.WebSocket) Response(com.squareup.okhttp.Response) EOFException(java.io.EOFException) BufferedSource(okio.BufferedSource)

Aggregations

BufferedSource (okio.BufferedSource)97 Test (org.junit.Test)51 IOException (java.io.IOException)23 Buffer (okio.Buffer)23 Request (okhttp3.Request)10 Response (okhttp3.Response)10 Gson (com.google.gson.Gson)8 EOFException (java.io.EOFException)8 ResponseBody (okhttp3.ResponseBody)8 BarCode (com.nytimes.android.external.store.base.impl.BarCode)7 BarCode (com.nytimes.android.external.store3.base.impl.BarCode)7 Charset (java.nio.charset.Charset)7 MediaType (okhttp3.MediaType)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 File (java.io.File)6 InputStream (java.io.InputStream)6 Headers (okhttp3.Headers)6 RequestBody (okhttp3.RequestBody)6 BufferedSink (okio.BufferedSink)6 List (java.util.List)4