Skip to content

[rustjava-unsupported-charset-exception] Throw UnsupportedEncodingException instead of panicking on unknown charsets - #5

Merged
Jun025 merged 5 commits into
mainfrom
unsupported-charset-exception
Jul 22, 2026
Merged

[rustjava-unsupported-charset-exception] Throw UnsupportedEncodingException instead of panicking on unknown charsets#5
Jun025 merged 5 commits into
mainfrom
unsupported-charset-exception

Conversation

@Jun025

@Jun025 Jun025 commented Jul 22, 2026

Copy link
Copy Markdown
Owner

What

  • Adds java.io.UnsupportedEncodingException (parent java.io.IOException), following the existing exception-class conventions (eof_exception etc.), registered in loader.rs.
  • Converts the three unimplemented!() charset panics — String decode/encode (string.rs:787/:797) and InputStreamReader.read() (input_stream_reader.rs:115) — into thrown UnsupportedEncodingException with the offending charset name as the message.
  • Unifies the String↔Reader charset support: both now resolve through a shared java_runtime::charset::Charset (UTF-8 / EUC-KR family / ISO-8859-1 / US-ASCII + aliases). InputStreamReader previously accepted only literal UTF-8/EUC-KR, so System.setProperty("file.encoding", "ISO-8859-1") panicked every subsequent read(). No decoder logic was duplicated: the reader's incremental path wraps the same tables (encoding_rs decoders + byte→char for the single-byte sets).

Necessary side fixes

  • System.setProperty was declared (...)Ljava/lang/Object; but the JDK signature returns String — javac-compiled fixtures failed with NoSuchMethodError. Fixed the proto and the jvm bootstrap caller (jvm/src/jvm.rs). Properties.setProperty correctly stays Object.
  • Added Throwable.getMessage() (was missing entirely), needed to verify the exception message from Java code.

Evidence

  • Before: the three new Rust tests hit unimplemented!() process panics (not implemented: unsupported charset: UTF-16 / Shift_JIS).
  • After: all throw catchable Java exceptions; is_instance checks confirm UnsupportedEncodingException and IOException in the hierarchy, and getMessage() returns the charset name.
  • New fixture test_data/UnsupportedCharset (javac --release 21, no invokedynamic/StringBuilder) proves Java-side try { … } catch (UnsupportedEncodingException e) actually catches for getBytes("UTF-16"), new String(bytes, "Shift_JIS"), and the file.encoding=UTF-16 reader path — and that file.encoding=ISO-8859-1 now reads aéb correctly.
  • cargo test --all fully green (124 java_runtime tests + all fixtures), cargo fmt --check, cargo clippy --all and --target wasm32-unknown-unknown with -D warnings clean.

Non-goals (per ticket)

  • No new real encodings (UTF-16/Shift_JIS remain unsupported — now rejected per spec instead of trapping).
  • No java.nio.charset API.
  • Known pre-existing issue left out of scope (noted in STATE.md): the reader recreates its stream decoder per read(), so multibyte sequences split across buffer boundaries can be lost for EUC-KR.

🤖 Generated with Claude Code

jun0 and others added 5 commits July 22, 2026 18:55
…wn charsets

An unsupported charset name — pure user input from Java code — hit
unimplemented!() in String.getBytes(charset), new String(byte[], charset)
and InputStreamReader.read(), killing the host process. "hi".getBytes("UTF-16")
was enough to trap the JVM.

- Add java.io.UnsupportedEncodingException (extends java.io.IOException),
  registered in the class loader; message carries the offending charset name.
- Extract the charset tables shared by java.lang.String and
  java.io.InputStreamReader into java_runtime::charset::Charset so both
  paths support the same set — ISO-8859-1/US-ASCII (and aliases) now work
  through InputStreamReader instead of only UTF-8/EUC-KR.
- Fix System.setProperty descriptor to the JDK signature
  (...)Ljava/lang/String; (was Object), including the jvm bootstrap caller —
  javac-compiled code failed with NoSuchMethodError on the old descriptor.
- Add Throwable.getMessage().
- Tests: Rust unit tests for all three throw paths + ISO-8859-1 reader path,
  and test_data/UnsupportedCharset fixture proving Java try/catch catches it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…-charset-exception

Resolve STATE.md/REPORT.md add/add conflicts by adopting the superset:
all task entries kept, classfile task marked merged at main 549b9eb.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
STATE.md superset resolution; REPORT.md auto-merged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…arset-exception

STATE.md/REPORT.md superset resolution.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Jun025
Jun025 merged commit 7fd0ad8 into main Jul 22, 2026
6 of 7 checks passed
@Jun025
Jun025 deleted the unsupported-charset-exception branch July 22, 2026 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant