public void close() { for (Source in : sources) { Util.closeQuietly(in); } } }
public void cancel() { // Close the raw socket so we don't end up doing synchronous I/O. closeQuietly(rawSocket); }
@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); } } }
@Override public void close() { Util.closeQuietly(source()); }
@Override public void abort() { synchronized (Cache.this) { if (done) { return; } done = true; writeAbortCount++; } Util.closeQuietly(cacheOut); try { editor.abort(); } catch (IOException ignored) { } }
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); } }
/** 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()); } }
@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(); }
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; }
@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); } }
/** 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); } }
@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; }
@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); } }
Util.closeQuietly(sources[i]); } else { break;
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); } } }
/** * 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); } }
closeQuietly(socket); if (releasedConnection != null) { eventListener.connectionReleased(call, releasedConnection);
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); } }
webSocket.failWebSocket(e, null); } finally { closeQuietly(source);
@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); } }