@@ -110,16 +110,16 @@ public static DataCodec<double[]> DOUBLE(ByteOrder order) {
110110 /**
111111 * Returns a {@link DataCodec} for fixed-length strings.
112112 * <p>
113- * The resulting codec uses UTF-32 string encoding to encode / decode
114- * String arrays to a pre-defined fixed-length. Shorter (longer)
115- * Strings will be padded (truncated) as necessary.
113+ * The resulting codec uses UTF-32 string encoding to encode / decode String
114+ * arrays to a pre-defined fixed-length. Shorter Strings will be padded as
115+ * necessary. If a String longer than maxLength are encountered, an
116+ * {@link IllegalArgumentException} will be thrown.
116117 *
117118 * @param maxLength
118- * the maximum number of characters per string
119+ * the maximum number of characters per string
119120 * @param order
120- * the {@link ByteOrder} for encoding
121- * @return
122- * a data codec for fixed-length strings
121+ * the {@link ByteOrder} for encoding
122+ * @return a data codec for fixed-length strings
123123 */
124124 public static DataCodec <String []> ZARR_FIXED_STRING (int maxLength , ByteOrder order ) {
125125 return new ZarrFixedLengthStringDataCodec (maxLength , order );
@@ -372,8 +372,9 @@ public String[] deserialize(ReadData readData, int numElements) throws N5IOExcep
372372 * A {@link DataCodec} for fixed-length strings.
373373 * <p>
374374 * Uses UTF-32 string encoding to encode / decode String arrays to a
375- * pre-defined fixed-length. Shorter (longer) Strings will be padded
376- * (truncated) as necessary.
375+ * pre-defined fixed-length. Shorter Strings will be padded as necessary. An
376+ * {@link IllegalArgumentException} will be thrown if a String with length >
377+ * maxLength is encountered during encoding.
377378 *
378379 * @see {{@link #ZARR_FIXED_STRING(int, ByteOrder)}
379380 */
@@ -398,18 +399,22 @@ private static final class ZarrFixedLengthStringDataCodec extends DataCodec<Stri
398399 @ Override
399400 public ReadData serialize (String [] data ) throws N5IOException {
400401
401- if ( data .length == 0 )
402+ if ( data .length == 0 )
402403 return ReadData .empty ();
403404
404405 final int N = data .length ;
405406 final int encodedStringSize = maxLength * BYTES_PER_CHAR ;
406407 final int totalSize = N * encodedStringSize ;
407408 final ByteBuffer buf = ByteBuffer .allocate (totalSize );
408409 int pos = 0 ;
409- for ( int i = 0 ; i < N ; i ++ ) {
410+ for (int i = 0 ; i < N ; i ++) {
411+
412+ if (data [i ].length () > maxLength )
413+ throw new IllegalArgumentException ("Can not encode string of length " +
414+ data [i ].length () + " when maxLength = " + maxLength );
415+
410416 buf .position (pos );
411- final String s = data [i ].length () > maxLength ? data [i ].substring (0 , maxLength ) : data [i ];
412- buf .put (charset .encode (s ));
417+ buf .put (charset .encode (data [i ]));
413418 pos += encodedStringSize ;
414419 }
415420 return ReadData .from (buf .array ());
0 commit comments