本文整理了Java中okhttp3.internal.Util.closeQuietly()
方法的一些代码示例,展示了Util.closeQuietly()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Util.closeQuietly()
方法的具体详情如下:
包路径:okhttp3.internal.Util
类名称:Util
方法名:closeQuietly
[英]Closes closeable, ignoring any checked exceptions. Does nothing if closeable is null.
[中]关闭closeable,忽略所有选中的异常。如果closeable为null,则不执行任何操作。
代码示例来源:origin: square/okhttp
public void close() {
for (Source in : sources) {
Util.closeQuietly(in);
}
}
}
代码示例来源:origin: square/okhttp
public void cancel() {
// Close the raw socket so we don't end up doing synchronous I/O.
closeQuietly(rawSocket);
}
代码示例来源:origin: square/okhttp
@Override public void close() throws IOException {
if (fileOperator == null) return; // Already closed.
fileOperator = null;
RandomAccessFile fileToClose = null;
synchronized (Relay.this) {
sourceCount--;
if (sourceCount == 0) {
fileToClose = file;
file = null;
}
}
if (fileToClose != null) {
closeQuietly(fileToClose);
}
}
}
代码示例来源:origin: square/okhttp
@Override public void close() {
Util.closeQuietly(source());
}
代码示例来源:origin: square/okhttp
@Override public void abort() {
synchronized (Cache.this) {
if (done) {
return;
}
done = true;
writeAbortCount++;
}
Util.closeQuietly(cacheOut);
try {
editor.abort();
} catch (IOException ignored) {
}
}
代码示例来源:origin: square/okhttp
public void failWebSocket(Exception e, @Nullable Response response) {
Streams streamsToClose;
synchronized (this) {
if (failed) return; // Already failed.
failed = true;
streamsToClose = this.streams;
this.streams = null;
if (cancelFuture != null) cancelFuture.cancel(false);
if (executor != null) executor.shutdown();
}
try {
listener.onFailure(this, e, response);
} finally {
closeQuietly(streamsToClose);
}
}
代码示例来源:origin: square/okhttp
/** Close and remove all idle connections in the pool. */
public void evictAll() {
List<RealConnection> evictedConnections = new ArrayList<>();
synchronized (this) {
for (Iterator<RealConnection> i = connections.iterator(); i.hasNext(); ) {
RealConnection connection = i.next();
if (connection.allocations.isEmpty()) {
connection.noNewStreams = true;
evictedConnections.add(connection);
i.remove();
}
}
}
for (RealConnection connection : evictedConnections) {
closeQuietly(connection.socket());
}
}
代码示例来源:origin: square/okhttp
@Override protected void execute() {
try {
logger.info(MockWebServer.this + " starting to accept connections");
acceptConnections();
} catch (Throwable e) {
logger.log(Level.WARNING, MockWebServer.this + " failed unexpectedly", e);
}
// Release all sockets and all threads, even if any close fails.
closeQuietly(serverSocket);
for (Iterator<Socket> s = openClientSockets.iterator(); s.hasNext(); ) {
closeQuietly(s.next());
s.remove();
}
for (Iterator<Http2Connection> s = openConnections.iterator(); s.hasNext(); ) {
closeQuietly(s.next());
s.remove();
}
dispatcher.shutdown();
executor.shutdown();
}
代码示例来源:origin: square/okhttp
void commit(long upstreamSize) throws IOException {
// Write metadata to the end of the file.
writeMetadata(upstreamSize);
file.getChannel().force(false);
// Once everything else is in place we can swap the dirty header for a clean one.
writeHeader(PREFIX_CLEAN, upstreamSize, metadata.size());
file.getChannel().force(false);
// This file is complete.
synchronized (Relay.this) {
complete = true;
}
closeQuietly(upstream);
upstream = null;
}
代码示例来源:origin: square/okhttp
@Override public void onReadClose(int code, String reason) {
if (code == -1) throw new IllegalArgumentException();
Streams toClose = null;
synchronized (this) {
if (receivedCloseCode != -1) throw new IllegalStateException("already closed");
receivedCloseCode = code;
receivedCloseReason = reason;
if (enqueuedClose && messageAndCloseQueue.isEmpty()) {
toClose = this.streams;
this.streams = null;
if (cancelFuture != null) cancelFuture.cancel(false);
this.executor.shutdown();
}
}
try {
listener.onClosing(this, code, reason);
if (toClose != null) {
listener.onClosed(this, code, reason);
}
} finally {
closeQuietly(toClose);
}
}
代码示例来源:origin: square/okhttp
/** Forbid new streams from being created on the connection that hosts this allocation. */
public void noNewStreams() {
Socket socket;
Connection releasedConnection;
synchronized (connectionPool) {
releasedConnection = connection;
socket = deallocate(true, false, false);
if (connection != null) releasedConnection = null;
}
closeQuietly(socket);
if (releasedConnection != null) {
eventListener.connectionReleased(call, releasedConnection);
}
}
代码示例来源:origin: square/okhttp
@Nullable Response get(Request request) {
String key = key(request.url());
DiskLruCache.Snapshot snapshot;
Entry entry;
try {
snapshot = cache.get(key);
if (snapshot == null) {
return null;
}
} catch (IOException e) {
// Give up because the cache cannot be read.
return null;
}
try {
entry = new Entry(snapshot.getSource(ENTRY_METADATA));
} catch (IOException e) {
Util.closeQuietly(snapshot);
return null;
}
Response response = entry.response(snapshot);
if (!entry.matches(request, response)) {
Util.closeQuietly(response.body());
return null;
}
return response;
}
代码示例来源:origin: square/okhttp
@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);
}
}
代码示例来源:origin: square/okhttp
Util.closeQuietly(sources[i]);
} else {
break;
代码示例来源:origin: square/okhttp
public void release(boolean callEnd) {
Socket socket;
Connection releasedConnection;
synchronized (connectionPool) {
releasedConnection = connection;
socket = deallocate(false, true, false);
if (connection != null) releasedConnection = null;
}
closeQuietly(socket);
if (releasedConnection != null) {
if (callEnd) {
Internal.instance.timeoutExit(call, null);
}
eventListener.connectionReleased(call, releasedConnection);
if (callEnd) {
eventListener.callEnd(call);
}
}
}
代码示例来源:origin: square/okhttp
/**
* Does all the work to build an HTTPS connection over a proxy tunnel. The catch here is that a
* proxy server can issue an auth challenge and then close the connection.
*/
private void connectTunnel(int connectTimeout, int readTimeout, int writeTimeout, Call call,
EventListener eventListener) throws IOException {
Request tunnelRequest = createTunnelRequest();
HttpUrl url = tunnelRequest.url();
for (int i = 0; i < MAX_TUNNEL_ATTEMPTS; i++) {
connectSocket(connectTimeout, readTimeout, call, eventListener);
tunnelRequest = createTunnel(readTimeout, writeTimeout, tunnelRequest, url);
if (tunnelRequest == null) break; // Tunnel successfully created.
// The proxy decided to close the connection after an auth challenge. We need to create a new
// connection, but this time with the auth credentials.
closeQuietly(rawSocket);
rawSocket = null;
sink = null;
source = null;
eventListener.connectEnd(call, route.socketAddress(), route.proxy(), null);
}
}
代码示例来源:origin: square/okhttp
closeQuietly(socket);
if (releasedConnection != null) {
eventListener.connectionReleased(call, releasedConnection);
代码示例来源:origin: square/okhttp
public void streamFinished(boolean noNewStreams, HttpCodec codec, long bytesRead, IOException e) {
eventListener.responseBodyEnd(call, bytesRead);
Socket socket;
Connection releasedConnection;
boolean callEnd;
synchronized (connectionPool) {
if (codec == null || codec != this.codec) {
throw new IllegalStateException("expected " + this.codec + " but was " + codec);
}
if (!noNewStreams) {
connection.successCount++;
}
releasedConnection = connection;
socket = deallocate(noNewStreams, false, true);
if (connection != null) releasedConnection = null;
callEnd = this.released;
}
closeQuietly(socket);
if (releasedConnection != null) {
eventListener.connectionReleased(call, releasedConnection);
}
if (e != null) {
e = Internal.instance.timeoutExit(call, e);
eventListener.callFailed(call, e);
} else if (callEnd) {
Internal.instance.timeoutExit(call, null);
eventListener.callEnd(call);
}
}
代码示例来源:origin: square/okhttp
webSocket.failWebSocket(e, null);
} finally {
closeQuietly(source);
代码示例来源:origin: square/okhttp
@Override public void onResponse(Call call, Response response) {
StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);
try {
checkResponse(response);
} catch (ProtocolException e) {
failWebSocket(e, response);
closeQuietly(response);
streamAllocation.streamFailed(e);
return;
}
// Promote the HTTP streams into web socket streams.
streamAllocation.noNewStreams(); // Prevent connection pooling!
Streams streams = streamAllocation.connection().newWebSocketStreams(streamAllocation);
// Process all web socket messages.
try {
String name = "OkHttp WebSocket " + request.url().redact();
initReaderAndWriter(name, streams);
listener.onOpen(RealWebSocket.this, response);
streamAllocation.connection().socket().setSoTimeout(0);
loopReader();
} catch (Exception e) {
failWebSocket(e, null);
}
}
内容来源于网络,如有侵权,请联系作者删除!