Handle invalid language codes in Boxes.java

When an invalid language code is give, write default value
instead of throwing.

This behaviour aligns with `MediaMuxer`.

PiperOrigin-RevId: 686066088
This commit is contained in:
sheenachhabra 2024-10-15 05:32:42 -07:00 committed by Copybara-Service
parent 5a827829b0
commit 643e16ca8f

View file

@ -1853,16 +1853,21 @@ import org.checkerframework.checker.nullness.qual.PolyNull;
return BoxUtils.wrapIntoBox("dOps", contents); return BoxUtils.wrapIntoBox("dOps", contents);
} }
/** Packs a three-letter language code into a short, packing 3x5 bits. */ /**
* Packs a three-letter language code into a short, packing 3x5 bits.
*
* <p>A default value 0 is returned if the {@code code} is not recognized.
*/
private static short languageCodeFromString(@Nullable String code) { private static short languageCodeFromString(@Nullable String code) {
short defaultLanguageCode = 0;
if (code == null) { if (code == null) {
return 0; return defaultLanguageCode;
} }
byte[] bytes = Util.getUtf8Bytes(code); byte[] bytes = Util.getUtf8Bytes(code);
if (bytes.length != 3) { if (bytes.length != 3) {
throw new IllegalArgumentException("Non-length-3 language code: " + code); return defaultLanguageCode;
} }
// Take only last 5 bits of each letter. // Take only last 5 bits of each letter.