@Override public void flush() throws IOException { if (closed) return; // Don't throw; this stream might have been closed on the caller's behalf. sink.flush(); }
@Override public synchronized void flush() throws IOException { if (closed) return; // Don't throw; this stream might have been closed on the caller's behalf. sink.flush(); }
@Override public void flush() throws IOException { if (closed) return; // Weird, but consistent with historical behavior. sink.flush(); }
@Override public void flushRequest() throws IOException { sink.flush(); }
@Override public void finishRequest() throws IOException { sink.flush(); }
public synchronized void flush() throws IOException { if (closed) throw new IOException("closed"); sink.flush(); }
public synchronized void rstStream(int streamId, ErrorCode errorCode) throws IOException { if (closed) throw new IOException("closed"); if (errorCode.httpCode == -1) throw new IllegalArgumentException(); int length = 4; byte type = TYPE_RST_STREAM; byte flags = FLAG_NONE; frameHeader(streamId, length, type, flags); sink.writeInt(errorCode.httpCode); sink.flush(); }
/** Force buffered operations to the filesystem. */ @Override public synchronized void flush() throws IOException { if (!initialized) return; checkNotClosed(); trimToSize(); journalWriter.flush(); }
public synchronized void connectionPreface() throws IOException { if (closed) throw new IOException("closed"); if (!client) return; // Nothing to write; servers don't send connection headers! if (logger.isLoggable(FINE)) { logger.fine(format(">> CONNECTION %s", CONNECTION_PREFACE.hex())); } sink.write(CONNECTION_PREFACE.toByteArray()); sink.flush(); }
/** * Send a connection-level ping to the peer. {@code ack} indicates this is a reply. The data in * {@code payload1} and {@code payload2} opaque binary, and there are no rules on the content. */ public synchronized void ping(boolean ack, int payload1, int payload2) throws IOException { if (closed) throw new IOException("closed"); int length = 8; byte type = TYPE_PING; byte flags = ack ? FLAG_ACK : FLAG_NONE; int streamId = 0; frameHeader(streamId, length, type, flags); sink.writeInt(payload1); sink.writeInt(payload2); sink.flush(); }
/** * Inform peer that an additional {@code windowSizeIncrement} bytes can be sent on {@code * streamId}, or the connection if {@code streamId} is zero. */ public synchronized void windowUpdate(int streamId, long windowSizeIncrement) throws IOException { if (closed) throw new IOException("closed"); if (windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL) { throw illegalArgument("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: %s", windowSizeIncrement); } int length = 4; byte type = TYPE_WINDOW_UPDATE; byte flags = FLAG_NONE; frameHeader(streamId, length, type, flags); sink.writeInt((int) windowSizeIncrement); sink.flush(); }
/** Applies {@code peerSettings} and then sends a settings ACK. */ public synchronized void applyAndAckSettings(Settings peerSettings) throws IOException { if (closed) throw new IOException("closed"); this.maxFrameSize = peerSettings.getMaxFrameSize(maxFrameSize); if (peerSettings.getHeaderTableSize() != -1) { hpackWriter.setHeaderTableSizeSetting(peerSettings.getHeaderTableSize()); } int length = 0; byte type = TYPE_SETTINGS; byte flags = FLAG_ACK; int streamId = 0; frameHeader(streamId, length, type, flags); sink.flush(); }
/** * Tell the peer to stop creating streams and that we last processed {@code lastGoodStreamId}, or * zero if no streams were processed. * * @param lastGoodStreamId the last stream ID processed, or zero if no streams were processed. * @param errorCode reason for closing the connection. * @param debugData only valid for HTTP/2; opaque debug data to send. */ public synchronized void goAway(int lastGoodStreamId, ErrorCode errorCode, byte[] debugData) throws IOException { if (closed) throw new IOException("closed"); if (errorCode.httpCode == -1) throw illegalArgument("errorCode.httpCode == -1"); int length = 8 + debugData.length; byte type = TYPE_GOAWAY; byte flags = FLAG_NONE; int streamId = 0; frameHeader(streamId, length, type, flags); sink.writeInt(lastGoodStreamId); sink.writeInt(errorCode.httpCode); if (debugData.length > 0) { sink.write(debugData); } sink.flush(); }
/** Write okhttp's settings to the peer. */ public synchronized void settings(Settings settings) throws IOException { if (closed) throw new IOException("closed"); int length = settings.size() * 6; byte type = TYPE_SETTINGS; byte flags = FLAG_NONE; int streamId = 0; frameHeader(streamId, length, type, flags); for (int i = 0; i < Settings.COUNT; i++) { if (!settings.isSet(i)) continue; int id = i; if (id == 4) { id = 3; // SETTINGS_MAX_CONCURRENT_STREAMS renumbered. } else if (id == 7) { id = 4; // SETTINGS_INITIAL_WINDOW_SIZE renumbered. } sink.writeShort(id); sink.writeInt(settings.get(i)); } sink.flush(); }
private void writeHeaders(BufferedSink sink, Headers headers) throws IOException { for (int i = 0, size = headers.size(); i < size; i++) { sink.writeUtf8(headers.name(i)); sink.writeUtf8(": "); sink.writeUtf8(headers.value(i)); sink.writeUtf8("\r\n"); } sink.writeUtf8("\r\n"); sink.flush(); }
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 writeNothing() throws IOException { sink.writeUtf8(""); sink.flush(); assertEquals(0, data.size()); }
@Test public void writeShort() throws Exception { sink.writeShort(0xabcd); sink.writeShort(0x4321); sink.flush(); assertEquals("[hex=abcd4321]", data.toString()); }
@Test public void writeIntLe() throws Exception { sink.writeIntLe(0xabcdef01); sink.writeIntLe(0x87654321); sink.flush(); assertEquals("[hex=01efcdab21436587]", data.toString()); }
@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()); }