/** * Sets the substitution characters to use when the converter is in substitution mode. The given * chars must not be longer than the value returned by getMaxCharsPerByte for this converter. * * @param chars the substitution chars * @exception IllegalArgumentException if given byte array is longer than the value returned by * the method getMaxBytesPerChar. * @see #setSubstitutionMode * @see #getMaxBytesPerChar */ public void setSubstitutionChars(char[] chars) throws IllegalArgumentException { if (decoder != null) decoder.replaceWith(new String(chars)); else { // only provided for subclasses if (chars.length > getMaxCharsPerByte()) throw new IllegalArgumentException(); subChars = new char[chars.length]; System.arraycopy(chars, 0, subChars, 0, chars.length); } }
/** * Convert text in a given character set to a Unicode string. Any invalid characters are replaced * with U+FFFD. Returns null if the character set is not recognized. * * @param text ByteBuffer containing the character array to convert. * @param charsetName Character set it's in encoded in. * @return: Unicode string on success, null on failure. */ @CalledByNative private static String convertToUnicodeWithSubstitutions(ByteBuffer text, String charsetName) { try { Charset charset = Charset.forName(charsetName); // TODO(mmenke): Investigate if Charset.decode() can be used // instead. The question is whether it uses the proper replace // character. JDK CharsetDecoder docs say U+FFFD is the default, // but Charset.decode() docs say it uses the "charset's default // replacement byte array". CharsetDecoder decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); decoder.replaceWith("\uFFFD"); return decoder.decode(text).toString(); } catch (Exception e) { return null; } }
@JRubyMethod(name = "replacement=", compat = RUBY1_9) public IRubyObject replacement_set(ThreadContext context, IRubyObject replacement) { srcDecoder.replaceWith(replacement.convertToString().asJavaString()); return replacement; }