private void writeInternal(ByteBuffer buffer) throws IOException {
            Platform.blockGuardOnNetwork();
            checkOpen();
            init();

            // Need to loop through at least once to enable handshaking where no application
            // bytes are processed.
            int len = buffer.remaining();
            SSLEngineResult engineResult;
            do {
                target.clear();
                engineResult = engine.wrap(buffer, target);
                if (engineResult.getStatus() != OK && engineResult.getStatus() != CLOSED) {
                    throw new SSLException("Unexpected engine result " + engineResult.getStatus());
                }
                if (target.position() != engineResult.bytesProduced()) {
                    throw new SSLException("Engine bytesProduced " + engineResult.bytesProduced()
                            + " does not match bytes written " + target.position());
                }
                len -= engineResult.bytesConsumed();
                if (len != buffer.remaining()) {
                    throw new SSLException("Engine did not read the correct number of bytes");
                }
                if (engineResult.getStatus() == CLOSED && engineResult.bytesProduced() == 0) {
                    if (len > 0) {
                        throw new SocketException("Socket closed");
                    }
                    break;
                }

                target.flip();

                // Write the data to the socket.
                writeToSocket();
            } while (len > 0);
        }
Wrap bytes written to the underlying socket.
common/src/main/java/org/conscrypt/ConscryptEngineSocket.java