Updated Java code - changed QrSegmentAdvanced.makeSegmentsOptimally() to throw DataTooLongException and synchronize logic and Javadoc with QrCode class, changed Javadoc of DataTooLongException.

pull/40/head
Project Nayuki 6 years ago
parent af872343c0
commit 52b885fae1

@ -28,9 +28,10 @@ package io.nayuki.qrcodegen;
* Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception include: * Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception include:
* <ul> * <ul>
* <li><p>Decrease the error correction level if it was greater than {@code Ecc.LOW}.</p></li> * <li><p>Decrease the error correction level if it was greater than {@code Ecc.LOW}.</p></li>
* <li><p>If the advanced {@code encodeSegments()} function with 6 arguments was called, then increase the * <li><p>If the advanced {@code encodeSegments()} function with 6 arguments or the
* maxVersion argument if it was less than {@link QrCode#MAX_VERSION}. (This advice does not apply to the * {@code makeSegmentsOptimally()} function was called, then increase the maxVersion argument
* other factory functions because they search all versions up to {@code QrCode.MAX_VERSION}.)</p></li> * if it was less than {@link QrCode#MAX_VERSION}. (This advice does not apply to the other
* factory functions because they search all versions up to {@code QrCode.MAX_VERSION}.)</p></li>
* <li><p>Split the text data into better or optimal segments in order to reduce the number of * <li><p>Split the text data into better or optimal segments in order to reduce the number of
* bits required. (See {@link QrSegmentAdvanced#makeSegmentsOptimally(String,QrCode.Ecc,int,int) * bits required. (See {@link QrSegmentAdvanced#makeSegmentsOptimally(String,QrCode.Ecc,int,int)
* QrSegmentAdvanced.makeSegmentsOptimally()}.)</p></li> * QrSegmentAdvanced.makeSegmentsOptimally()}.)</p></li>
@ -42,6 +43,7 @@ package io.nayuki.qrcodegen;
* @see QrCode#encodeBinary(byte[], QrCode.Ecc) * @see QrCode#encodeBinary(byte[], QrCode.Ecc)
* @see QrCode#encodeSegments(java.util.List, QrCode.Ecc) * @see QrCode#encodeSegments(java.util.List, QrCode.Ecc)
* @see QrCode#encodeSegments(java.util.List, QrCode.Ecc, int, int, int, boolean) * @see QrCode#encodeSegments(java.util.List, QrCode.Ecc, int, int, int, boolean)
* @see QrSegmentAdvanced#makeSegmentsOptimally(String, io.nayuki.qrcodegen.QrCode.Ecc, int, int)
*/ */
public class DataTooLongException extends IllegalArgumentException { public class DataTooLongException extends IllegalArgumentException {

@ -57,8 +57,8 @@ public final class QrSegmentAdvanced {
* @return a new mutable list (not {@code null}) of segments (not {@code null}) * @return a new mutable list (not {@code null}) of segments (not {@code null})
* containing the text, minimizing the bit length with respect to the constraints * containing the text, minimizing the bit length with respect to the constraints
* @throws NullPointerException if the text or error correction level is {@code null} * @throws NullPointerException if the text or error correction level is {@code null}
* @throws IllegalArgumentException if 1 &#x2264; minVersion &#x2264; maxVersion &#x2264; 40 * @throws IllegalArgumentException if 1 &#x2264; minVersion &#x2264; maxVersion &#x2264; 40 is violated
* is violated, or if the data is too long to fit in a QR Code at maxVersion at ECL * @throws DataTooLongException if the text fails to fit in the maxVersion QR Code at the ECL
*/ */
public static List<QrSegment> makeSegmentsOptimally(String text, QrCode.Ecc ecl, int minVersion, int maxVersion) { public static List<QrSegment> makeSegmentsOptimally(String text, QrCode.Ecc ecl, int minVersion, int maxVersion) {
// Check arguments // Check arguments
@ -70,7 +70,7 @@ public final class QrSegmentAdvanced {
// Iterate through version numbers, and make tentative segments // Iterate through version numbers, and make tentative segments
List<QrSegment> segs = null; List<QrSegment> segs = null;
int[] codePoints = toCodePoints(text); int[] codePoints = toCodePoints(text);
for (int version = minVersion; version <= maxVersion; version++) { for (int version = minVersion; ; version++) {
if (version == minVersion || version == 10 || version == 27) if (version == minVersion || version == 10 || version == 27)
segs = makeSegmentsOptimally(codePoints, version); segs = makeSegmentsOptimally(codePoints, version);
assert segs != null; assert segs != null;
@ -79,9 +79,14 @@ public final class QrSegmentAdvanced {
int dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8; int dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;
int dataUsedBits = QrSegment.getTotalBits(segs, version); int dataUsedBits = QrSegment.getTotalBits(segs, version);
if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits) if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
return segs; return segs; // This version number is found to be suitable
if (version >= maxVersion) { // All versions in the range could not fit the given text
String msg = "Segment too long";
if (dataUsedBits != -1)
msg = String.format("Data length = %d bits, Max capacity = %d bits", dataUsedBits, dataCapacityBits);
throw new DataTooLongException(msg);
}
} }
throw new IllegalArgumentException("Data too long");
} }

Loading…
Cancel
Save