use of okio.ByteString in project okhttp by square.
the class WebSocketHttpTest method overflowOutgoingQueue.
@Test
public void overflowOutgoingQueue() throws IOException {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
// Send messages until the client's outgoing buffer overflows!
ByteString message = ByteString.of(new byte[1024 * 1024]);
int messageCount = 0;
while (true) {
boolean success = webSocket.send(message);
if (!success)
break;
messageCount++;
long queueSize = webSocket.queueSize();
assertTrue(queueSize >= 0 && queueSize <= messageCount * message.size());
// Expect to fail before enqueueing 32 MiB.
assertTrue(messageCount < 32);
}
// Confirm all sent messages were received, followed by a client-initiated close.
WebSocket server = serverListener.assertOpen();
for (int i = 0; i < messageCount; i++) {
serverListener.assertBinaryMessage(message);
}
serverListener.assertClosing(1001, "");
// When the server acknowledges the close the connection shuts down gracefully.
server.close(1000, null);
clientListener.assertClosing(1000, "");
clientListener.assertClosed(1000, "");
serverListener.assertClosed(1001, "");
}
use of okio.ByteString in project wire by square.
the class JavaGenerator method messageFieldsAndUnknownFieldsConstructor.
// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64, ByteString unknownFields) {
// super(ADAPTER, unknownFields);
// this.optional_int32 = optional_int32;
// this.optional_int64 = optional_int64;
// }
//
private MethodSpec messageFieldsAndUnknownFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
NameAllocator localNameAllocator = nameAllocator.clone();
String adapterName = localNameAllocator.get("ADAPTER");
String unknownFieldsName = localNameAllocator.newName("unknownFields");
MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super($N, $N)", adapterName, unknownFieldsName);
for (OneOf oneOf : type.oneOfs()) {
if (oneOf.fields().size() < 2)
continue;
CodeBlock.Builder fieldNamesBuilder = CodeBlock.builder();
boolean first = true;
for (Field field : oneOf.fields()) {
if (!first)
fieldNamesBuilder.add(", ");
fieldNamesBuilder.add("$N", localNameAllocator.get(field));
first = false;
}
CodeBlock fieldNames = fieldNamesBuilder.build();
result.beginControlFlow("if ($T.countNonNull($L) > 1)", Internal.class, fieldNames);
result.addStatement("throw new IllegalArgumentException($S)", "at most one of " + fieldNames + " may be non-null");
result.endControlFlow();
}
for (Field field : type.fieldsAndOneOfFields()) {
TypeName javaType = fieldType(field);
String fieldName = localNameAllocator.get(field);
ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
if (emitAndroid && field.isOptional()) {
param.addAnnotation(NULLABLE);
}
result.addParameter(param.build());
if (field.isRepeated() || field.type().isMap()) {
result.addStatement("this.$1L = $2T.immutableCopyOf($1S, $1L)", fieldName, Internal.class);
} else {
result.addStatement("this.$1L = $1L", fieldName);
}
}
result.addParameter(BYTE_STRING, unknownFieldsName);
return result.build();
}
use of okio.ByteString in project wire by square.
the class Sample method run.
public void run() throws IOException {
// Create an immutable value object with the Builder API.
Dinosaur stegosaurus = new Dinosaur.Builder().name("Stegosaurus").period(Period.JURASSIC).length_meters(9.0).mass_kilograms(5_000.0).picture_urls(Arrays.asList("http://goo.gl/LD5KY5", "http://goo.gl/VYRM67")).build();
// Encode that value to bytes, and print that as base64.
byte[] stegosaurusEncoded = Dinosaur.ADAPTER.encode(stegosaurus);
System.out.println(ByteString.of(stegosaurusEncoded).base64());
// Decode base64 bytes, and decode those bytes as a dinosaur.
ByteString tyrannosaurusEncoded = ByteString.decodeBase64("Cg1UeXJhbm5vc2F1cnVzEmhodHRwOi8vdmln" + "bmV0dGUxLndpa2lhLm5vY29va2llLm5ldC9qdXJhc3NpY3BhcmsvaW1hZ2VzLzYvNmEvTGVnbzUuanBnL3Jldmlz" + "aW9uL2xhdGVzdD9jYj0yMDE1MDMxOTAxMTIyMRJtaHR0cDovL3ZpZ25ldHRlMy53aWtpYS5ub2Nvb2tpZS5uZXQv" + "anVyYXNzaWNwYXJrL2ltYWdlcy81LzUwL1JleHlfcHJlcGFyaW5nX2Zvcl9iYXR0bGVfd2l0aF9JbmRvbWludXNf" + "cmV4LmpwZxmamZmZmZkoQCEAAAAAAJC6QCgB");
Dinosaur tyrannosaurus = Dinosaur.ADAPTER.decode(tyrannosaurusEncoded.toByteArray());
// Print both of our dinosaurs.
System.out.println(stegosaurus.name + " is " + stegosaurus.length_meters + " meters long!");
System.out.println(tyrannosaurus.name + " weighs " + tyrannosaurus.mass_kilograms + " kilos!");
}
use of okio.ByteString in project grpc-java by grpc.
the class Headers method createRequestHeaders.
/**
* Serializes the given headers and creates a list of OkHttp {@link Header}s to be used when
* creating a stream. Since this serializes the headers, this method should be called in the
* application thread context.
*/
public static List<Header> createRequestHeaders(Metadata headers, String defaultPath, String authority, String userAgent) {
Preconditions.checkNotNull(headers, "headers");
Preconditions.checkNotNull(defaultPath, "defaultPath");
Preconditions.checkNotNull(authority, "authority");
// 7 is the number of explicit add calls below.
List<Header> okhttpHeaders = new ArrayList<Header>(7 + headers.headerCount());
// Set GRPC-specific headers.
okhttpHeaders.add(SCHEME_HEADER);
okhttpHeaders.add(METHOD_HEADER);
okhttpHeaders.add(new Header(Header.TARGET_AUTHORITY, authority));
String path = defaultPath;
okhttpHeaders.add(new Header(Header.TARGET_PATH, path));
okhttpHeaders.add(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent));
// All non-pseudo headers must come after pseudo headers.
okhttpHeaders.add(CONTENT_TYPE_HEADER);
okhttpHeaders.add(TE_HEADER);
// Now add any application-provided headers.
byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers);
for (int i = 0; i < serializedHeaders.length; i += 2) {
ByteString key = ByteString.of(serializedHeaders[i]);
String keyString = key.utf8();
if (isApplicationHeader(keyString)) {
ByteString value = ByteString.of(serializedHeaders[i + 1]);
okhttpHeaders.add(new Header(key, value));
}
}
return okhttpHeaders;
}
use of okio.ByteString in project zipkin by openzipkin.
the class AWSSignatureVersion4 method signatureKey.
ByteString signatureKey(String secretKey, String yyyyMMdd) {
ByteString kSecret = ByteString.encodeUtf8("AWS4" + secretKey);
ByteString kDate = ByteString.encodeUtf8(yyyyMMdd).hmacSha256(kSecret);
ByteString kRegion = ByteString.encodeUtf8(region).hmacSha256(kDate);
ByteString kService = ByteString.encodeUtf8(service).hmacSha256(kRegion);
ByteString kSigning = ByteString.encodeUtf8("aws4_request").hmacSha256(kService);
return kSigning;
}
Aggregations