Search in sources :

Example 51 with ByteString

use of okio.ByteString in project okhttp by square.

the class Relay method read.

/**
   * Creates a relay that reads a recorded stream from {@code file}.
   *
   * <p><strong>Warning:</strong> callers to this method must immediately call {@link #newSource} to
   * create a source and close that when they're done. Otherwise a handle to {@code file} will be
   * leaked.
   */
public static Relay read(File file) throws IOException {
    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
    FileOperator fileOperator = new FileOperator(randomAccessFile.getChannel());
    // Read the header.
    Buffer header = new Buffer();
    fileOperator.read(0, header, FILE_HEADER_SIZE);
    ByteString prefix = header.readByteString(PREFIX_CLEAN.size());
    if (!prefix.equals(PREFIX_CLEAN))
        throw new IOException("unreadable cache file");
    long upstreamSize = header.readLong();
    long metadataSize = header.readLong();
    // Read the metadata.
    Buffer metadataBuffer = new Buffer();
    fileOperator.read(FILE_HEADER_SIZE + upstreamSize, metadataBuffer, metadataSize);
    ByteString metadata = metadataBuffer.readByteString();
    // Return the result.
    return new Relay(randomAccessFile, null, upstreamSize, metadata, 0L);
}
Also used : Buffer(okio.Buffer) RandomAccessFile(java.io.RandomAccessFile) ByteString(okio.ByteString) IOException(java.io.IOException)

Example 52 with ByteString

use of okio.ByteString in project retrofit by square.

the class WireConverterFactoryTest method deserializeWrongType.

@Test
public void deserializeWrongType() throws IOException {
    ByteString encoded = ByteString.decodeBase64("Cg4oNTE5KSA4NjctNTMwOQ==");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    try {
        service.wrongType();
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Unable to create converter for java.util.List<java.lang.String>\n" + "    for method Service.wrongType");
        assertThat(e.getCause()).hasMessage("" + "Could not locate ResponseBody converter for java.util.List<java.lang.String>.\n" + "  Tried:\n" + "   * retrofit2.BuiltInConverters\n" + "   * retrofit2.converter.wire.WireConverterFactory");
    }
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) Test(org.junit.Test)

Example 53 with ByteString

use of okio.ByteString in project retrofit by square.

the class WireConverterFactoryTest method serializeAndDeserialize.

@Test
public void serializeAndDeserialize() throws IOException, InterruptedException {
    ByteString encoded = ByteString.decodeBase64("Cg4oNTE5KSA4NjctNTMwOQ==");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    Call<Phone> call = service.post(new Phone("(519) 867-5309"));
    Response<Phone> response = call.execute();
    Phone body = response.body();
    assertThat(body.number).isEqualTo("(519) 867-5309");
    RecordedRequest request = server.takeRequest();
    assertThat(request.getBody().readByteString()).isEqualTo(encoded);
    assertThat(request.getHeader("Content-Type")).isEqualTo("application/x-protobuf");
}
Also used : Buffer(okio.Buffer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) Test(org.junit.Test)

Example 54 with ByteString

use of okio.ByteString in project wire by square.

the class GsonTest method createBuilder.

