private void readPing(Handler handler, int length, byte flags, int streamId) throws IOException { if (length != 8) throw ioException("TYPE_PING length != 8: %s", length); if (streamId != 0) throw ioException("TYPE_PING streamId != 0"); int payload1 = source.readInt(); int payload2 = source.readInt(); boolean ack = (flags & FLAG_ACK) != 0; handler.ping(ack, payload1, payload2); }
private void readPriority(Handler handler, int streamId) throws IOException { int w1 = source.readInt(); boolean exclusive = (w1 & 0x80000000) != 0; int streamDependency = (w1 & 0x7fffffff); int weight = (source.readByte() & 0xff) + 1; handler.priority(streamId, streamDependency, weight, exclusive); }
private void readTheList() throws IOException { byte[] publicSuffixListBytes; byte[] publicSuffixExceptionListBytes; InputStream resource = PublicSuffixDatabase.class.getResourceAsStream(PUBLIC_SUFFIX_RESOURCE); if (resource == null) return; try (BufferedSource bufferedSource = Okio.buffer(new GzipSource(Okio.source(resource)))) { int totalBytes = bufferedSource.readInt(); publicSuffixListBytes = new byte[totalBytes]; bufferedSource.readFully(publicSuffixListBytes); int totalExceptionBytes = bufferedSource.readInt(); publicSuffixExceptionListBytes = new byte[totalExceptionBytes]; bufferedSource.readFully(publicSuffixExceptionListBytes); } synchronized (this) { this.publicSuffixListBytes = publicSuffixListBytes; this.publicSuffixExceptionListBytes = publicSuffixExceptionListBytes; } readCompleteLatch.countDown(); }
public EhTagDatabase(String name, BufferedSource source) throws IOException { this.name = name; int totalBytes = source.readInt(); tags = new byte[totalBytes]; source.readFully(tags); }
private void readWindowUpdate(Handler handler, int length, byte flags, int streamId) throws IOException { if (length != 4) throw ioException("TYPE_WINDOW_UPDATE length !=4: %s", length); long increment = (source.readInt() & 0x7fffffffL); if (increment == 0) throw ioException("windowSizeIncrement was 0", increment); handler.windowUpdate(streamId, increment); }
private void readPing(Handler handler, int length, byte flags, int streamId) throws IOException { if (length != 8) throw ioException("TYPE_PING length != 8: %s", length); if (streamId != 0) throw ioException("TYPE_PING streamId != 0"); int payload1 = source.readInt(); int payload2 = source.readInt(); boolean ack = (flags & FLAG_ACK) != 0; handler.ping(ack, payload1, payload2); }
private void readContinuationHeader() throws IOException { int previousStreamId = streamId; length = left = readMedium(source); byte type = (byte) (source.readByte() & 0xff); flags = (byte) (source.readByte() & 0xff); if (logger.isLoggable(FINE)) logger.fine(frameLog(true, streamId, length, type, flags)); streamId = (source.readInt() & 0x7fffffff); if (type != TYPE_CONTINUATION) throw ioException("%s != TYPE_CONTINUATION", type); if (streamId != previousStreamId) throw ioException("TYPE_CONTINUATION streamId changed"); } }
private void readGoAway(Handler handler, int length, byte flags, int streamId) throws IOException { if (length < 8) throw ioException("TYPE_GOAWAY length < 8: %s", length); if (streamId != 0) throw ioException("TYPE_GOAWAY streamId != 0"); int lastStreamId = source.readInt(); int errorCodeInt = source.readInt(); int opaqueDataLength = length - 8; ErrorCode errorCode = ErrorCode.fromHttp2(errorCodeInt); if (errorCode == null) { throw ioException("TYPE_GOAWAY unexpected error code: %d", errorCodeInt); } ByteString debugData = EMPTY; if (opaqueDataLength > 0) { // Must read debug data in order to not corrupt the connection. debugData = source.readByteString(opaqueDataLength); } handler.goAway(lastStreamId, errorCode, debugData); }
private void readPriority(Handler handler, int streamId) throws IOException { int w1 = source.readInt(); boolean exclusive = (w1 & 0x80000000) != 0; int streamDependency = (w1 & 0x7fffffff); int weight = (source.readByte() & 0xff) + 1; handler.priority(streamId, streamDependency, weight, exclusive); }
private void readPushPromise(Handler handler, int length, byte flags, int streamId) throws IOException { if (streamId == 0) { throw ioException("PROTOCOL_ERROR: TYPE_PUSH_PROMISE streamId == 0"); } short padding = (flags & FLAG_PADDED) != 0 ? (short) (source.readByte() & 0xff) : 0; int promisedStreamId = source.readInt() & 0x7fffffff; length -= 4; // account for above read. length = lengthWithoutPadding(length, flags, padding); List<Header> headerBlock = readHeaderBlock(length, padding, flags, streamId); handler.pushPromise(streamId, promisedStreamId, headerBlock); }
private void readRstStream(Handler handler, int length, byte flags, int streamId) throws IOException { if (length != 4) throw ioException("TYPE_RST_STREAM length: %d != 4", length); if (streamId == 0) throw ioException("TYPE_RST_STREAM streamId == 0"); int errorCodeInt = source.readInt(); ErrorCode errorCode = ErrorCode.fromHttp2(errorCodeInt); if (errorCode == null) { throw ioException("TYPE_RST_STREAM unexpected error code: %d", errorCodeInt); } handler.rstStream(streamId, errorCode); }
private void readWindowUpdate(Handler handler, int length, byte flags, int streamId) throws IOException { if (length != 4) throw ioException("TYPE_WINDOW_UPDATE length !=4: %s", length); long increment = (source.readInt() & 0x7fffffffL); if (increment == 0) throw ioException("windowSizeIncrement was 0", increment); handler.windowUpdate(streamId, increment); }
private void readContinuationHeader() throws IOException { int previousStreamId = streamId; length = left = readMedium(source); byte type = (byte) (source.readByte() & 0xff); flags = (byte) (source.readByte() & 0xff); if (logger.isLoggable(FINE)) logger.fine(frameLog(true, streamId, length, type, flags)); streamId = (source.readInt() & 0x7fffffff); if (type != TYPE_CONTINUATION) throw ioException("%s != TYPE_CONTINUATION", type); if (streamId != previousStreamId) throw ioException("TYPE_CONTINUATION streamId changed"); } }
private void readRstStream(Handler handler, int length, byte flags, int streamId) throws IOException { if (length != 4) throw ioException("TYPE_RST_STREAM length: %d != 4", length); if (streamId == 0) throw ioException("TYPE_RST_STREAM streamId == 0"); int errorCodeInt = source.readInt(); ErrorCode errorCode = ErrorCode.fromHttp2(errorCodeInt); if (errorCode == null) { throw ioException("TYPE_RST_STREAM unexpected error code: %d", errorCodeInt); } handler.rstStream(streamId, errorCode); }
private void readPushPromise(Handler handler, int length, byte flags, int streamId) throws IOException { if (streamId == 0) { throw ioException("PROTOCOL_ERROR: TYPE_PUSH_PROMISE streamId == 0"); } short padding = (flags & FLAG_PADDED) != 0 ? (short) (source.readByte() & 0xff) : 0; int promisedStreamId = source.readInt() & 0x7fffffff; length -= 4; // account for above read. length = lengthWithoutPadding(length, flags, padding); List<Header> headerBlock = readHeaderBlock(length, padding, flags, streamId); handler.pushPromise(streamId, promisedStreamId, headerBlock); }
@Test public void readInt() throws Exception { sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x01, (byte) 0x87, (byte) 0x65, (byte) 0x43, (byte) 0x21 }); sink.emit(); assertEquals(0xabcdef01, source.readInt()); assertEquals(0x87654321, source.readInt()); assertTrue(source.exhausted()); }
@Test public void readIntTooShortThrows() throws IOException { sink.writeInt(Integer.MAX_VALUE); sink.emit(); source.readByte(); try { source.readInt(); fail(); } catch (EOFException expected) { } }
int streamId = (source.readInt() & 0x7fffffff); // Ignore reserved bit. if (logger.isLoggable(FINE)) logger.fine(frameLog(true, streamId, length, type, flags));
for (int i = 0; i < length; i += 6) { int id = source.readShort() & 0xFFFF; int value = source.readInt();
@Test public void readIntSplitAcrossMultipleSegments() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE - 3)); sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x01 }); sink.emit(); source.skip(SEGMENT_SIZE - 3); assertEquals(0xabcdef01, source.readInt()); assertTrue(source.exhausted()); }