public String boundary() { return boundary.utf8(); }
/** * An HTTP/2 response cannot contain uppercase header characters and must be treated as * malformed. */ static ByteString checkLowercase(ByteString name) throws IOException { for (int i = 0, length = name.size(); i < length; i++) { byte c = name.getByte(i); if (c >= 'A' && c <= 'Z') { throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + name.utf8()); } } return name; } }
MultipartBody(ByteString boundary, MediaType type, List<Part> parts) { this.boundary = boundary; this.originalType = type; this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8()); this.parts = Util.immutableList(parts); }
MultipartBody(ByteString boundary, MediaType type, List<Part> parts) { this.boundary = boundary; this.originalType = type; this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8()); this.parts = Util.immutableList(parts); }
public void readConnectionPreface(Handler handler) throws IOException { if (client) { // The client reads the initial SETTINGS frame. if (!nextFrame(true, handler)) { throw ioException("Required SETTINGS preface not received"); } } else { // The server reads the CONNECTION_PREFACE byte string. ByteString connectionPreface = source.readByteString(CONNECTION_PREFACE.size()); if (logger.isLoggable(FINE)) logger.fine(format("<< CONNECTION %s", connectionPreface.hex())); if (!CONNECTION_PREFACE.equals(connectionPreface)) { throw ioException("Expected a connection header but was %s", connectionPreface.utf8()); } } }
@Test public void ofByteBuffer() { byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byteBuffer.position(2).limit(11); ByteString byteString = ByteString.of(byteBuffer); // Verify that the bytes were copied out. byteBuffer.put(4, (byte) 'a'); assertEquals("llo, Worl", byteString.utf8()); }
@Test public void decodeBase64WithWhitespace() { assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA AA ").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA A\r\nA ").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("AA AA").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA AA ").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA A\r\nA ").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("A AAA").utf8()); assertEquals("", ByteString.decodeBase64(" ").utf8()); }
@Test public void ofCopy() { byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8); ByteString byteString = ByteString.of(bytes); // Verify that the bytes were copied out. bytes[4] = (byte) 'a'; assertEquals("Hello, World!", byteString.utf8()); }
@Test public void ofCopyRange() { byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8); ByteString byteString = ByteString.of(bytes, 2, 9); // Verify that the bytes were copied out. bytes[4] = (byte) 'a'; assertEquals("llo, Worl", byteString.utf8()); }
@Test public void utf8() throws Exception { ByteString byteString = factory.encodeUtf8(bronzeHorseman); assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(Charsets.UTF_8)); assertTrue(byteString.equals(ByteString.of(bronzeHorseman.getBytes(Charsets.UTF_8)))); assertEquals(byteString.utf8(), bronzeHorseman); }
@Test public void readByteString() throws IOException { sink.writeUtf8("abcd").writeUtf8(repeat('e', SEGMENT_SIZE)); sink.emit(); assertEquals("abcd" + repeat('e', SEGMENT_SIZE), source.readByteString().utf8()); }
@Test public void read_withByteArraySmallerThanLimit_shouldConsumeSizeOfByteArray() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 6); byte[] data = new byte[5]; int numberOfBytesRead = fixedLengthInputStream.read(data); assertEquals(5, numberOfBytesRead); assertEquals("Hello", ByteString.of(data).utf8()); }
@Test public void readByteStringPartial() throws IOException { sink.writeUtf8("abcd").writeUtf8(repeat('e', SEGMENT_SIZE)); sink.emit(); assertEquals("abc", source.readByteString(3).utf8()); assertEquals("d", source.readUtf8(1)); }
@Test public void read_withOverSizedByteArrayInMiddleOfStream_shouldReturnDataUpToLimit() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 6); consumeBytes(fixedLengthInputStream, 5); byte[] data = new byte[10]; int numberOfBytesRead = fixedLengthInputStream.read(data); assertEquals(1, numberOfBytesRead); assertEquals(" ", ByteString.of(data, 0, numberOfBytesRead).utf8()); }
@Test public void decodeBase64() { assertEquals("", ByteString.decodeBase64("").utf8()); assertEquals(null, ByteString.decodeBase64("/===")); // Can't do anything with 6 bits! assertEquals(ByteString.decodeHex("ff"), ByteString.decodeBase64("//==")); assertEquals(ByteString.decodeHex("ff"), ByteString.decodeBase64("__==")); assertEquals(ByteString.decodeHex("ffff"), ByteString.decodeBase64("///=")); assertEquals(ByteString.decodeHex("ffff"), ByteString.decodeBase64("___=")); assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("////")); assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("____")); assertEquals(ByteString.decodeHex("ffffffffffff"), ByteString.decodeBase64("////////")); assertEquals(ByteString.decodeHex("ffffffffffff"), ByteString.decodeBase64("________")); assertEquals("What's to be scared about? It's just a little hiccup in the power...", ByteString.decodeBase64("V2hhdCdzIHRvIGJlIHNjYXJlZCBhYm91dD8gSXQncyBqdXN0IGEgbGl0dGxlIGhpY2" + "N1cCBpbiB0aGUgcG93ZXIuLi4=").utf8()); // Uses two encoding styles. Malformed, but supported as a side-effect. assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("__//")); }