Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / QRCode / repo / java / src / main / java / io / nayuki / qrcodegen / QrCodeGeneratorWorker.java
1 /* 
2  * QR Code generator test worker (Java)
3  * 
4  * This program reads data and encoding parameters from standard input and writes
5  * QR Code bitmaps to standard output. The I/O format is one integer per line.
6  * Run with no command line arguments. The program is intended for automated
7  * batch testing of end-to-end functionality of this QR Code generator library.
8  * 
9  * Copyright (c) Project Nayuki. (MIT License)
10  * https://www.nayuki.io/page/qr-code-generator-library
11  * 
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of
13  * this software and associated documentation files (the "Software"), to deal in
14  * the Software without restriction, including without limitation the rights to
15  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16  * the Software, and to permit persons to whom the Software is furnished to do so,
17  * subject to the following conditions:
18  * - The above copyright notice and this permission notice shall be included in
19  *   all copies or substantial portions of the Software.
20  * - The Software is provided "as is", without warranty of any kind, express or
21  *   implied, including but not limited to the warranties of merchantability,
22  *   fitness for a particular purpose and noninfringement. In no event shall the
23  *   authors or copyright holders be liable for any claim, damages or other
24  *   liability, whether in an action of contract, tort or otherwise, arising from,
25  *   out of or in connection with the Software or the use or other dealings in the
26  *   Software.
27  */
28
29 package io.nayuki.qrcodegen;
30
31 import java.nio.charset.StandardCharsets;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Scanner;
35
36
37 public final class QrCodeGeneratorWorker {
38         
39         public static void main(String[] args) {
40                 // Set up input stream and start loop
41                 try (Scanner input = new Scanner(System.in, "US-ASCII")) {
42                         input.useDelimiter("\r\n|\n|\r");
43                         while (processCase(input));
44                 }
45         }
46         
47         
48         private static boolean processCase(Scanner input) {
49                 // Read data length or exit
50                 int length = input.nextInt();
51                 if (length == -1)
52                         return false;
53                 if (length > Short.MAX_VALUE)
54                         throw new RuntimeException();
55                 
56                 // Read data bytes
57                 boolean isAscii = true;
58                 byte[] data = new byte[length];
59                 for (int i = 0; i < data.length; i++) {
60                         int b = input.nextInt();
61                         if (b < 0 || b > 255)
62                                 throw new RuntimeException();
63                         data[i] = (byte)b;
64                         isAscii &= b < 128;
65                 }
66                 
67                 // Read encoding parameters
68                 int errCorLvl  = input.nextInt();
69                 int minVersion = input.nextInt();
70                 int maxVersion = input.nextInt();
71                 int mask       = input.nextInt();
72                 int boostEcl   = input.nextInt();
73                 if (!(0 <= errCorLvl && errCorLvl <= 3) || !(-1 <= mask && mask <= 7) || (boostEcl >>> 1) != 0
74                                 || !(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION))
75                         throw new RuntimeException();
76                 
77                 // Make segments for encoding
78                 List<QrSegment> segs;
79                 if (isAscii)
80                         segs = QrSegment.makeSegments(new String(data, StandardCharsets.US_ASCII));
81                 else
82                         segs = Arrays.asList(QrSegment.makeBytes(data));
83                 
84                 try {  // Try to make QR Code symbol
85                         QrCode qr = QrCode.encodeSegments(segs, QrCode.Ecc.values()[errCorLvl], minVersion, maxVersion, mask, boostEcl != 0);
86                         // Print grid of modules
87                         System.out.println(qr.version);
88                         for (int y = 0; y < qr.size; y++) {
89                                 for (int x = 0; x < qr.size; x++)
90                                         System.out.println(qr.getModule(x, y) ? 1 : 0);
91                         }
92                         
93                 } catch (DataTooLongException e) {
94                         System.out.println(-1);
95                 }
96                 System.out.flush();
97                 return true;
98         }
99         
100 }