From: Jeremias Maerki Date: Mon, 9 Mar 2009 08:12:45 +0000 (+0000) Subject: Restored compatibility with Java VMs that don't support nio Charsets for codepages... X-Git-Tag: fop-1_0~297 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=efcd8b9100f7dcc85d7a8d8f3d0fe7f919192e99;p=xmlgraphics-fop.git Restored compatibility with Java VMs that don't support nio Charsets for codepages such as Cp500 (EBCDIC). The simplifications for the fallback case may look wrong but the code basically represents the state before revision 746664 which introduced this problem. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@751613 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/afp/fonts/CharacterSet.java b/src/java/org/apache/fop/afp/fonts/CharacterSet.java index 52bebca56..f0b671932 100644 --- a/src/java/org/apache/fop/afp/fonts/CharacterSet.java +++ b/src/java/org/apache/fop/afp/fonts/CharacterSet.java @@ -27,6 +27,7 @@ import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; import java.nio.charset.CodingErrorAction; +import java.nio.charset.UnsupportedCharsetException; import java.util.Map; import org.apache.commons.logging.Log; @@ -114,8 +115,14 @@ public class CharacterSet { } this.codePage = codePage; this.encoding = encoding; - this.encoder = Charset.forName(encoding).newEncoder(); - this.encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + try { + this.encoder = Charset.forName(encoding).newEncoder(); + this.encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + } catch (UnsupportedCharsetException uce) { + //No nio-capable encoder available + //This may happen with "Cp500" on Sun Java 1.4.2 + this.encoder = null; + } this.path = path; this.characterSetOrientations = new java.util.HashMap(4); @@ -321,7 +328,12 @@ public class CharacterSet { * @return true if the character is in the character set */ public boolean hasChar(char c) { - return encoder.canEncode(c); + if (encoder != null) { + return encoder.canEncode(c); + } else { + //Sun Java 1.4.2 compatibility + return true; + } } /** @@ -331,14 +343,26 @@ public class CharacterSet { * @throws CharacterCodingException if the encoding operation fails */ public byte[] encodeChars(CharSequence chars) throws CharacterCodingException { - ByteBuffer bb = encoder.encode(CharBuffer.wrap(chars)); - if (bb.hasArray()) { - return bb.array(); + if (encoder != null) { + ByteBuffer bb = encoder.encode(CharBuffer.wrap(chars)); + if (bb.hasArray()) { + return bb.array(); + } else { + bb.rewind(); + byte[] bytes = new byte[bb.remaining()]; + bb.get(bytes); + return bytes; + } } else { - bb.rewind(); - byte[] bytes = new byte[bb.remaining()]; - bb.get(bytes); - return bytes; + //Sun Java 1.4.2 compatibility + byte[] bytes; + try { + bytes = chars.toString().getBytes(this.encoding); + return bytes; + } catch (UnsupportedEncodingException uee) { + throw new UnsupportedOperationException( + "Unsupported encoding: " + uee.getMessage()); + } } }