private static AllTypes.Builder createBuilder() {
    ByteString bytes = ByteString.of((byte) 123, (byte) 125);
    AllTypes.NestedMessage nestedMessage = new AllTypes.NestedMessage.Builder().a(999).build();
    return new AllTypes.Builder().opt_int32(111).opt_uint32(112).opt_sint32(113).opt_fixed32(114).opt_sfixed32(115).opt_int64(116L).opt_uint64(117L).opt_sint64(118L).opt_fixed64(119L).opt_sfixed64(120L).opt_bool(true).opt_float(122.0F).opt_double(123.0).opt_string("124").opt_bytes(bytes).opt_nested_enum(AllTypes.NestedEnum.A).opt_nested_message(nestedMessage).req_int32(111).req_uint32(112).req_sint32(113).req_fixed32(114).req_sfixed32(115).req_int64(116L).req_uint64(117L).req_sint64(118L).req_fixed64(119L).req_sfixed64(120L).req_bool(true).req_float(122.0F).req_double(123.0).req_string("124").req_bytes(bytes).req_nested_enum(AllTypes.NestedEnum.A).req_nested_message(nestedMessage).rep_int32(list(111)).rep_uint32(list(112)).rep_sint32(list(113)).rep_fixed32(list(114)).rep_sfixed32(list(115)).rep_int64(list(116L)).rep_uint64(list(117L)).rep_sint64(list(118L)).rep_fixed64(list(119L)).rep_sfixed64(list(120L)).rep_bool(list(true)).rep_float(list(122.0F)).rep_double(list(123.0)).rep_string(list("124")).rep_bytes(list(bytes)).rep_nested_enum(list(AllTypes.NestedEnum.A)).rep_nested_message(list(nestedMessage)).pack_int32(list(111)).pack_uint32(list(112)).pack_sint32(list(113)).pack_fixed32(list(114)).pack_sfixed32(list(115)).pack_int64(list(116L)).pack_uint64(list(117L)).pack_sint64(list(118L)).pack_fixed64(list(119L)).pack_sfixed64(list(120L)).pack_bool(list(true)).pack_float(list(122.0F)).pack_double(list(123.0)).pack_nested_enum(list(AllTypes.NestedEnum.A)).map_int32_int32(singletonMap(1, 2)).map_string_string(singletonMap("key", "value")).map_string_message(singletonMap("message", new AllTypes.NestedMessage(1))).map_string_enum(singletonMap("enum", AllTypes.NestedEnum.A)).ext_opt_int32(Integer.MAX_VALUE).ext_opt_int64(Long.MIN_VALUE / 2 + 178).ext_opt_uint64(Long.MIN_VALUE / 2 + 178).ext_opt_sint64(Long.MIN_VALUE / 2 + 178).ext_opt_bool(true).ext_opt_float(1.2345e6F).ext_opt_double(1.2345e67).ext_opt_nested_enum(AllTypes.NestedEnum.A).ext_opt_nested_message(nestedMessage).ext_rep_int32(list(Integer.MAX_VALUE)).ext_rep_uint64(list(Long.MIN_VALUE / 2 + 178)).ext_rep_sint64(list(Long.MIN_VALUE / 2 + 178)).ext_rep_bool(list(true)).ext_rep_float(list(1.2345e6F)).ext_rep_double(list(1.2345e67)).ext_rep_nested_enum(list(AllTypes.NestedEnum.A)).ext_rep_nested_message(list(nestedMessage)).ext_pack_int32(list(Integer.MAX_VALUE)).ext_pack_uint64(list(Long.MIN_VALUE / 2 + 178)).ext_pack_sint64(list(Long.MIN_VALUE / 2 + 178)).ext_pack_bool(list(true)).ext_pack_float(list(1.2345e6F)).ext_pack_double(list(1.2345e67)).ext_pack_nested_enum(list(AllTypes.NestedEnum.A)).ext_map_int32_int32(singletonMap(1, 2)).ext_map_string_string(singletonMap("key", "value")).ext_map_string_message(singletonMap("message", new AllTypes.NestedMessage(1))).ext_map_string_enum(singletonMap("enum", AllTypes.NestedEnum.A));
}
Also used : ByteString(okio.ByteString) GsonBuilder(com.google.gson.GsonBuilder) AllTypes(com.squareup.wire.protos.alltypes.AllTypes)

Example 55 with ByteString

use of okio.ByteString in project wire by square.

the class ParseTest method truncatedMessageThrowsEOFException.

@Test
public void truncatedMessageThrowsEOFException() throws Exception {
    // tag 1 / 4-byte length delimited string: 0x000000 (3 bytes)
    ByteString data = ByteString.decodeHex("0a04000000");
    try {
        OneBytesField.ADAPTER.decode(data.toByteArray());
        fail();
    } catch (EOFException expected) {
    }
}
Also used : ByteString(okio.ByteString) EOFException(java.io.EOFException) 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