public Builder(String boundary) { this.boundary = ByteString.encodeUtf8(boundary); }
public Header(ByteString name, String value) { this(name, ByteString.encodeUtf8(value)); }
public Header(String name, String value) { this(ByteString.encodeUtf8(name), ByteString.encodeUtf8(value)); }
@Override public boolean send(String text) { if (text == null) throw new NullPointerException("text == null"); return send(ByteString.encodeUtf8(text), OPCODE_TEXT); }
public static String key(HttpUrl url) { return ByteString.encodeUtf8(url.toString()).md5().hex(); }
public static String acceptHeader(String key) { return ByteString.encodeUtf8(key + WebSocketProtocol.ACCEPT_MAGIC).sha1().base64(); }
public void dumpStringData(String s) throws IOException { System.out.println(" " + s); System.out.println(" String.length: " + s.length()); System.out.println("String.codePointCount: " + s.codePointCount(0, s.length())); System.out.println(" Utf8.size: " + Utf8.size(s)); System.out.println(" UTF-8 bytes: " + ByteString.encodeUtf8(s).hex()); System.out.println(); }
synchronized boolean close(int code, String reason, long cancelAfterCloseMillis) { validateCloseCode(code); ByteString reasonBytes = null; if (reason != null) { reasonBytes = ByteString.encodeUtf8(reason); if (reasonBytes.size() > CLOSE_MESSAGE_MAX) { throw new IllegalArgumentException("reason.size() > " + CLOSE_MESSAGE_MAX + ": " + reason); } } if (failed || enqueuedClose) return false; // Immediately prevent further frames from being enqueued. enqueuedClose = true; // Enqueue the close frame. messageAndCloseQueue.add(new Close(code, reasonBytes, cancelAfterCloseMillis)); runWriter(); return true; }
void checkResponse(Response response) throws ProtocolException { if (response.code() != 101) { throw new ProtocolException("Expected HTTP 101 response but was '" + response.code() + " " + response.message() + "'"); } String headerConnection = response.header("Connection"); if (!"Upgrade".equalsIgnoreCase(headerConnection)) { throw new ProtocolException("Expected 'Connection' header value 'Upgrade' but was '" + headerConnection + "'"); } String headerUpgrade = response.header("Upgrade"); if (!"websocket".equalsIgnoreCase(headerUpgrade)) { throw new ProtocolException( "Expected 'Upgrade' header value 'websocket' but was '" + headerUpgrade + "'"); } String headerAccept = response.header("Sec-WebSocket-Accept"); String acceptExpected = ByteString.encodeUtf8(key + WebSocketProtocol.ACCEPT_MAGIC) .sha1().base64(); if (!acceptExpected.equals(headerAccept)) { throw new ProtocolException("Expected 'Sec-WebSocket-Accept' header value '" + acceptExpected + "' but was '" + headerAccept + "'"); } }
@Test public void selectNotFound() throws IOException { Options options = Options.Companion.of( ByteString.encodeUtf8("ROCK"), ByteString.encodeUtf8("SCISSORS"), ByteString.encodeUtf8("PAPER")); sink.writeUtf8("SPOCK"); sink.emit(); assertEquals(-1, source.select(options)); assertEquals("SPOCK", source.readUtf8()); }
@Test public void selectLongerThanSource() throws IOException { Options options = Options.Companion.of( ByteString.encodeUtf8("abcd"), ByteString.encodeUtf8("abce"), ByteString.encodeUtf8("abcc")); sink.writeUtf8("abc"); sink.emit(); assertEquals(-1, source.select(options)); assertEquals("abc", source.readUtf8()); }
@Test public void indexOfByteStringWithFromIndex() throws Exception { sink.writeUtf8("aaa"); sink.emit(); assertEquals(0, source.indexOf(ByteString.encodeUtf8("a"))); assertEquals(0, source.indexOf(ByteString.encodeUtf8("a"), 0)); assertEquals(1, source.indexOf(ByteString.encodeUtf8("a"), 1)); assertEquals(2, source.indexOf(ByteString.encodeUtf8("a"), 2)); }
@Test public void indexOfElementWithFromIndex() throws Exception { sink.writeUtf8("aaa"); sink.emit(); assertEquals(0, source.indexOfElement(ByteString.encodeUtf8("a"))); assertEquals(0, source.indexOfElement(ByteString.encodeUtf8("a"), 0)); assertEquals(1, source.indexOfElement(ByteString.encodeUtf8("a"), 1)); assertEquals(2, source.indexOfElement(ByteString.encodeUtf8("a"), 2)); }
@Test public void indexOfElement() throws IOException { sink.writeUtf8("a").writeUtf8(repeat('b', SEGMENT_SIZE)).writeUtf8("c"); sink.emit(); assertEquals(0, source.indexOfElement(ByteString.encodeUtf8("DEFGaHIJK"))); assertEquals(1, source.indexOfElement(ByteString.encodeUtf8("DEFGHIJKb"))); assertEquals(SEGMENT_SIZE + 1, source.indexOfElement(ByteString.encodeUtf8("cDEFGHIJK"))); assertEquals(1, source.indexOfElement(ByteString.encodeUtf8("DEFbGHIc"))); assertEquals(-1L, source.indexOfElement(ByteString.encodeUtf8("DEFGHIJK"))); assertEquals(-1L, source.indexOfElement(ByteString.encodeUtf8(""))); }
@Test public void writeUtf8SubstringWithCharset() throws IOException { sink.writeString("təˈranəˌsôr", 3, 7, Charset.forName("utf-8")); sink.flush(); assertEquals(ByteString.encodeUtf8("ranə"), data.readByteString()); }
@Test public void readAndToUppercase() throws Exception { InputStream in = new ByteArrayInputStream("abc".getBytes(Charsets.UTF_8)); assertEquals(ByteString.encodeUtf8("AB"), ByteString.read(in, 2).toAsciiUppercase()); assertEquals(ByteString.encodeUtf8("C"), ByteString.read(in, 1).toAsciiUppercase()); assertEquals(ByteString.EMPTY, ByteString.read(in, 0).toAsciiUppercase()); }
@Test public void indexOfByteStringWithOffset() throws IOException { assertEquals(-1, source.indexOf(ByteString.encodeUtf8("flop"), 1)); sink.writeUtf8("flop flip flop"); sink.emit(); assertEquals(10, source.indexOf(ByteString.encodeUtf8("flop"), 1)); source.readUtf8(); // Clear stream // Make sure we backtrack and resume searching after partial match. sink.writeUtf8("hi hi hi hi hey"); sink.emit(); assertEquals(6, source.indexOf(ByteString.encodeUtf8("hi hi hey"), 1)); }
@Test public void selectValuesHaveCommonPrefix() throws IOException { Options options = Options.Companion.of( ByteString.encodeUtf8("abcd"), ByteString.encodeUtf8("abce"), ByteString.encodeUtf8("abcc")); sink.writeUtf8("abcc").writeUtf8("abcd").writeUtf8("abce"); sink.emit(); assertEquals(2, source.select(options)); assertEquals(0, source.select(options)); assertEquals(1, source.select(options)); }
@Test public void indexOfElementWithOffset() throws IOException { sink.writeUtf8("a").writeUtf8(repeat('b', SEGMENT_SIZE)).writeUtf8("c"); sink.emit(); assertEquals(-1, source.indexOfElement(ByteString.encodeUtf8("DEFGaHIJK"), 1)); assertEquals(15, source.indexOfElement(ByteString.encodeUtf8("DEFGHIJKb"), 15)); }
@Test public void rangeEqualsOnlyReadsUntilMismatch() throws IOException { assumeTrue(factory == Factory.ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE); // Other sources read in chunks anyway. sink.writeUtf8("A man, a plan, a canal. Panama."); sink.emit(); assertFalse(source.rangeEquals(0, ByteString.encodeUtf8("A man."))); assertEquals("A man,", source.getBuffer().readUtf8()); }