From 70ee6c601c86489154801c5f7734d002c3ac838d Mon Sep 17 00:00:00 2001 From: Lucas Wiman Date: Sun, 14 Feb 2016 22:48:20 -0800 Subject: [PATCH] Use StringWriter to prevent race condition in println --- .../zxing/client/j2se/DecodeWorker.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/javase/src/main/java/com/google/zxing/client/j2se/DecodeWorker.java b/javase/src/main/java/com/google/zxing/client/j2se/DecodeWorker.java index 6e1054d89..d0b78d87a 100644 --- a/javase/src/main/java/com/google/zxing/client/j2se/DecodeWorker.java +++ b/javase/src/main/java/com/google/zxing/client/j2se/DecodeWorker.java @@ -34,6 +34,7 @@ import com.google.zxing.multi.MultipleBarcodeReader; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.IOException; +import java.io.StringWriter; import java.net.URI; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -149,21 +150,30 @@ final class DecodeWorker implements Callable { if (config.brief) { System.out.println(uri + ": Success"); } else { - for (Result result : results) { + StringWriter output = new StringWriter(); + for (int resultIndex = 0; resultIndex < results.length; resultIndex++) { + Result result = results[resultIndex]; ParsedResult parsedResult = ResultParser.parseResult(result); - System.out.println(uri + + output.write(uri + " (format: " + result.getBarcodeFormat() + ", type: " + parsedResult.getType() + "):\n" + "Raw result:\n" + result.getText() + "\n" + "Parsed result:\n" + - parsedResult.getDisplayResult()); - System.out.println("Found " + result.getResultPoints().length + " result points."); - for (int i = 0; i < result.getResultPoints().length; i++) { - ResultPoint rp = result.getResultPoints()[i]; - System.out.println(" Point " + i + ": (" + rp.getX() + ',' + rp.getY() + ')'); + parsedResult.getDisplayResult() + "\n"); + output.write("Found " + result.getResultPoints().length + " result points.\n"); + for (int pointIndex = 0; pointIndex < result.getResultPoints().length; pointIndex++) { + ResultPoint rp = result.getResultPoints()[pointIndex]; + output.write(" Point " + pointIndex + ": (" + rp.getX() + ',' + rp.getY() + ')'); + if (pointIndex != result.getResultPoints().length - 1) { + output.write('\n'); + } + } + if (resultIndex != results.length - 1) { + output.write('\n'); } } + System.out.println(output.toString()); } return results;