diff --git a/java/io/nayuki/qrcodegen/DataTooLongException.java b/java/io/nayuki/qrcodegen/DataTooLongException.java
index 4baadfc..17f8d5e 100644
--- a/java/io/nayuki/qrcodegen/DataTooLongException.java
+++ b/java/io/nayuki/qrcodegen/DataTooLongException.java
@@ -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:
*
* Decrease the error correction level if it was greater than {@code Ecc.LOW}.
- * If the advanced {@code encodeSegments()} function with 6 arguments was called, then increase the
- * maxVersion argument 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}.)
+ * If the advanced {@code encodeSegments()} function with 6 arguments or the
+ * {@code makeSegmentsOptimally()} function was called, then increase the maxVersion argument
+ * 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}.)
* 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)
* QrSegmentAdvanced.makeSegmentsOptimally()}.)
@@ -42,6 +43,7 @@ package io.nayuki.qrcodegen;
* @see QrCode#encodeBinary(byte[], QrCode.Ecc)
* @see QrCode#encodeSegments(java.util.List, QrCode.Ecc)
* @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 {
diff --git a/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java b/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java
index c435a92..1efe7b5 100644
--- a/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java
+++ b/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java
@@ -57,8 +57,8 @@ public final class QrSegmentAdvanced {
* @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
* @throws NullPointerException if the text or error correction level is {@code null}
- * @throws IllegalArgumentException if 1 ≤ minVersion ≤ maxVersion ≤ 40
- * is violated, or if the data is too long to fit in a QR Code at maxVersion at ECL
+ * @throws IllegalArgumentException if 1 ≤ minVersion ≤ maxVersion ≤ 40 is violated
+ * @throws DataTooLongException if the text fails to fit in the maxVersion QR Code at the ECL
*/
public static List makeSegmentsOptimally(String text, QrCode.Ecc ecl, int minVersion, int maxVersion) {
// Check arguments
@@ -70,7 +70,7 @@ public final class QrSegmentAdvanced {
// Iterate through version numbers, and make tentative segments
List segs = null;
int[] codePoints = toCodePoints(text);
- for (int version = minVersion; version <= maxVersion; version++) {
+ for (int version = minVersion; ; version++) {
if (version == minVersion || version == 10 || version == 27)
segs = makeSegmentsOptimally(codePoints, version);
assert segs != null;
@@ -79,9 +79,14 @@ public final class QrSegmentAdvanced {
int dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;
int dataUsedBits = QrSegment.getTotalBits(segs, version);
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");
}