Trying to load a Font from resources throws a BufferUnderflowException

I am trying to load a Selawik font from my project's resources with following code:
try (InputStream is = getClass().getResourceAsStream("/fonts/selawk.ttf")) {
Font selawik = Font.createFont(Font.TRUETYPE_FONT, is);
}
try (InputStream is = getClass().getResourceAsStream("/fonts/selawk.ttf")) {
Font selawik = Font.createFont(Font.TRUETYPE_FONT, is);
}
but it results in an exception:
java.awt.FontFormatException: java.nio.BufferUnderflowException
at java.desktop/sun.font.TrueTypeFont.init(TrueTypeFont.java:532)
at java.desktop/sun.font.TrueTypeFont.<init>(TrueTypeFont.java:216)
at java.desktop/sun.font.TrueTypeFont.<init>(TrueTypeFont.java:196)
at java.desktop/sun.font.SunFontManager.createFont2D(SunFontManager.java:2208)
at java.desktop/java.awt.Font.createFont0(Font.java:1169)
at java.desktop/java.awt.Font.createFont(Font.java:1076)
at io.github.defective4.onematch.game.Application.<init>(Application.java:82)
at io.github.defective4.onematch.game.Application.<clinit>(Application.java:69)
java.awt.FontFormatException: java.nio.BufferUnderflowException
at java.desktop/sun.font.TrueTypeFont.init(TrueTypeFont.java:532)
at java.desktop/sun.font.TrueTypeFont.<init>(TrueTypeFont.java:216)
at java.desktop/sun.font.TrueTypeFont.<init>(TrueTypeFont.java:196)
at java.desktop/sun.font.SunFontManager.createFont2D(SunFontManager.java:2208)
at java.desktop/java.awt.Font.createFont0(Font.java:1169)
at java.desktop/java.awt.Font.createFont(Font.java:1076)
at io.github.defective4.onematch.game.Application.<init>(Application.java:82)
at io.github.defective4.onematch.game.Application.<clinit>(Application.java:69)
Does anyone have any clue why? The font appears to be a valid TrueType font, but Java still refuses to load it...
3 Replies
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @Defective! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Defective
Defective3mo ago
The font was downloaded from here and it works okay, OS can load and use it I also tried to wrap my stream in BufferedInputStream, but with no effect AND the font loads just fine when I pass it as a File, but obviously I can't do that when it's inside a compiled jar file
JavaBot
JavaBot3mo ago
Post Closed
This post has been closed by <@1014466568484294656>.
Want results from more Discord servers?
Add your server