Repository: apache/kafka
File: server-common/src/test/java/org/apache/kafka/server/IntegrationTestUtils.java (Java)

Code before:
        outgoing.flush();
    }

    public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) {
        return new RequestHeader(apiKey, apiVersion, "client-id", correlationId.getAndIncrement());
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractResponse> T receive(Socket socket, ApiKeys apiKey, short version) throws IOException, ClassCastException {
        var incoming = new DataInputStream(socket.getInputStream());
        int len = incoming.readInt();

        var responseBytes = new byte[len];
        incoming.readFully(responseBytes);

        var responseBuffer = ByteBuffer.wrap(responseBytes);
        ResponseHeader.parse(responseBuffer, apiKey.responseHeaderVersion(version));

        return (T) AbstractResponse.parseResponse(apiKey, new ByteBufferAccessor(responseBuffer), version);
    }

    public static <T extends AbstractResponse> T sendAndReceive(
        AbstractRequest request,
        Socket socket
    ) throws IOException {
        correlationId.getAndIncrement();
        RequestHeader header = new RequestHeader(request.apiKey(), request.version(), "client-id", correlationId.get());
        byte[] serializedBytes = Utils.toArray(request.serializeWithHeader(header));
        sendRequest(socket, serializedBytes);
        return receive(socket, request.apiKey(), request.version());
    }

    public static <T extends AbstractResponse> T connectAndReceive(
        AbstractRequest request,
        int port
    ) throws IOException {
        try (Socket socket = connect(port)) {
            return sendAndReceive(request, socket);
        }
    }

    public static Socket connect(int port) throws IOException {
        return new Socket("localhost", port);
    }
}

Review comment (suggestion):
```java
        var header = nextRequestHeader(request.apiKey(), request.version());
        sendRequest(socket, Utils.toArray(request.serializeWithHeader(header)));
        return receive(socket, request.apiKey(), request.version());
```

Code after:
        outgoing.flush();
    }

    public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) {
        return new RequestHeader(apiKey, apiVersion, "client-id", CORRELATION_ID.getAndIncrement());
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractResponse> T receive(Socket socket, ApiKeys apiKey, short version) throws IOException, ClassCastException {
        var incoming = new DataInputStream(socket.getInputStream());
        int len = incoming.readInt();

        var responseBytes = new byte[len];
        incoming.readFully(responseBytes);

        var responseBuffer = ByteBuffer.wrap(responseBytes);
        ResponseHeader.parse(responseBuffer, apiKey.responseHeaderVersion(version));

        return (T) AbstractResponse.parseResponse(apiKey, new ByteBufferAccessor(responseBuffer), version);
    }

    public static <T extends AbstractResponse> T sendAndReceive(
        AbstractRequest request,
        Socket socket
    ) throws IOException {
        var header = nextRequestHeader(request.apiKey(), request.version());
        sendRequest(socket, Utils.toArray(request.serializeWithHeader(header)));
        return receive(socket, request.apiKey(), request.version());
    }

    public static <T extends AbstractResponse> T connectAndReceive(
        AbstractRequest request,
        int port
    ) throws IOException {
        try (Socket socket = connect(port)) {
            return sendAndReceive(request, socket);
        }
    }

    public static Socket connect(int port) throws IOException {
        return new Socket("localhost", port);
    }
}