繁体   English   中英

如何安全地将 List 转换为 csv 字节数组?

[英]How to convert List to the csv byte array safely?

最初我有以下代码:

尝试 1

try (var output = new ByteArrayOutputStream();
     var printer = new CSVPrinter(new OutputStreamWriter(output), CSVFormat.DEFAULT)) {
   printer.printRecord(EMAIL);
   for (MyBean mb : items) {
     printer.printRecord(mb.getEmail());
   }
   externalHttpCall(output.toByteArray());
}

在这里我发现有时字节数组没有完全写入。

我知道这是因为在externalHttpCall调用期间没有刷新流。

为了修复它,我写了以下内容:

尝试 2

try (var output = new ByteArrayOutputStream();
     var printer = new CSVPrinter(new OutputStreamWriter(output), CSVFormat.DEFAULT)) {
  printer.printRecord(EMAIL);
  for (MyBean mb : items) {
    printer.printRecord(mb.getEmail());
  }
  printer.flush();
  log.info("Printer was flushed");

  externalHttpCall(output.toByteArray());
}

它解决了这个问题,但在这里我迷失了一个想法,即仅在externalHttpCall之后关闭流是非常糟糕的主意。 所以我想出了以下解决方案:

尝试 3

externalHttpCall(convertToByteArray(items);

public byte[] convertToByteArray(List<MyBean> items){
  try (var output = new ByteArrayOutputStream();
       var printer = new CSVPrinter(new OutputStreamWriter(output), CSVFormat.DEFAULT)) {
    printer.printRecord(EMAIL);
    for (MyBean mb : items) {
      printer.printRecord(mb.getEmail());
    }
    return output.toByteArray();    
  }
}

我预计在流关闭之前会发生刷新。 但根据我的实验,它不起作用。 看起来这是因为刷新发生在流关闭之前但在 toByteArray 调用之后。

我怎么能修好呢?

鉴于问题中的三个代码片段,我认为这应该有效:

externalHttpCall(convertToByteArray(items);

public byte[] convertToByteArray(List<MyBean> items){
  try (var output = new ByteArrayOutputStream();
       var printer = new CSVPrinter(new OutputStreamWriter(output), CSVFormat.DEFAULT)) {
    printer.printRecord(EMAIL);
    for (MyBean mb : items) {
      printer.printRecord(mb.getEmail());
    }
    printer.flush()
    log.info("Printer was flushed");

    return output.toByteArray();
  }
}

根据CSVFormatCSVPrinter在关闭时自动刷新( CSVFormat.DEFAULT不会自动刷新...)。 您可以使用CSVFormat的 builder like pattern 使格式在关闭时与CSVFormat.DEFAULT.withAutoFlush(true)齐平(感谢@PetrBodnár 提供此提示)。 然而,这在上面的例子中可能没有区别。

如果您将 try-with-resource 转换为实际的调用顺序,您将得到如下内容:

var output = new ByteArrayOutputStream();
var printer = new CSVPrinter(new OutputStreamWriter(output), CSVFormat.DEFAULT)
printer.printRecord(EMAIL);
...
var result = output.toByteArray();
printer.close();  // might call flush
output.close();
return result;

由于关闭操作将在 finally 块中调用,因此它们将创建字节数组后进行。 如果需要刷新,则需要在调用toByteArray之前执行此操作。

以下是正确的用法:

var output = new ByteArrayOutputStream();
try (var printer = new CSVPrinter(
            new OutputStreamWriter(output, StandardCharsets.UTF_8), CSVFormat.DEFAULT)) {
    printer.printRecord(EMAIL);
    for (MyBean mb : items) {
        printer.printRecord(mb.getEmail());
    }
}
// Everything flushed and closed.
externalHttpCall(output.toByteArray());

此错误行为可能源于其他原因。

例如 externalHttpCall 不刷新。 或者将字节写为文本(使用 Writer io OutputStream),并期待 UTF-8,其多字节序列很脆弱,可能会引发异常。 或者将 HTTP 标头 Content-Length 设置错误,如 String.length()。

另一个原因:包含 null 或getEmail抛出未检测到的异常。

可用的还有:

String s = output.toString(StandardCharsets.UTF_8);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM