Пример #1
0
  public static void serializeAndCommitResponse(Response response, DiskLruCache.Editor edit)
      throws IOException {
    if (edit == null) {
      return;
    }

    boolean successful = false;
    try {
      new Entry(response).writeTo(edit);
      Buffer buffer = new Buffer();
      ResponseBody body = response.body();
      if (body != null) {
        body.source().read(buffer, body.contentLength());
      }
      Sink sink = edit.newSink(RESPONSE_BODY);
      sink.write(buffer, buffer.size());
      sink.close();
      successful = true;
    } finally {
      if (successful) {
        commitQuitely(edit);
      } else {
        abortQuietly(edit);
      }
    }
  }
Пример #2
0
    private void writeTo(DiskLruCache.Editor editor) throws IOException {
      BufferedSink sink = Okio.buffer(editor.newSink(RESPONSE_METADATA));

      sink.writeUtf8(url);
      sink.writeByte('\n');
      sink.writeUtf8(requestMethod);
      sink.writeByte('\n');
      sink.writeDecimalLong(requestBody == null ? 0 : 1);
      sink.writeByte('\n');
      if (requestBody != null) {
        sink.writeUtf8(requestBody.contentType().toString());
        sink.writeByte('\n');
        sink.writeDecimalLong(requestBody.contentLength());
        sink.writeByte('\n');
        requestBody.writeTo(sink);
      }
      sink.writeDecimalLong(varyHeaders.size());
      sink.writeByte('\n');
      for (int i = 0, size = varyHeaders.size(); i < size; i++) {
        sink.writeUtf8(varyHeaders.name(i));
        sink.writeUtf8(": ");
        sink.writeUtf8(varyHeaders.value(i));
        sink.writeByte('\n');
      }

      sink.writeUtf8(new StatusLine(protocol, code, message).toString());
      sink.writeByte('\n');
      sink.writeDecimalLong(responseHeaders.size());
      sink.writeByte('\n');
      for (int i = 0, size = responseHeaders.size(); i < size; i++) {
        sink.writeUtf8(responseHeaders.name(i));
        sink.writeUtf8(": ");
        sink.writeUtf8(responseHeaders.value(i));
        sink.writeByte('\n');
      }

      if (isHttps()) {
        sink.writeByte('\n');
        sink.writeUtf8(handshake.cipherSuite());
        sink.writeByte('\n');
        writeCertList(sink, handshake.peerCertificates());
        writeCertList(sink, handshake.localCertificates());
      }
      sink.close();
    }
Пример #3
0
 private static void abortQuietly(DiskLruCache.Editor editor) {
   try {
     editor.abort();
   } catch (IOException ignore) {
   }
 }
Пример #4
0
 private static void commitQuitely(DiskLruCache.Editor editor) {
   try {
     editor.commit();
   } catch (IOException ignore) {
   }
 }