/** @deprecated Use {@link #getBody() getBody().readUtf8()}. */ public String getUtf8Body() { return getBody().readUtf8(); }
static String percentDecode(String encoded, int pos, int limit, boolean plusIsSpace) { for (int i = pos; i < limit; i++) { char c = encoded.charAt(i); if (c == '%' || (c == '+' && plusIsSpace)) { // Slow path: the character at i requires decoding! Buffer out = new Buffer(); out.writeUtf8(encoded, pos, i); percentDecode(out, encoded, i, limit, plusIsSpace); return out.readUtf8(); } } // Fast path: no characters in [pos..limit) required decoding. return encoded.substring(pos, limit); }
/** Prints out this {@link StatsSnapshot} into log. */ @SuppressWarnings("UnusedDeclaration") public void dump() { Buffer buffer = new Buffer(); try { dump(buffer); } catch (IOException e) { throw new AssertionError(e); } Log.i(TAG, buffer.readUtf8()); }
@Override public String convert(T value) throws IOException { Buffer buffer = new Buffer(); delegate.convert(value).writeTo(buffer); return buffer.readUtf8(); } }
private static String canonicalizeForPath(String input, boolean alreadyEncoded) { int codePoint; for (int i = 0, limit = input.length(); i < limit; i += Character.charCount(codePoint)) { codePoint = input.codePointAt(i); if (codePoint < 0x20 || codePoint >= 0x7f || PATH_SEGMENT_ALWAYS_ENCODE_SET.indexOf(codePoint) != -1 || (!alreadyEncoded && (codePoint == '/' || codePoint == '%'))) { // Slow path: the character at i requires encoding! Buffer out = new Buffer(); out.writeUtf8(input, 0, i); canonicalizeForPath(out, input, i, limit, alreadyEncoded); return out.readUtf8(); } } // Fast path: no characters required encoding. return input; }
/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */ private static String toHumanReadableAscii(String s) { for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) { c = s.codePointAt(i); if (c > '\u001f' && c < '\u007f') continue; Buffer buffer = new Buffer(); buffer.writeUtf8(s, 0, i); buffer.writeUtf8CodePoint('?'); for (int j = i + Character.charCount(c); j < length; j += Character.charCount(c)) { c = s.codePointAt(j); buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?'); } return buffer.readUtf8(); } return s; }
/** * Consumes and returns a non-empty token, terminating at special characters in {@link * #TOKEN_DELIMITERS}. Returns null if the buffer is empty or prefixed with a delimiter. */ private static String readToken(Buffer buffer) { try { long tokenSize = buffer.indexOfElement(TOKEN_DELIMITERS); if (tokenSize == -1L) tokenSize = buffer.size(); return tokenSize != 0L ? buffer.readUtf8(tokenSize) : null; } catch (EOFException e) { throw new AssertionError(); } }
private void readMessageFrame() throws IOException { int opcode = this.opcode; if (opcode != OPCODE_TEXT && opcode != OPCODE_BINARY) { throw new ProtocolException("Unknown opcode: " + toHexString(opcode)); } readMessage(); if (opcode == OPCODE_TEXT) { frameCallback.onReadMessage(messageFrameBuffer.readUtf8()); } else { frameCallback.onReadMessage(messageFrameBuffer.readByteString()); } }
private void completeEvent(String id, String type, Buffer data) throws IOException { skipCrAndOrLf(); if (data.size() != 0L) { lastId = id; data.skip(1L); // Leading newline. callback.onEvent(id, type, data.readUtf8()); } }
/** * Reads a double-quoted string, unescaping quoted pairs like {@code \"} to the 2nd character in * each sequence. Returns the unescaped string, or null if the buffer isn't prefixed with a * double-quoted string. */ private static String readQuotedString(Buffer buffer) { if (buffer.readByte() != '\"') throw new IllegalArgumentException(); Buffer result = new Buffer(); while (true) { long i = buffer.indexOfElement(QUOTED_STRING_DELIMITERS); if (i == -1L) return null; // Unterminated quoted string. if (buffer.getByte(i) == '"') { result.write(buffer, i); buffer.readByte(); // Consume '"'. return result.readUtf8(); } if (buffer.size() == i + 1L) return null; // Dangling escape. result.write(buffer, i); buffer.readByte(); // Consume '\'. result.write(buffer, 1L); // The escaped character. } }
@Test public void testReadChannel() throws Exception { ReadableByteChannel channel = new Buffer().writeUtf8(quote); Buffer buffer = new Buffer(); Source source = new ByteChannelSource(channel, Timeout.NONE); source.read(buffer, 75); assertThat(buffer.readUtf8()) .isEqualTo("John, the kind of control you're attempting simply is... it's not possible."); }
@Test public void writePrefixToEmptyBuffer() throws IOException { Buffer sink = new Buffer(); Buffer source = new Buffer(); source.writeUtf8("abcd"); sink.write(source, 2); assertEquals("ab", sink.readUtf8(2)); }
private MockResponse formRequest(RecordedRequest request) { assertEquals("application/x-www-form-urlencoded;charset=UTF-8", request.getHeader("Content-Type")); String body = request.getBody().readUtf8(); assertThat(body, Matchers.containsString("name+1=value+1")); assertThat(body, Matchers.containsString("name+2=value+2%2B1")); assertThat(body, Matchers.containsString("name+2=value+2%2B2")); return new MockResponse().setResponseCode(200); }
private void assertLongHexString(long value) throws IOException { sink.writeHexadecimalUnsignedLong(value).writeUtf8("zzz").flush(); String expected = String.format("%x", value) + "zzz"; String actual = data.readUtf8(); assertEquals(value + " expected " + expected + " but was " + actual, actual, expected); } }
@Test public void readFullyTooShortThrows() throws IOException { sink.writeUtf8("Hi"); sink.emit(); Buffer sink = new Buffer(); try { source.readFully(sink, 5); fail(); } catch (EOFException ignored) { } // Verify we read all that we could from the source. assertEquals("Hi", sink.readUtf8()); }
@Test public void stringSpansSegments() throws Exception { Buffer buffer = new Buffer(); String a = TestUtil.repeat('a', SEGMENT_SIZE - 1); String b = "bb"; String c = TestUtil.repeat('c', SEGMENT_SIZE - 1); buffer.writeUtf8(a); buffer.writeUtf8(b); buffer.writeUtf8(c); assertEquals(a + b + c, buffer.readUtf8()); }
@Test public void cloneMultipleSegments() throws Exception { Buffer original = new Buffer(); original.writeUtf8(repeat('a', SEGMENT_SIZE * 3)); Buffer clone = original.clone(); original.writeUtf8(repeat('b', SEGMENT_SIZE * 3)); clone.writeUtf8(repeat('c', SEGMENT_SIZE * 3)); assertEquals(repeat('a', SEGMENT_SIZE * 3) + repeat('b', SEGMENT_SIZE * 3), original.readUtf8(SEGMENT_SIZE * 6)); assertEquals(repeat('a', SEGMENT_SIZE * 3) + repeat('c', SEGMENT_SIZE * 3), clone.readUtf8(SEGMENT_SIZE * 6)); }
@Test public void writeLastIntegerInSegment() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE - 4)); sink.writeInt(0xabcdef01); sink.writeInt(0x87654321); sink.flush(); assertEquals(asList(SEGMENT_SIZE, 4), segmentSizes(data)); assertEquals(repeat('a', SEGMENT_SIZE - 4), data.readUtf8(SEGMENT_SIZE - 4)); assertEquals("[hex=abcdef0187654321]", data.toString()); }
@Test public void shouldSendPojoAsJson() { prepareResponse(response -> response.setHeader("Content-Type", "application/json") .setBody("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}")); Mono<Pojo> result = this.webClient.post() .uri("/pojo/capitalize") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) .syncBody(new Pojo("foofoo", "barbar")) .retrieve() .bodyToMono(Pojo.class); StepVerifier.create(result) .consumeNextWith(p -> assertEquals("BARBAR", p.getBar())) .expectComplete() .verify(Duration.ofSeconds(3)); expectRequestCount(1); expectRequest(request -> { assertEquals("/pojo/capitalize", request.getPath()); assertEquals("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}", request.getBody().readUtf8()); assertEquals("31", request.getHeader(HttpHeaders.CONTENT_LENGTH)); assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); assertEquals("application/json", request.getHeader(HttpHeaders.CONTENT_TYPE)); }); }