From 646835f287e4b214a341e0d08d475b7445574938 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Sun, 9 Jun 2024 20:54:51 +0100 Subject: [PATCH] Force reading of title, artist and isrc as UTF-8 --- .../container/matroska/MatroskaStreamingFile.java | 10 ++++++---- .../matroska/format/MatroskaFileReader.java | 12 +++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/matroska/MatroskaStreamingFile.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/matroska/MatroskaStreamingFile.java index 73b0012d..d804e443 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/matroska/MatroskaStreamingFile.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/matroska/MatroskaStreamingFile.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -400,7 +401,7 @@ private void parseSegmentInfo(MatroskaElement infoElement) throws IOException { } else if (child.is(MatroskaElementType.TimecodeScale)) { timecodeScale = reader.asLong(child); } else if (child.is(MatroskaElementType.Title) && title == null) { - title = reader.asString(child); + title = reader.asString(child, StandardCharsets.UTF_8); } reader.skip(child); @@ -453,11 +454,12 @@ private void parseSimpleTag(MatroskaElement simpleTagElement) throws IOException } else if (child.is(MatroskaElementType.TagString)) { // https://www.matroska.org/technical/tagging.html if ("title".equalsIgnoreCase(tagName) && title == null) { - title = reader.asString(child); + title = reader.asString(child, StandardCharsets.UTF_8); } else if ("artist".equalsIgnoreCase(tagName)) { - artist = reader.asString(child); + artist = reader.asString(child, StandardCharsets.UTF_8); } else if ("isrc".equalsIgnoreCase(tagName)) { - isrc = reader.asString(child); + // probably not necessary to force a charset here + isrc = reader.asString(child, StandardCharsets.UTF_8); } } } diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/matroska/format/MatroskaFileReader.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/matroska/format/MatroskaFileReader.java index 4eabe158..dc2a432e 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/matroska/format/MatroskaFileReader.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/matroska/format/MatroskaFileReader.java @@ -5,6 +5,7 @@ import java.io.DataInput; import java.io.DataInputStream; import java.io.IOException; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; /** @@ -153,16 +154,21 @@ public double asDouble(MatroskaElement element) throws IOException { } } + public String asString(MatroskaElement element) throws IOException { + return asString(element, null); + } + /** * @param element Element to read from + * @param forceCharset The charset to use, or null for default. * @return The contents of the element as a string * @throws IOException On read error */ - public String asString(MatroskaElement element) throws IOException { + public String asString(MatroskaElement element, Charset forceCharset) throws IOException { if (element.is(MatroskaElementType.DataType.STRING)) { - return new String(asBytes(element), StandardCharsets.US_ASCII); + return new String(asBytes(element), forceCharset != null ? forceCharset : StandardCharsets.US_ASCII); } else if (element.is(MatroskaElementType.DataType.UTF8_STRING)) { - return new String(asBytes(element), StandardCharsets.UTF_8); + return new String(asBytes(element), forceCharset != null ? forceCharset : StandardCharsets.UTF_8); } else { throw new IllegalArgumentException("Not a string element."); }