CipherInput 流在 CipherOutputStream 完全写入之前不可用

问题描述 投票:0回答:1

基本上,我正在尝试通过 REST 从 cipherInputStream 读取数据,但看起来在写入 cipherOutputStream 的其他服务完成之前该数据不可用。

其他服务预计将传输 protobuf 数据,如下所示,

 @GetMapping(value = "/stream/data")
  public ResponseEntity<StreamingResponseBody> streamData() {
    StreamingResponseBody responseBody =
        response -> {
          try (org.bouncycastle.jcajce.io.CipherOutputStream cipherOutputStream =
              new org.bouncycastle.jcajce.io.CipherOutputStream(response, initCipher())) {
            for (int i = 1; i <= count; i++) {
              VehicleInfo vehicleInfo =
                  VehicleInfo.newBuilder()
                      .setId(123)
                      .setHandle(123)
                      .build();
              vehicleInfo.writeDelimitedTo(cipherOutputStream);
              logger.info("Encrypted vehicle--> {} {}", i,vehicleInfo);
              
            }
          } 
        };

    return ResponseEntity.ok().body(responseBody);
  }

我的服务尝试使用 apache Httpclient 消耗流,但 while 循环的读取操作似乎被阻止,直到其他服务完全写入 CipherOutputStream。

        final InputStream content = closeableHttpResponse.getEntity().getContent();
        CipherInputStream cipherInputStream = new CipherInputStream(content, cipher);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = cipherInputStream.read(buffer)) > 0) {
          logger.info("reading bytes of length--1024");
        }

我希望在其他服务仍在流式传输数据时从 CipherInputStream 读取数据。

java encryption inputstream java-io protobuf-java
1个回答
0
投票

看起来我们使用的密码实例“AES/GCM/NoPadding”正在阻止调用。我们现在在这两个服务上使用 AES/CBC/PKCS5Padding。现在我们可以看到两个服务器正在同时处理。

© www.soinside.com 2019 - 2024. All rights reserved.