/** Returns bytes of a request header for sending on an HTTP transport. */ public void writeRequest(Headers headers, String requestLine) throws IOException { if (state != STATE_IDLE) throw new IllegalStateException("state: " + state); sink.writeUtf8(requestLine).writeUtf8("\r\n"); for (int i = 0, size = headers.size(); i < size; i++) { sink.writeUtf8(headers.name(i)) .writeUtf8(": ") .writeUtf8(headers.value(i)) .writeUtf8("\r\n"); } sink.writeUtf8("\r\n"); state = STATE_OPEN_REQUEST_BODY; }
@Override public synchronized void close() throws IOException { if (closed) return; closed = true; sink.writeUtf8("0\r\n\r\n"); detachTimeout(timeout); state = STATE_READ_RESPONSE_HEADERS; } }
@Override public void run() { try { sink.writeUtf8("Numbers\n"); sink.writeUtf8("-------\n"); for (int i = 2; i <= 997; i++) { System.out.println(i); Thread.sleep(10); sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i))); } sink.close(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } }
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; }
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()); } }
/** * 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; }
private void writeHttpResponse(Socket socket, BufferedSink sink, MockResponse response) throws IOException { sleepIfDelayed(response.getHeadersDelay(TimeUnit.MILLISECONDS)); sink.writeUtf8(response.getStatus()); sink.writeUtf8("\r\n"); writeHeaders(sink, response.getHeaders()); Buffer body = response.getBody(); if (body == null) return; sleepIfDelayed(response.getBodyDelay(TimeUnit.MILLISECONDS)); throttledTransfer(response, socket, body, sink, body.size(), false); if ("chunked".equalsIgnoreCase(response.getHeaders().get("Transfer-Encoding"))) { writeHeaders(sink, response.getTrailers()); } }
@Test public void bytesNotEmittedToSinkWithoutFlush() throws Exception { Buffer sink = new Buffer(); BufferedSink bufferedSink = Okio.buffer((Sink) sink); bufferedSink.writeUtf8("abc"); assertEquals(0, sink.size()); }
@Test public void completeSegmentsEmitted() throws Exception { Buffer sink = new Buffer(); BufferedSink bufferedSink = Okio.buffer((Sink) sink); bufferedSink.writeUtf8(repeat('a', SEGMENT_SIZE * 3)); assertEquals(SEGMENT_SIZE * 3, sink.size()); }
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 longHexStringTooShortThrows() throws IOException { try { sink.writeUtf8(" "); sink.emit(); source.readHexadecimalUnsignedLong(); fail(); } catch (NumberFormatException e) { assertEquals("Expected leading [0-9a-fA-F] character but was 0x20", e.getMessage()); } }
@Test public void writeNothing() throws IOException { sink.writeUtf8(""); sink.flush(); assertEquals(0, data.size()); }
@Test public void readWriteFile() throws Exception { File file = temporaryFolder.newFile(); BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeUtf8("Hello, java.io file!"); sink.close(); assertTrue(file.exists()); assertEquals(20, file.length()); BufferedSource source = Okio.buffer(Okio.source(file)); assertEquals("Hello, java.io file!", source.readUtf8()); source.close(); }
@Test public void readFullyTooShortThrows() throws IOException { sink.writeUtf8("Hi"); sink.emit(); Buffer sink = new Buffer(); try { source.readFully(sink, 5); fail(); } catch (EOFException ignored) { } // Verify we read all that we could from the source. assertEquals("Hi", sink.readUtf8()); }
@Test public void readUtf8TooShortThrows() throws IOException { sink.writeUtf8("abc"); sink.emit(); try { source.readUtf8(4L); fail(); } catch (EOFException expected) { } assertEquals("abc", source.readUtf8()); // The read shouldn't consume any data. }
@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()); }
@Test public void selectNotFound() throws IOException { Options options = Options.Companion.of( ByteString.encodeUtf8("ROCK"), ByteString.encodeUtf8("SCISSORS"), ByteString.encodeUtf8("PAPER")); sink.writeUtf8("SPOCK"); sink.emit(); assertEquals(-1, source.select(options)); assertEquals("SPOCK", source.readUtf8()); }