private int readByte() throws IOException { return source.readByte() & 0xff; }
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 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); }
/** Consumes {@code \r}, {@code \r\n}, or {@code \n} from {@link #source}. */ private void skipCrAndOrLf() throws IOException { if ((source.readByte() & 0xff) == '\r' && source.request(1) && source.getBuffer().getByte(0) == '\n') { source.skip(1); } }
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 readHeaders(Handler handler, int length, byte flags, int streamId) throws IOException { if (streamId == 0) throw ioException("PROTOCOL_ERROR: TYPE_HEADERS streamId == 0"); boolean endStream = (flags & FLAG_END_STREAM) != 0; short padding = (flags & FLAG_PADDED) != 0 ? (short) (source.readByte() & 0xff) : 0; if ((flags & FLAG_PRIORITY) != 0) { readPriority(handler, streamId); length -= 5; // account for above read. } length = lengthWithoutPadding(length, flags, padding); List<Header> headerBlock = readHeaderBlock(length, padding, flags, streamId); handler.headers(endStream, streamId, -1, headerBlock); }
private void readData(Handler handler, int length, byte flags, int streamId) throws IOException { if (streamId == 0) throw ioException("PROTOCOL_ERROR: TYPE_DATA streamId == 0"); // TODO: checkState open or half-closed (local) or raise STREAM_CLOSED boolean inFinished = (flags & FLAG_END_STREAM) != 0; boolean gzipped = (flags & FLAG_COMPRESSED) != 0; if (gzipped) { throw ioException("PROTOCOL_ERROR: FLAG_COMPRESSED without SETTINGS_COMPRESS_DATA"); } short padding = (flags & FLAG_PADDED) != 0 ? (short) (source.readByte() & 0xff) : 0; length = lengthWithoutPadding(length, flags, padding); handler.data(inFinished, streamId, source, length); source.skip(padding); }
@Test public void readByteTooShortThrows() throws IOException { try { source.readByte(); fail(); } catch (EOFException expected) { } }
int b = source.readByte() & 0xff; if (b == 0x80) { // 10000000 throw new IOException("index == 0");
@Test public void gunzipExhaustsSource() throws Exception { Buffer gzippedSource = new Buffer() .write(ByteString.decodeHex("1f8b08000000000000004b4c4a0600c241243503000000")); // 'abc' ExhaustableSource exhaustableSource = new ExhaustableSource(gzippedSource); BufferedSource gunzippedSource = Okio.buffer(new GzipSource(exhaustableSource)); assertEquals('a', gunzippedSource.readByte()); assertEquals('b', gunzippedSource.readByte()); assertEquals('c', gunzippedSource.readByte()); assertFalse(exhaustableSource.exhausted); assertEquals(-1, gunzippedSource.read(new Buffer(), 1)); assertTrue(exhaustableSource.exhausted); }
@Test public void readIntTooShortThrows() throws IOException { sink.writeInt(Integer.MAX_VALUE); sink.emit(); source.readByte(); try { source.readInt(); fail(); } catch (EOFException expected) { } }
@Test public void readLongTooShortThrows() throws IOException { sink.writeLong(Long.MAX_VALUE); sink.emit(); source.readByte(); try { source.readLong(); fail(); } catch (EOFException expected) { } }
@Test public void readIntLeTooShortThrows() throws IOException { sink.writeIntLe(Integer.MAX_VALUE); sink.emit(); source.readByte(); try { source.readIntLe(); fail(); } catch (EOFException expected) { } }
@Test public void readShortTooShortThrows() throws IOException { sink.writeShort(Short.MAX_VALUE); sink.emit(); source.readByte(); try { source.readShort(); fail(); } catch (EOFException expected) { } }
@Test public void readShortLeTooShortThrows() throws IOException { sink.writeShortLe(Short.MAX_VALUE); sink.emit(); source.readByte(); try { source.readShortLe(); fail(); } catch (EOFException expected) { } }
@Test public void readLongLeTooShortThrows() throws IOException { sink.writeLongLe(Long.MAX_VALUE); sink.emit(); source.readByte(); try { source.readLongLe(); fail(); } catch (EOFException expected) { } }
@Test public void skip() throws Exception { sink.writeUtf8("a"); sink.writeUtf8(repeat('b', SEGMENT_SIZE)); sink.writeUtf8("c"); sink.emit(); source.skip(1); assertEquals('b', source.readByte() & 0xff); source.skip(SEGMENT_SIZE - 2); assertEquals('b', source.readByte() & 0xff); source.skip(1); assertTrue(source.exhausted()); }
@Test public void select() throws IOException { Options options = Options.Companion.of( ByteString.encodeUtf8("ROCK"), ByteString.encodeUtf8("SCISSORS"), ByteString.encodeUtf8("PAPER")); sink.writeUtf8("PAPER,SCISSORS,ROCK"); sink.emit(); assertEquals(2, source.select(options)); assertEquals(',', source.readByte()); assertEquals(1, source.select(options)); assertEquals(',', source.readByte()); assertEquals(0, source.select(options)); assertTrue(source.exhausted()); }