Search in sources :

Example 21 with ByteString

use of okio.ByteString in project okhttp by square.

the class FileOperatorTest method largeRead.

@Test
public void largeRead() throws Exception {
    ByteString data = randomByteString(1000000);
    write(data);
    FileOperator operator = new FileOperator(randomAccessFile.getChannel());
    Buffer buffer = new Buffer();
    operator.read(0, buffer, data.size());
    assertEquals(data, buffer.readByteString());
}
Also used : Buffer(okio.Buffer) ByteString(okio.ByteString) Test(org.junit.Test)

Example 22 with ByteString

use of okio.ByteString in project okhttp by square.

the class RelayTest method racingReaders.

@Test
public void racingReaders() throws Exception {
    Pipe pipe = new Pipe(1024);
    BufferedSink sink = Okio.buffer(pipe.sink());
    Relay relay = Relay.edit(file, pipe.source(), metadata, 5);
    Future<ByteString> future1 = executor.submit(sourceReader(relay.newSource()));
    Future<ByteString> future2 = executor.submit(sourceReader(relay.newSource()));
    Thread.sleep(500);
    sink.writeUtf8("abcdefghij");
    Thread.sleep(500);
    sink.writeUtf8("klmnopqrst");
    sink.close();
    assertEquals(ByteString.encodeUtf8("abcdefghijklmnopqrst"), future1.get());
    assertEquals(ByteString.encodeUtf8("abcdefghijklmnopqrst"), future2.get());
    assertTrue(relay.isClosed());
    assertFile(Relay.PREFIX_CLEAN, 20L, metadata.size(), "abcdefghijklmnopqrst", metadata);
}
Also used : ByteString(okio.ByteString) BufferedSink(okio.BufferedSink) Pipe(okio.Pipe) Test(org.junit.Test)

Example 23 with ByteString

use of okio.ByteString in project moshi by square.

the class JsonUtf8Reader method nextQuotedValue.

/**
   * Returns the string up to but not including {@code quote}, unescaping any character escape
   * sequences encountered along the way. The opening quote should have already been read. This
   * consumes the closing quote, but does not include it in the returned string.
   *
   * @throws IOException if any unicode escape sequences are malformed.
   */
private String nextQuotedValue(ByteString runTerminator) throws IOException {
    StringBuilder builder = null;
    while (true) {
        long index = source.indexOfElement(runTerminator);
        if (index == -1L)
            throw syntaxError("Unterminated string");
        // If we've got an escape character, we're going to need a string builder.
        if (buffer.getByte(index) == '\\') {
            if (builder == null)
                builder = new StringBuilder();
            builder.append(buffer.readUtf8(index));
            // '\'
            buffer.readByte();
            builder.append(readEscapeCharacter());
            continue;
        }
        // If it isn't the escape character, it's the quote. Return the string.
        if (builder == null) {
            String result = buffer.readUtf8(index);
            // Consume the quote character.
            buffer.readByte();
            return result;
        } else {
            builder.append(buffer.readUtf8(index));
            // Consume the quote character.
            buffer.readByte();
            return builder.toString();
        }
    }
}
Also used : ByteString(okio.ByteString)

Example 24 with ByteString

use of okio.ByteString in project okhttp by square.

the class OAuthSessionFactory method dispatch.

/** When the browser hits the redirect URL, use the provided code to ask Slack for a session. */
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
    HttpUrl requestUrl = mockWebServer.url(request.getPath());
    String code = requestUrl.queryParameter("code");
    String stateString = requestUrl.queryParameter("state");
    ByteString state = stateString != null ? ByteString.decodeBase64(stateString) : null;
    Listener listener;
    synchronized (this) {
        listener = listeners.get(state);
    }
    if (code == null || listener == null) {
        return new MockResponse().setResponseCode(404).setBody("unexpected request");
    }
    try {
        OAuthSession session = slackApi.exchangeCode(code, redirectUrl());
        listener.sessionGranted(session);
    } catch (IOException e) {
        return new MockResponse().setResponseCode(400).setBody("code exchange failed: " + e.getMessage());
    }
    synchronized (this) {
        listeners.remove(state);
    }
    // Success!
    return new MockResponse().setResponseCode(302).addHeader("Location", "https://twitter.com/CuteEmergency/status/789457462864863232");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) ByteString(okio.ByteString) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl)

Example 25 with ByteString

use of okio.ByteString in project wire by square.

the class SchemaProtoAdapterTest method decodeToUnpacked.

@Test
public void decodeToUnpacked() throws IOException {
    ProtoAdapter<Object> adapter = new RepoBuilder().add("message.proto", "" + "message Message {\n" + "  repeated int32 a = 90 [packed = false];\n" + "}\n").protoAdapter("Message");
    ImmutableMap<String, Object> expected = ImmutableMap.<String, Object>of("a", ImmutableList.of(601, 701));
    ByteString packedEncoded = ByteString.decodeHex("d20504d904bd05");
    assertThat(adapter.decode(new Buffer().write(packedEncoded))).isEqualTo(expected);
    ByteString unpackedEncoded = ByteString.decodeHex("d005d904d005bd05");
    assertThat(adapter.decode(new Buffer().write(unpackedEncoded))).isEqualTo(expected);
}
Also used : Buffer(okio.Buffer) ByteString(okio.ByteString) ByteString(okio.ByteString) Test(org.junit.Test)

Aggregations

ByteString (okio.ByteString)59 Test (org.junit.Test)37 Buffer (okio.Buffer)26 MockResponse (okhttp3.mockwebserver.MockResponse)11 ProtocolException (java.net.ProtocolException)5 IOException (java.io.IOException)4 OneField (com.squareup.wire.protos.edgecases.OneField)3 EOFException (java.io.EOFException)3 BufferedSink (okio.BufferedSink)3 BufferedSource (okio.BufferedSource)3 AllTypes (com.squareup.wire.protos.alltypes.AllTypes)2 Person (com.squareup.wire.protos.person.Person)2 ArrayList (java.util.ArrayList)2 Headers (okhttp3.Headers)2 Request (okhttp3.Request)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 GzipSink (okio.GzipSink)2 Ignore (org.junit.Ignore)2 Phone (retrofit2.converter.protobuf.PhoneProtos.Phone)2 GsonBuilder (com.google.gson.GsonBuilder)1