Search in sources :

Example 76 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class DiskLruCache method newJournalWriter.

private BufferedSink newJournalWriter() throws FileNotFoundException {
    Sink fileSink = fileSystem.appendingSink(journalFile);
    Sink faultHidingSink = new FaultHidingSink(fileSink) {

        @Override
        protected void onException(IOException e) {
            assert (Thread.holdsLock(DiskLruCache.this));
            hasJournalErrors = true;
        }
    };
    return Okio.buffer(faultHidingSink);
}
Also used : Sink(okio.Sink) BufferedSink(okio.BufferedSink) IOException(java.io.IOException)

Example 77 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class DiskLruCache method rebuildJournal.

/**
   * Creates a new journal that omits redundant information. This replaces the current journal if it
   * exists.
   */
synchronized void rebuildJournal() throws IOException {
    if (journalWriter != null) {
        journalWriter.close();
    }
    BufferedSink writer = Okio.buffer(fileSystem.sink(journalFileTmp));
    try {
        writer.writeUtf8(MAGIC).writeByte('\n');
        writer.writeUtf8(VERSION_1).writeByte('\n');
        writer.writeDecimalLong(appVersion).writeByte('\n');
        writer.writeDecimalLong(valueCount).writeByte('\n');
        writer.writeByte('\n');
        for (Entry entry : lruEntries.values()) {
            if (entry.currentEditor != null) {
                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');
            }
        }
    } finally {
        writer.close();
    }
    if (fileSystem.exists(journalFile)) {
        fileSystem.rename(journalFile, journalFileBackup);
    }
    fileSystem.rename(journalFileTmp, journalFile);
    fileSystem.delete(journalFileBackup);
    journalWriter = newJournalWriter();
    hasJournalErrors = false;
    mostRecentRebuildFailed = false;
}
Also used : BufferedSink(okio.BufferedSink)

Example 78 with BufferedSink

use of okio.BufferedSink in project wire by square.

the class ServiceGeneratorTest method schema.

private Schema schema(Map<String, String> fileToProto) throws IOException {
    SchemaLoader schemaLoader = new SchemaLoader();
    schemaLoader.addSource(temporaryFolder.getRoot());
    for (Map.Entry<String, String> entry : fileToProto.entrySet()) {
        File file = new File(temporaryFolder.getRoot(), entry.getKey());
        file.getParentFile().mkdirs();
        try (BufferedSink out = Okio.buffer(Okio.sink(file))) {
            out.writeUtf8(entry.getValue());
        }
        schemaLoader.addProto(entry.getKey());
    }
    return schemaLoader.load();
}
Also used : SchemaLoader(com.squareup.wire.schema.SchemaLoader) BufferedSink(okio.BufferedSink) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Example 79 with BufferedSink

use of okio.BufferedSink in project wire by square.

the class ProtoAdapter method encode.

/** Encode {@code value} and write it to {@code stream}. */
public final void encode(OutputStream stream, E value) throws IOException {
    checkNotNull(value, "value == null");
    checkNotNull(stream, "stream == null");
    BufferedSink buffer = Okio.buffer(Okio.sink(stream));
    encode(buffer, value);
    buffer.emit();
}
Also used : BufferedSink(okio.BufferedSink)

Example 80 with BufferedSink

use of okio.BufferedSink in project u2020 by JakeWharton.

the class SocketActivityHierarchyServer method writeValue.

private static boolean writeValue(Socket client, String value) {
    boolean result;
    BufferedSink out = null;
    try {
        out = Okio.buffer(Okio.sink(client));
        out.writeUtf8(value);
        out.writeByte('\n');
        out.flush();
        result = true;
    } catch (Exception e) {
        result = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                result = false;
            }
        }
    }
    return result;
}
Also used : BufferedSink(okio.BufferedSink) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

BufferedSink (okio.BufferedSink)84 Test (org.junit.Test)31 IOException (java.io.IOException)27 Buffer (okio.Buffer)18 File (java.io.File)13 RequestBody (okhttp3.RequestBody)13 Source (okio.Source)11 Response (okhttp3.Response)10 GzipSink (okio.GzipSink)10 Request (okhttp3.Request)9 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)9 BufferedSource (okio.BufferedSource)9 MediaType (okhttp3.MediaType)7 InterruptedIOException (java.io.InterruptedIOException)6 MockResponse (okhttp3.mockwebserver.MockResponse)5 Sink (okio.Sink)5 InputStream (java.io.InputStream)4 OkHttpClient (okhttp3.OkHttpClient)4 Request (com.squareup.okhttp.Request)3 Socket (java.net.Socket)3