Java Code Examples for okhttp3.internal.Util#closeQuietly()

The following examples show how to use okhttp3.internal.Util#closeQuietly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestBody.java    From AndroidProjects with MIT License 6 votes vote down vote up
/** Returns a new request body that transmits the content of {@code file}. */
public static RequestBody create(final MediaType contentType, final File file) {
  if (file == null) throw new NullPointerException("content == null");

  return new RequestBody() {
    @Override public MediaType contentType() {
      return contentType;
    }

    @Override public long contentLength() {
      return file.length();
    }

    @Override public void writeTo(BufferedSink sink) throws IOException {
      Source source = null;
      try {
        source = Okio.source(file);
        sink.writeAll(source);
      } finally {
        Util.closeQuietly(source);
      }
    }
  };
}
 
Example 2
Source File: FileProgressRequestBody.java    From tysq-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void writeTo(BufferedSink sink) throws IOException {
    Source source = null;
    try {
        source = Okio.source(file);
        long total = 0;
        long read;

        while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) {
            total += read;
            sink.flush();
            this.listener.onTransferred(total);

        }
    } finally {
        Util.closeQuietly(source);
    }
}
 
Example 3
Source File: CountingFileRequestBody.java    From 4pdaClient-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(@NotNull BufferedSink sink) throws IOException {
    Source source = null;
    try {
        source = Okio.source(file);
        long total = 0;
        long read;

        while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) {
            total += read;
            sink.flush();
            this.listener.transferred(total);

        }
    } finally {
        if (source != null)
            Util.closeQuietly(source);
    }
}
 
Example 4
Source File: Http2Connection.java    From styT with Apache License 2.0 6 votes vote down vote up
@Override protected void execute() {
  ErrorCode connectionErrorCode = ErrorCode.INTERNAL_ERROR;
  ErrorCode streamErrorCode = ErrorCode.INTERNAL_ERROR;
  try {
    reader.readConnectionPreface(this);
    while (reader.nextFrame(false, this)) {
    }
    connectionErrorCode = ErrorCode.NO_ERROR;
    streamErrorCode = ErrorCode.CANCEL;
  } catch (IOException e) {
    connectionErrorCode = ErrorCode.PROTOCOL_ERROR;
    streamErrorCode = ErrorCode.PROTOCOL_ERROR;
  } finally {
    try {
      close(connectionErrorCode, streamErrorCode);
    } catch (IOException ignored) {
    }
    Util.closeQuietly(reader);
  }
}
 
Example 5
Source File: DiskLruCache.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a snapshot of this entry. This opens all streams eagerly to guarantee that we see a
 * single published snapshot. If we opened streams lazily then the streams could come from
 * different edits.
 */
Snapshot snapshot() {
  if (!Thread.holdsLock(DiskLruCache.this)) throw new AssertionError();

  Source[] sources = new Source[valueCount];
  long[] lengths = this.lengths.clone(); // Defensive copy since these can be zeroed out.
  try {
    for (int i = 0; i < valueCount; i++) {
      sources[i] = fileSystem.source(cleanFiles[i]);
    }
    return new Snapshot(key, sequenceNumber, sources, lengths);
  } catch (FileNotFoundException e) {
    // A file must have been deleted manually!
    for (int i = 0; i < valueCount; i++) {
      if (sources[i] != null) {
        Util.closeQuietly(sources[i]);
      } else {
        break;
      }
    }
    // Since the entry is no longer valid, remove it so the metadata is accurate (i.e. the cache
    // size.)
    try {
      removeEntry(this);
    } catch (IOException ignored) {
    }
    return null;
  }
}
 
Example 6
Source File: QiscusApi.java    From qiscus-sdk-android with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException {
    numWriteToCall++;

    Source source = null;
    try {
        source = Okio.source(file);
        long total = 0;
        long read;

        while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) {
            total += read;
            sink.flush();

            /**
             * When we use HttpLoggingInterceptor,
             * we have issue with progress update not valid.
             * So we must check, first call is to HttpLoggingInterceptor
             * second call is to request
             */
            if (numWriteToCall > IGNORE_FIRST_NUMBER_OF_WRITE_TO_CALL) {
                progressListener.onProgress(total);
            }

        }
    } finally {
        Util.closeQuietly(source);
    }
}
 
Example 7
Source File: InputStreamUploadBody.java    From qingstor-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(BufferedSink sink) throws IOException {

    if (contentLength > 0) {
        writeWithContentLength(sink, offset);
    } else {
        writeAll(sink);
    }
    sink.flush();
    Util.closeQuietly(file);
}
 
Example 8
Source File: DiskLruCache.java    From AndroidProjects with MIT License 5 votes vote down vote up
/**
 * Returns a snapshot of this entry. This opens all streams eagerly to guarantee that we see a
 * single published snapshot. If we opened streams lazily then the streams could come from
 * different edits.
 */
Snapshot snapshot() {
  if (!Thread.holdsLock(DiskLruCache.this)) throw new AssertionError();

  Source[] sources = new Source[valueCount];
  long[] lengths = this.lengths.clone(); // Defensive copy since these can be zeroed out.
  try {
    for (int i = 0; i < valueCount; i++) {
      sources[i] = fileSystem.source(cleanFiles[i]);
    }
    return new Snapshot(key, sequenceNumber, sources, lengths);
  } catch (FileNotFoundException e) {
    // A file must have been deleted manually!
    for (int i = 0; i < valueCount; i++) {
      if (sources[i] != null) {
        Util.closeQuietly(sources[i]);
      } else {
        break;
      }
    }
    // Since the entry is no longer valid, remove it so the metadata is accurate (i.e. the cache
    // size.)
    try {
      removeEntry(this);
    } catch (IOException ignored) {
    }
    return null;
  }
}
 
