/** Append space-prefixed lengths to {@code writer}. */ void writeLengths(BufferedSink writer) throws IOException { for (long length : lengths) { writer.writeByte(' ').writeDecimalLong(length); } }
void encode(ByteString data, BufferedSink sink) throws IOException { long current = 0; int n = 0; for (int i = 0; i < data.size(); i++) { int b = data.getByte(i) & 0xFF; int code = CODES[b]; int nbits = CODE_LENGTHS[b]; current <<= nbits; current |= code; n += nbits; while (n >= 8) { n -= 8; sink.writeByte(((int) (current >> n))); } } if (n > 0) { current <<= (8 - n); current |= (0xFF >>> n); sink.writeByte((int) current); } }
private void newline() throws IOException { if (indent == null) { return; } sink.writeByte('\n'); for (int i = 1, size = stackSize; i < size; i++) { sink.writeUtf8(indent); } }
private void writeCertList(BufferedSink sink, List<Certificate> certificates) throws IOException { try { sink.writeDecimalLong(certificates.size()) .writeByte('\n'); for (int i = 0, size = certificates.size(); i < size; i++) { byte[] bytes = certificates.get(i).getEncoded(); String line = ByteString.of(bytes).base64(); sink.writeUtf8(line) .writeByte('\n'); } } catch (CertificateEncodingException e) { throw new IOException(e.getMessage()); } }
public void frameHeader(int streamId, int length, byte type, byte flags) throws IOException { if (logger.isLoggable(FINE)) logger.fine(frameLog(false, streamId, length, type, flags)); if (length > maxFrameSize) { throw illegalArgument("FRAME_SIZE_ERROR length > %d: %d", maxFrameSize, length); } if ((streamId & 0x80000000) != 0) throw illegalArgument("reserved bit set: %s", streamId); writeMedium(sink, length); sink.writeByte(type & 0xff); sink.writeByte(flags & 0xff); sink.writeInt(streamId & 0x7fffffff); }
boolean removeEntry(Entry entry) throws IOException { if (entry.currentEditor != null) { entry.currentEditor.detach(); // Prevent the edit from completing normally. } for (int i = 0; i < valueCount; i++) { fileSystem.delete(entry.cleanFiles[i]); size -= entry.lengths[i]; entry.lengths[i] = 0; } redundantOpCount++; journalWriter.writeUtf8(REMOVE).writeByte(' ').writeUtf8(entry.key).writeByte('\n'); lruEntries.remove(entry.key); if (journalRebuildRequired()) { executor.execute(cleanupRunnable); } return true; }
writer.writeUtf8(MAGIC).writeByte('\n'); writer.writeUtf8(VERSION_1).writeByte('\n'); writer.writeDecimalLong(appVersion).writeByte('\n'); writer.writeDecimalLong(valueCount).writeByte('\n'); writer.writeByte('\n'); writer.writeUtf8(DIRTY).writeByte(' '); writer.writeUtf8(entry.key); writer.writeByte('\n'); } else { writer.writeUtf8(CLEAN).writeByte(' '); writer.writeUtf8(entry.key); entry.writeLengths(writer); writer.writeByte('\n');
/** * Returns a snapshot of the entry named {@code key}, or null if it doesn't exist is not currently * readable. If a value is returned, it is moved to the head of the LRU queue. */ public synchronized Snapshot get(String key) throws IOException { initialize(); checkNotClosed(); validateKey(key); Entry entry = lruEntries.get(key); if (entry == null || !entry.readable) return null; Snapshot snapshot = entry.snapshot(); if (snapshot == null) return null; redundantOpCount++; journalWriter.writeUtf8(READ).writeByte(' ').writeUtf8(key).writeByte('\n'); if (journalRebuildRequired()) { executor.execute(cleanupRunnable); } return snapshot; }
/** * Inserts any necessary separators and whitespace before a name. Also * adjusts the stack to expect the name's value. */ private void beforeName() throws IOException { int context = peekScope(); if (context == NONEMPTY_OBJECT) { // first in object sink.writeByte(','); } else if (context != EMPTY_OBJECT) { // not in an object! throw new IllegalStateException("Nesting problem."); } newline(); replaceTop(DANGLING_NAME); }
.writeByte('\n'); sink.writeUtf8(requestMethod) .writeByte('\n'); sink.writeDecimalLong(varyHeaders.size()) .writeByte('\n'); for (int i = 0, size = varyHeaders.size(); i < size; i++) { sink.writeUtf8(varyHeaders.name(i)) .writeUtf8(": ") .writeUtf8(varyHeaders.value(i)) .writeByte('\n'); .writeByte('\n'); sink.writeDecimalLong(responseHeaders.size() + 2) .writeByte('\n'); for (int i = 0, size = responseHeaders.size(); i < size; i++) { sink.writeUtf8(responseHeaders.name(i)) .writeUtf8(": ") .writeUtf8(responseHeaders.value(i)) .writeByte('\n'); .writeByte('\n'); sink.writeUtf8(RECEIVED_MILLIS) .writeUtf8(": ") .writeDecimalLong(receivedResponseMillis) .writeByte('\n'); sink.writeByte('\n'); sink.writeUtf8(handshake.cipherSuite().javaName()) .writeByte('\n');
@Test public void closeWithExceptionWhenWriting() throws IOException { MockSink mockSink = new MockSink(); mockSink.scheduleThrow(0, new IOException()); BufferedSink bufferedSink = Okio.buffer(mockSink); bufferedSink.writeByte('a'); try { bufferedSink.close(); fail(); } catch (IOException expected) { } mockSink.assertLog("write([text=a], 1)", "close()"); }
@Test public void closeWithExceptionWhenClosing() throws IOException { MockSink mockSink = new MockSink(); mockSink.scheduleThrow(1, new IOException()); BufferedSink bufferedSink = Okio.buffer(mockSink); bufferedSink.writeByte('a'); try { bufferedSink.close(); fail(); } catch (IOException expected) { } mockSink.assertLog("write([text=a], 1)", "close()"); }
@Test public void closeWithExceptionWhenWritingAndClosing() throws IOException { MockSink mockSink = new MockSink(); mockSink.scheduleThrow(0, new IOException("first")); mockSink.scheduleThrow(1, new IOException("second")); BufferedSink bufferedSink = Okio.buffer(mockSink); bufferedSink.writeByte('a'); try { bufferedSink.close(); fail(); } catch (IOException expected) { assertEquals("first", expected.getMessage()); } mockSink.assertLog("write([text=a], 1)", "close()"); }
@Test public void closeEmitsBufferedBytes() throws IOException { sink.writeByte('a'); sink.close(); assertEquals('a', data.readByte()); }
@Test public void writeLastByteInSegment() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE - 1)); sink.writeByte(0x20); sink.writeByte(0x21); sink.flush(); assertEquals(asList(SEGMENT_SIZE, 1), segmentSizes(data)); assertEquals(repeat('a', SEGMENT_SIZE - 1), data.readUtf8(SEGMENT_SIZE - 1)); assertEquals("[text= !]", data.toString()); }
@Test public void bufferedSinkFlush() throws IOException { Buffer sink = new Buffer(); BufferedSink bufferedSink = Okio.buffer((Sink) sink); bufferedSink.writeByte('a'); assertEquals(0, sink.size()); bufferedSink.flush(); assertEquals(0, bufferedSink.getBuffer().size()); assertEquals(1, sink.size()); }
@Test public void bufferedSinkEmitsTailWhenItIsComplete() throws IOException { Buffer sink = new Buffer(); BufferedSink bufferedSink = Okio.buffer((Sink) sink); bufferedSink.writeUtf8(repeat('a', SEGMENT_SIZE - 1)); assertEquals(0, sink.size()); bufferedSink.writeByte(0); assertEquals(SEGMENT_SIZE, sink.size()); assertEquals(0, bufferedSink.getBuffer().size()); }