本文整理匯總了Java中com.squareup.okhttp.internal.Util.closeQuietly方法的典型用法代碼示例。如果您正苦於以下問題:Java Util.closeQuietly方法的具體用法?Java Util.closeQuietly怎麽用?Java Util.closeQuietly使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.squareup.okhttp.internal.Util
的用法示例。
在下文中一共展示了Util.closeQuietly方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: create
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
public static RequestBody create(final MediaType contentType, final File file) {
if (file != null) {
return new RequestBody() {
public MediaType contentType() {
return contentType;
}
public long contentLength() {
return file.length();
}
public void writeTo(BufferedSink sink) throws IOException {
Closeable source = null;
try {
source = Okio.source(file);
sink.writeAll(source);
} finally {
Util.closeQuietly(source);
}
}
};
}
throw new NullPointerException("content == null");
}
示例2: execute
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
protected void execute() {
ErrorCode connectionErrorCode = ErrorCode.INTERNAL_ERROR;
ErrorCode streamErrorCode = ErrorCode.INTERNAL_ERROR;
try {
if (!FramedConnection.this.client) {
this.frameReader.readConnectionPreface();
}
while (true) {
if (!this.frameReader.nextFrame(this)) {
break;
}
}
connectionErrorCode = ErrorCode.NO_ERROR;
streamErrorCode = ErrorCode.CANCEL;
} catch (IOException e) {
connectionErrorCode = ErrorCode.PROTOCOL_ERROR;
streamErrorCode = ErrorCode.PROTOCOL_ERROR;
} finally {
try {
FramedConnection.this.close(connectionErrorCode, streamErrorCode);
} catch (IOException e2) {
}
Util.closeQuietly(this.frameReader);
}
}
示例3: close
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
@Override
public void close() throws IOException {
if (!mClosed) {
Util.closeQuietly(mFileOutputStream);
mByteArrayOutputStream.reset();
mClosed = true;
}
}
示例4: disconnect
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
@Override public final void disconnect() {
// Calling disconnect() before a connection exists should have no effect.
if (httpEngine != null) {
// We close the response body here instead of in
// HttpEngine.release because that is called when input
// has been completely read from the underlying socket.
// However the response body can be a GZIPInputStream that
// still has unread data.
if (httpEngine.hasResponse()) {
Util.closeQuietly(httpEngine.getResponseBody());
}
httpEngine.release(true);
}
}
示例5: abort
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
public void abort() {
synchronized (Cache.this) {
if (this.done) {
return;
}
this.done = true;
Cache.this.writeAbortCount = Cache.this.writeAbortCount + 1;
Util.closeQuietly(this.cacheOut);
try {
this.editor.abort();
} catch (IOException e) {
}
}
}
示例6: recycle
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
/**
* Gives {@code connection} to the pool. The pool may store the connection,
* or close it, as its policy describes.
*
* <p>It is an error to use {@code connection} after calling this method.
*/
public void recycle(Connection connection) {
if (connection.isSpdy()) {
return;
}
if (!connection.isAlive()) {
Util.closeQuietly(connection);
return;
}
try {
Platform.get().untagSocket(connection.getSocket());
} catch (SocketException e) {
// When unable to remove tagging, skip recycling and close.
Platform.get().logW("Unable to untagSocket(): " + e);
Util.closeQuietly(connection);
return;
}
synchronized (this) {
connections.addFirst(connection);
connection.resetIdleStartTime();
}
executorService.submit(connectionsCleanupCallable);
}
示例7: abort
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
@Override public void abort() {
synchronized (HttpResponseCache.this) {
if (done) {
return;
}
done = true;
writeAbortCount++;
}
Util.closeQuietly(cacheOut);
try {
editor.abort();
} catch (IOException ignored) {
}
}
示例8: cleanup
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
long cleanup(long now) {
int inUseConnectionCount = 0;
int idleConnectionCount = 0;
RealConnection longestIdleConnection = null;
long longestIdleDurationNs = Long.MIN_VALUE;
synchronized (this) {
for (RealConnection connection : this.connections) {
if (pruneAndGetAllocationCount(connection, now) > 0) {
inUseConnectionCount++;
} else {
idleConnectionCount++;
long idleDurationNs = now - connection.idleAtNanos;
if (idleDurationNs > longestIdleDurationNs) {
longestIdleDurationNs = idleDurationNs;
longestIdleConnection = connection;
}
}
}
if (longestIdleDurationNs >= this.keepAliveDurationNs || idleConnectionCount > this
.maxIdleConnections) {
this.connections.remove(longestIdleConnection);
Util.closeQuietly(longestIdleConnection.getSocket());
return 0;
} else if (idleConnectionCount > 0) {
r10 = this.keepAliveDurationNs - longestIdleDurationNs;
return r10;
} else if (inUseConnectionCount > 0) {
r10 = this.keepAliveDurationNs;
return r10;
} else {
return -1;
}
}
}
示例9: evictAll
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
/** Close and remove all connections in the pool. */
public void evictAll() {
List<Connection> connections;
synchronized (this) {
connections = new ArrayList<Connection>(this.connections);
this.connections.clear();
}
for (Connection connection : connections) {
Util.closeQuietly(connection);
}
}
示例10: close
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
public StreamAllocation close() {
if (this.bufferedRequestBody != null) {
Util.closeQuietly(this.bufferedRequestBody);
} else if (this.requestBodyOut != null) {
Util.closeQuietly(this.requestBodyOut);
}
if (this.userResponse != null) {
Util.closeQuietly(this.userResponse.body());
} else {
this.streamAllocation.connectionFailed();
}
return this.streamAllocation;
}
示例11: deallocate
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
private void deallocate(boolean noNewStreams, boolean released, boolean streamFinished) {
RealConnection connectionToClose = null;
synchronized (this.connectionPool) {
if (streamFinished) {
this.stream = null;
}
if (released) {
this.released = true;
}
if (this.connection != null) {
if (noNewStreams) {
this.connection.noNewStreams = true;
}
if (this.stream == null && (this.released || this.connection.noNewStreams)) {
release(this.connection);
if (this.connection.streamCount > 0) {
this.routeSelector = null;
}
if (this.connection.allocations.isEmpty()) {
this.connection.idleAtNanos = System.nanoTime();
if (Internal.instance.connectionBecameIdle(this.connectionPool, this
.connection)) {
connectionToClose = this.connection;
}
}
this.connection = null;
}
}
}
if (connectionToClose != null) {
Util.closeQuietly(connectionToClose.getSocket());
}
}
示例12: get
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
/** Returns a recycled connection to {@code address}, or null if no such connection exists. */
public synchronized Connection get(Address address) {
Connection foundConnection = null;
for (ListIterator<Connection> i = connections.listIterator(connections.size());
i.hasPrevious(); ) {
Connection connection = i.previous();
if (!connection.getRoute().getAddress().equals(address)
|| !connection.isAlive()
|| System.nanoTime() - connection.getIdleStartTimeNs() >= keepAliveDurationNs) {
continue;
}
i.remove();
if (!connection.isSpdy()) {
try {
Platform.get().tagSocket(connection.getSocket());
} catch (SocketException e) {
Util.closeQuietly(connection);
// When unable to tag, skip recycling and close
Platform.get().logW("Unable to tagSocket(): " + e);
continue;
}
}
foundConnection = connection;
break;
}
if (foundConnection != null && foundConnection.isSpdy()) {
connections.addFirst(foundConnection); // Add it back after iteration.
}
executorService.submit(connectionsCleanupCallable);
return foundConnection;
}
示例13: initResponseSource
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
/**
* Initialize the source for this response. It may be corrected later if the
* request headers forbids network use.
*/
private void initResponseSource() throws IOException {
responseSource = ResponseSource.NETWORK;
if (!policy.getUseCaches()) return;
OkResponseCache responseCache = client.getOkResponseCache();
if (responseCache == null) return;
CacheResponse candidate = responseCache.get(
uri, method, requestHeaders.getHeaders().toMultimap(false));
if (candidate == null) return;
Map<String, List<String>> responseHeadersMap = candidate.getHeaders();
cachedResponseBody = candidate.getBody();
if (!acceptCacheResponseType(candidate)
|| responseHeadersMap == null
|| cachedResponseBody == null) {
Util.closeQuietly(cachedResponseBody);
return;
}
RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(responseHeadersMap, true);
cachedResponseHeaders = new ResponseHeaders(uri, rawResponseHeaders);
long now = System.currentTimeMillis();
this.responseSource = cachedResponseHeaders.chooseResponseSource(now, requestHeaders);
if (responseSource == ResponseSource.CACHE) {
this.cacheResponse = candidate;
setResponse(cachedResponseHeaders, cachedResponseBody);
} else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
this.cacheResponse = candidate;
} else if (responseSource == ResponseSource.NETWORK) {
Util.closeQuietly(cachedResponseBody);
} else {
throw new AssertionError();
}
}
示例14: sendRequest
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
/**
* Figures out what the response source will be, and opens a socket to that
* source if necessary. Prepares the request headers and gets ready to start
* writing the request body if it exists.
*/
public final void sendRequest() throws IOException {
if (responseSource != null) {
return;
}
prepareRawRequestHeaders();
initResponseSource();
OkResponseCache responseCache = client.getOkResponseCache();
if (responseCache != null) {
responseCache.trackResponse(responseSource);
}
// The raw response source may require the network, but the request
// headers may forbid network use. In that case, dispose of the network
// response and use a GATEWAY_TIMEOUT response instead, as specified
// by http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
Util.closeQuietly(cachedResponseBody);
}
this.responseSource = ResponseSource.CACHE;
this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders(), true);
setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
}
if (responseSource.requiresConnection()) {
sendSocketRequest();
} else if (connection != null) {
client.getConnectionPool().recycle(connection);
connection = null;
}
}
示例15: flush
import com.squareup.okhttp.internal.Util; //導入方法依賴的package包/類
@Override
public void flush() throws TTransportException {
Util.closeQuietly(mResponseBody);
mResponseBody = null;
RequestBody requestBody = new RequestBody() {
@Override
public MediaType contentType() {
if (mHeaders != null && mHeaders.containsKey("Content-Type")) {
return MediaType.parse(mHeaders.get("Content-Type"));
} else {
return MEDIA_TYPE_THRIFT;
}
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write(mByteStore.getData(), 0, mByteStore.getBytesWritten());
}
};
try {
Request.Builder builder = new Request.Builder()
.url(mUrl)
.post(requestBody);
if (mHeaders != null) {
for (String name : mHeaders.keySet()) {
builder.header(name, mHeaders.get(name));
}
}
Response response = mHttpClient.newCall(builder.build()).execute();
if (response.code() != 200) {
throw new TTransportException("HTTP Response code: " + response.code() + ", message " + response.message());
}
mResponseBody = response.body().byteStream();
} catch (Exception e) {
throw new TTransportException(e);
} finally {
try {
mByteStore.reset();
} catch (IOException ignored) {
}
}
}