Skip to content

Commit

Permalink
Merge pull request #4596 from ColdAnkles/asset-constructor-string-fix
Browse files Browse the repository at this point in the history
Alternate Encoding Handling for Addon Content
  • Loading branch information
cwisniew authored Jan 2, 2024
2 parents be87712 + 85fb279 commit 82c0b27
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/main/java/net/rptools/maptool/model/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
Expand Down Expand Up @@ -471,7 +475,23 @@ private Asset(MD5Key key, String name, byte[] data, Type type, String extension,
}

if (type.isStringType()) {
dataAsString = new String(data);
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
decoder
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
String decodedString;
try {
decodedString = decoder.decode(ByteBuffer.wrap(data)).toString();
} catch (Exception eOne) {
try {
decoder = StandardCharsets.UTF_16.newDecoder();
decodedString = decoder.decode(ByteBuffer.wrap(data)).toString();
} catch (Exception eTwo) {
decodedString = null;
}
}

dataAsString = decodedString;
} else {
dataAsString = null;
}
Expand Down

0 comments on commit 82c0b27

Please sign in to comment.