public CompletableFuture doRequestAsync()

in src/main/java/com/aliyun/openservices/paifeaturestore/datasource/FeatureDBClient.java [266:308]


    public CompletableFuture<byte[]> doRequestAsync(Request request) {
        CompletableFuture<byte[]> future = new CompletableFuture<>();
        httpclient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                // 处理失败
                future.completeExceptionally(new HttpException(-1, e.getMessage()));
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // 处理响应
                if (response.isSuccessful() && response.body() != null) {
                    try (InputStream inputStream = response.body().byteStream()) {
                        byte[] sizeByte = new byte[4];
                        if (inputStream.read(sizeByte) != sizeByte.length) {
                            future.completeExceptionally(new HttpException(-1, "input stream read error"));
                            return;
                        }
                        int size = ByteBuffer.wrap(sizeByte).order(ByteOrder.LITTLE_ENDIAN).getInt();
                        byte[] content = new byte[size];
                        int offset = 0;
                        while (offset < size) {
                            int read = inputStream.read(content, offset, size - offset);
                            if (read == -1) { // End of the stream
                                future.completeExceptionally(new HttpException(-1, "Input stream read error: unexpected end of stream"));
                                return;
                            }
                            offset += read;
                        }
                        future.complete(content);
                    }
                } else {
                    int errorCode = response.code();
                    try (InputStream errorStream = response.body().byteStream()) {
                        String errorMessage = IOUtils.readStreamAsString(errorStream, "UTF-8");
                        future.completeExceptionally(new HttpException(errorCode, errorMessage));
                    }
                }
            }
        });
        return future;
    }