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());
}
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);
}
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();
}
}
}
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");
}
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);
}
Aggregations