Example 9
Source File: OkHttpConverters.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
static okhttp3.RequestBody toOkHttpRequestBody(RequestBody body) {
    Object object = body.get();
    if (object instanceof InputStream) {
        InputStream inputStream = (InputStream) object;

        return new okhttp3.RequestBody() {
            @Override
            public MediaType contentType() {
                return null;
            }

            @Override
            public long contentLength() {
                return -1L;
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                Source source = null;
                try {
                    source = Okio.source(inputStream);
                    sink.writeAll(source);
                } finally {
                    Util.closeQuietly(source);
                }
            }
        };

    } else if (object instanceof String) {
        String string = (String) object;
        return okhttp3.RequestBody.create(null, string);
    } else {
        return null;
    }
}
 
Example 10
Source File: InputStreamRequestBody.java    From domo-java-sdk with MIT License 5 votes vote down vote up
@Override
public void writeTo(BufferedSink sink) throws IOException {
    Source source = null;
    try {
        source = Okio.source(inputStream);
        sink.writeAll(source);
    } finally {
        Util.closeQuietly(source);
    }
}
 
Example 11
Source File: RequestBodyUtils.java    From RxEasyHttp with Apache License 2.0 5 votes vote down vote up
public static RequestBody create(final MediaType mediaType, final InputStream inputStream) {
    return new RequestBody() {
        @Override
        public MediaType contentType() {
            return mediaType;
        }

        @Override
        public long contentLength() {
            try {
                return inputStream.available();
            } catch (IOException e) {
                return 0;
            }
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            Source source = null;
            try {
                source = Okio.source(inputStream);
                sink.writeAll(source);
            } finally {
                Util.closeQuietly(source);
            }
        }
    };
}
 
Example 12
Source File: RequestBodyUtil.java    From react-native-GPay with MIT License 5 votes vote down vote up
/**
 * Creates a RequestBody from a mediaType and inputStream given.
 */
public static RequestBody create(final MediaType mediaType, final InputStream inputStream) {
  return new RequestBody() {
    @Override
    public MediaType contentType() {
      return mediaType;
    }

    @Override
    public long contentLength() {
      try {
        return inputStream.available();
      } catch (IOException e) {
        return 0;
      }
    }

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
      Source source = null;
      try {
        source = Okio.source(inputStream);
        sink.writeAll(source);
      } finally {
        Util.closeQuietly(source);
      }
    }
  };
}
 
Example 13
Source File: DiskLruCache.java    From styT with Apache License 2.0 5 votes vote down vote up
private void readJournal() throws IOException {
  BufferedSource source = Okio.buffer(fileSystem.source(journalFile));
  try {
    String magic = source.readUtf8LineStrict();
    String version = source.readUtf8LineStrict();
    String appVersionString = source.readUtf8LineStrict();
    String valueCountString = source.readUtf8LineStrict();
    String blank = source.readUtf8LineStrict();
    if (!MAGIC.equals(magic)
        || !VERSION_1.equals(version)
        || !Integer.toString(appVersion).equals(appVersionString)
        || !Integer.toString(valueCount).equals(valueCountString)
        || !"".equals(blank)) {
      throw new IOException("unexpected journal header: [" + magic + ", " + version + ", "
          + valueCountString + ", " + blank + "]");
    }

    int lineCount = 0;
    while (true) {
      try {
        readJournalLine(source.readUtf8LineStrict());
        lineCount++;
      } catch (EOFException endOfJournal) {
        break;
      }
    }
    redundantOpCount = lineCount - lruEntries.size();

    // If we ended on a truncated line, rebuild the journal before appending to it.
    if (!source.exhausted()) {
      rebuildJournal();
    } else {
      journalWriter = newJournalWriter();
    }
  } finally {
    Util.closeQuietly(source);
  }
}
 
Example 14
Source File: Cache.java    From styT with Apache License 2.0 5 votes vote down vote up
@Override public void abort() {
  synchronized (Cache.this) {
    if (done) {
      return;
    }
    done = true;
    writeAbortCount++;
  }
  Util.closeQuietly(cacheOut);
  try {
    editor.abort();
  } catch (IOException ignored) {
  }
}
 
Example 15
Source File: OkHttpRequestBodyUtil.java    From spring-microservice-exam with MIT License 5 votes vote down vote up
/**
 * @param
 * @return
 * @author tangyi
 * @date 2018/9/9 10:22
 */
public static RequestBody create(final MediaType mediaType, final InputStream inputStream) {
    return new RequestBody() {
        @Override
        public MediaType contentType() {
            return mediaType;
        }

        @Override
        public long contentLength() {
            try {
                return inputStream.available();
            } catch (IOException e) {
                return 0;
            }
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            Source source = null;
            try {
                source = Okio.source(inputStream);
                sink.writeAll(source);
            } finally {
                Util.closeQuietly(source);
            }
        }
    };
}
 
Example 16
Source File: HttpResponse.java    From mica with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void close() throws IOException {
	Util.closeQuietly(this.body);
}
 
Example 17
Source File: DiskLruCache.java    From styT with Apache License 2.0 4 votes vote down vote up
public void close() {
  for (Source in : sources) {
    Util.closeQuietly(in);
  }
}
 
Example 18
Source File: ResponseBody.java    From styT with Apache License 2.0 4 votes vote down vote up
@Override public void close() {
  Util.closeQuietly(source());
}
 
Example 19
Source File: DiskLruCache.java    From AndroidProjects with MIT License 4 votes vote down vote up
public void close() {
  for (Source in : sources) {
    Util.closeQuietly(in);
  }
}
 
Example 20
Source File: ResponseBody.java    From AndroidProjects with MIT License 4 votes vote down vote up
@Override public void close() {
  Util.closeQuietly(source());
}