Index: test/java/org/apache/fop/StandardTestSuite.java
===================================================================
--- test/java/org/apache/fop/StandardTestSuite.java	(revision 1147766)
+++ test/java/org/apache/fop/StandardTestSuite.java	(working copy)
@@ -22,6 +22,8 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
+import org.apache.fop.afp.fonts.CharactersetEncoderTest;
+import org.apache.fop.afp.parser.MODCAParserTestCase;
 import org.apache.fop.area.ViewportTestSuite;
 import org.apache.fop.afp.parser.MODCAParserTestCase;
 import org.apache.fop.fonts.DejaVuLGCSerifTest;
@@ -59,6 +61,7 @@
         suite.addTest(new TestSuite(PDFsRGBSettingsTestCase.class));
         suite.addTest(new TestSuite(DejaVuLGCSerifTest.class));
         suite.addTest(new TestSuite(MODCAParserTestCase.class));
+        suite.addTest(new TestSuite(CharactersetEncoderTest.class));
         suite.addTest(AFPTestSuite.suite());
         suite.addTest(PSTestSuite.suite());
         suite.addTest(RichTextFormatTestSuite.suite());
Index: test/java/org/apache/fop/afp/fonts/CharactersetEncoderTest.java
===================================================================
--- test/java/org/apache/fop/afp/fonts/CharactersetEncoderTest.java	(revision 0)
+++ test/java/org/apache/fop/afp/fonts/CharactersetEncoderTest.java	(revision 0)
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.afp.fonts;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.CharacterCodingException;
+
+import junit.framework.TestCase;
+
+/**
+ * Test {@link CharactersetEncoder}
+ */
+public class CharactersetEncoderTest extends TestCase {
+    private CharactersetEncoder singlebyteEncoder;
+    private CharactersetEncoder doublebyteEncoder;
+
+    public void setUp() {
+        singlebyteEncoder = CharactersetEncoder.newInstance("cp500", false);
+        doublebyteEncoder = CharactersetEncoder.newInstance("cp937", true);
+    }
+
+    // This is just an arbitrary CJK string
+    private final String testCJKText = "\u8ACB\u65BC\u627F\u505A\u65E5\u4E03\u65E5\u5167\u672A\u9054"
+            + "\u4E03\u65E5\u4E4B\u5B9A\u5B58\u8005\u4EE5\u5BE6\u969B\u5230\u671F\u65E5\u5167\u78BA"
+            + "\u8A8D\u672C\u4EA4\u6613\u5167\u5BB9\u3002\u5982\u672A\u65BC\u4E0A\u8FF0\u671F\u9593"
+            + "\u5167\u63D0\u51FA\u7570\u8B70\uFF0C\u8996\u540C\u610F\u627F\u8A8D\u672C\u4EA4\u6613"
+            + "\u3002";
+
+    private final byte[] test6CJKChars = {
+            (byte) 0x61, (byte) 0x99,
+            (byte) 0x50, (byte) 0xf4,
+            (byte) 0x50, (byte) 0xd4,
+            (byte) 0x56, (byte) 0x99,
+            (byte) 0x4c, (byte) 0xc9,
+            (byte) 0x4c, (byte) 0x44 };
+
+    private final String testEngText = "Hello World!";
+    private final byte[] testEngChars = {
+            (byte) 0xc8, // H
+            (byte) 0x85, // e
+            (byte) 0x93, // l
+            (byte) 0x93, // l
+            (byte) 0x96, // o
+            (byte) 0x40, // " "
+            (byte) 0xe6, // W
+            (byte) 0x96, // o
+            (byte) 0x99, // r
+            (byte) 0x93, // l
+            (byte) 0x84, // d
+            (byte) 0x4f  // !
+    };
+
+    /**
+     * Tests canEncode() - tests that canEncode() responds properly to various input characters.
+     */
+    public void testCanEncode() {
+        // Both SBCS and DBCS should support Latin characters
+        for (char c = '!'; c < '~'; c++) {
+            assertTrue(singlebyteEncoder.canEncode(c));
+            assertTrue(doublebyteEncoder.canEncode(c));
+        }
+        // ONLY the double byte characters can handle CJK text
+        for (char c : testCJKText.toCharArray()) {
+            assertFalse(singlebyteEncoder.canEncode(c));
+            assertTrue(doublebyteEncoder.canEncode(c));
+        }
+        // Ensure that double byte encoder doesn't just return true all the time...
+        assertFalse(doublebyteEncoder.canEncode('\u00BB'));
+    }
+
+    public void testEncode() throws CharacterCodingException, IOException {
+        CharactersetEncoder.EncodedChars encChars;// = doublebyteEncoder.encode(testCJKText);
+        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+        // JAVA 1.5 has a bug in the JVM in which these err for some reason... JAVA 1.6 no issues
+        /*encChars.writeTo(bOut, 0, encChars.getLength());
+        byte[] bytes = bOut.toByteArray();
+        for (int i = 0; i < 12; i++) {
+            assertEquals(test6CJKChars[i], bytes[i]);
+        }
+        bOut.reset();*/
+
+        encChars = singlebyteEncoder.encode(testEngText);
+        encChars.writeTo(bOut, 0, encChars.getLength());
+        byte[] engBytes = bOut.toByteArray();
+        for (int i = 0; i < testEngChars.length; i++) {
+            assertEquals(testEngChars[i], engBytes[i]);
+        }
+        assertEquals(testEngChars.length, engBytes.length);
+    }
+}
Index: src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java
===================================================================
--- src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java	(revision 1147766)
+++ src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java	(working copy)
@@ -27,7 +27,6 @@
 
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
-
 import org.apache.fop.afp.AFPResourceLevel;
 import org.apache.fop.afp.AFPResourceLevelDefaults;
 import org.apache.fop.afp.fonts.AFPFont;
@@ -258,9 +257,11 @@
             }
             String name = afpFontCfg.getAttribute("name", characterset);
             CharacterSet characterSet = null;
+            boolean ebcdicDBCS = afpFontCfg.getAttributeAsBoolean("ebcdic-dbcs", false);
+
             try {
-                characterSet = CharacterSetBuilder.getDoubleByteInstance()
-                                .build(characterset, codepage, encoding, accessor);
+                characterSet = CharacterSetBuilder.getDoubleByteInstance().buildDBCS(characterset,
+                        codepage, encoding, ebcdicDBCS, accessor);
             } catch (IOException ioe) {
                 toConfigurationException(codepage, characterset, ioe);
             }
Index: src/java/org/apache/fop/afp/fonts/CharacterSet.java
===================================================================
--- src/java/org/apache/fop/afp/fonts/CharacterSet.java	(revision 1147766)
+++ src/java/org/apache/fop/afp/fonts/CharacterSet.java	(working copy)
@@ -21,19 +21,13 @@
 
 import java.io.File;
 import java.io.UnsupportedEncodingException;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
 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;
 import org.apache.commons.logging.LogFactory;
-
 import org.apache.fop.afp.AFPConstants;
+import org.apache.fop.afp.fonts.CharactersetEncoder.EncodedChars;
 import org.apache.fop.afp.util.ResourceAccessor;
 import org.apache.fop.afp.util.SimpleResourceAccessor;
 import org.apache.fop.afp.util.StringUtils;
@@ -70,16 +64,16 @@
 
 
     /** The code page to which the character set relates */
-    protected String codePage;
+    protected final String codePage;
 
     /** The encoding used for the code page */
-    protected String encoding;
+    protected final String encoding;
 
-    /** The charset encoder corresponding to this encoding */
-    private CharsetEncoder encoder;
+    /** The characterset encoder corresponding to this encoding */
+    private final CharactersetEncoder encoder;
 
     /** The character set relating to the font */
-    protected String name;
+    protected final String name;
 
     /** The path to the installed fonts */
     private ResourceAccessor accessor;
@@ -105,20 +99,22 @@
      * {@link #CharacterSet(String, String, String, ResourceAccessor)} instead.
      */
     public CharacterSet(String codePage, String encoding, String name, String path) {
-        this(codePage, encoding, name,
+        this(codePage, encoding, false, name,
                 new SimpleResourceAccessor(path != null ? new File(path) : null));
     }
 
     /**
-     * Constructor for the CharacterSetMetric object, the character set is used
-     * to load the font information from the actual AFP font.
+     * Constructor for the CharacterSetMetric object, the character set is used to load the font
+     * information from the actual AFP font.
      *
      * @param codePage the code page identifier
      * @param encoding the encoding of the font
+     * @param isEBDCS if this is an EBCDIC double byte character set.
      * @param name the character set name
      * @param accessor the resource accessor to load resource with
      */
-     CharacterSet(String codePage, String encoding, String name, ResourceAccessor accessor) {
+    CharacterSet(String codePage, String encoding, boolean isEBDCS, String name,
+            ResourceAccessor accessor) {
         if (name.length() > MAX_NAME_LEN) {
             String msg = "Character set name '" + name + "' must be a maximum of "
                 + MAX_NAME_LEN + " characters";
@@ -133,14 +129,7 @@
         }
         this.codePage = codePage;
         this.encoding = encoding;
-        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.encoder = CharactersetEncoder.newInstance(encoding, isEBDCS);
         this.accessor = accessor;
 
         this.characterSetOrientations = new java.util.HashMap(4);
@@ -357,32 +346,8 @@
      * @return the encoded characters
      * @throws CharacterCodingException if the encoding operation fails
      */
-    public byte[] encodeChars(CharSequence chars) throws CharacterCodingException {
-        if (encoder != null) {
-            ByteBuffer bb;
-            // encode method is not thread safe
-            synchronized (encoder) {
-                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 {
-            //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());
-            }
-        }
+    public EncodedChars encodeChars(CharSequence chars) throws CharacterCodingException {
+        return encoder.encode(chars);
     }
 
     /**
Index: src/java/org/apache/fop/afp/fonts/FopCharacterSet.java
===================================================================
--- src/java/org/apache/fop/afp/fonts/FopCharacterSet.java	(revision 1147766)
+++ src/java/org/apache/fop/afp/fonts/FopCharacterSet.java	(working copy)
@@ -44,7 +44,7 @@
         String name,
         Typeface charSet) {
 
-        super(codePage, encoding, name, (ResourceAccessor)null);
+        super(codePage, encoding, false, name, (ResourceAccessor) null);
         this.charSet = charSet;
     }
 
Index: src/java/org/apache/fop/afp/fonts/CharacterSetBuilder.java
===================================================================
--- src/java/org/apache/fop/afp/fonts/CharacterSetBuilder.java	(revision 1147766)
+++ src/java/org/apache/fop/afp/fonts/CharacterSetBuilder.java	(working copy)
@@ -30,13 +30,11 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
-import org.apache.xmlgraphics.image.loader.util.SoftMapCache;
-
 import org.apache.fop.afp.AFPConstants;
 import org.apache.fop.afp.util.ResourceAccessor;
 import org.apache.fop.afp.util.StructuredFieldReader;
 import org.apache.fop.fonts.Typeface;
+import org.apache.xmlgraphics.image.loader.util.SoftMapCache;
 
 /**
  * The CharacterSetBuilder is responsible building the a CharacterSet instance that holds
@@ -181,9 +179,9 @@
     }
 
     /**
-     * Load the font details and metrics into the CharacterSetMetric object,
-     * this will use the actual afp code page and character set files to load
-     * the object with the necessary metrics.
+     * Load the font details and metrics into the CharacterSetMetric object, this will use the
+     * actual afp code page and character set files to load the object with the necessary metrics.
+     * 
      * @param characterSetName name of the characterset
      * @param codePageName name of the code page file
      * @param encoding encoding name
@@ -191,9 +189,47 @@
      * @return CharacterSet object
      * @throws IOException if an I/O error occurs
      */
-    public CharacterSet build(String characterSetName, String codePageName,
-            String encoding, ResourceAccessor accessor) throws IOException {
+    public CharacterSet build(String characterSetName, String codePageName, String encoding,
+            ResourceAccessor accessor) throws IOException {
+        return processFont(characterSetName, codePageName, encoding, false, accessor);
+    }
 
+    /**
+     * Load the font details and metrics into the CharacterSetMetric object, this will use the
+     * actual afp code page and character set files to load the object with the necessary metrics.
+     * This method is to be used for double byte character sets (DBCS).
+     *
+     * @param characterSetName name of the characterset
+     * @param codePageName name of the code page file
+     * @param encoding encoding name
+     * @param isEDBCS if this is an EBCDIC double byte character set (DBCS)
+     * @param accessor used to load codepage and characterset
+     * @return CharacterSet object
+     * @throws IOException if an I/O error occurs
+     */
+    public CharacterSet buildDBCS(String characterSetName, String codePageName, String encoding,
+            boolean isEDBCS, ResourceAccessor accessor) throws IOException {
+        return processFont(characterSetName, codePageName, encoding, isEDBCS, accessor);
+    }
+
+    /**
+     * Load the font details and metrics into the CharacterSetMetric object, this will use the
+     * actual afp code page and character set files to load the object with the necessary metrics.
+     * 
+     * @param characterSetName the CharacterSetMetric object to populate
+     * @param codePageName the name of the code page to use
+     * @param encoding name of the encoding in use
+     * @param typeface base14 font name
+     * @return CharacterSet object
+     * @throws IOException if an I/O error occurs
+     */
+    public CharacterSet build(String characterSetName, String codePageName, String encoding,
+            Typeface typeface) throws IOException {
+        return new FopCharacterSet(codePageName, encoding, characterSetName, typeface);
+    }
+
+    private CharacterSet processFont(String characterSetName, String codePageName, String encoding,
+            boolean isEDBCS, ResourceAccessor accessor) throws IOException {
         // check for cached version of the characterset
         String descriptor = characterSetName + "_" + encoding + "_" + codePageName;
         CharacterSet characterSet = (CharacterSet)characterSetsCache.get(descriptor);
@@ -203,8 +239,8 @@
         }
 
         // characterset not in the cache, so recreating
-        characterSet = new CharacterSet(
-                codePageName, encoding, characterSetName, accessor);
+        characterSet = new CharacterSet(codePageName, encoding, isEDBCS, characterSetName,
+                accessor);
 
         InputStream inputStream = null;
 
@@ -268,26 +304,9 @@
         }
         characterSetsCache.put(descriptor, characterSet);
         return characterSet;
-
     }
 
     /**
-     * Load the font details and metrics into the CharacterSetMetric object,
-     * this will use the actual afp code page and character set files to load
-     * the object with the necessary metrics.
-     *
-     * @param characterSetName the CharacterSetMetric object to populate
-     * @param codePageName the name of the code page to use
-     * @param encoding name of the encoding in use
-     * @param typeface base14 font name
-     * @return CharacterSet object
-     */
-    public CharacterSet build(String characterSetName, String codePageName,
-            String encoding, Typeface typeface) {
-       return new FopCharacterSet(codePageName, encoding, characterSetName, typeface);
-    }
-
-    /**
      * Load the code page information from the appropriate file. The file name
      * to load is determined by the code page name and the file extension 'CDP'.
      *
Index: src/java/org/apache/fop/afp/ptoca/PtocaBuilder.java
===================================================================
--- src/java/org/apache/fop/afp/ptoca/PtocaBuilder.java	(revision 1147766)
+++ src/java/org/apache/fop/afp/ptoca/PtocaBuilder.java	(working copy)
@@ -29,6 +29,7 @@
 import org.apache.xmlgraphics.java2d.color.CIELabColorSpace;
 import org.apache.xmlgraphics.java2d.color.ColorUtil;
 import org.apache.xmlgraphics.java2d.color.ColorWithAlternatives;
+import org.apache.fop.afp.fonts.CharactersetEncoder.EncodedChars;
 
 /**
  * Generator class for PTOCA data structures.
@@ -190,34 +191,25 @@
      * @param data The text data to add.
      * @throws IOException if an I/O error occurs
      */
-    public void addTransparentData(byte[] data) throws IOException {
-        if (data.length <= TRANSPARENT_DATA_MAX_SIZE) {
-            addTransparentDataChunk(data);
-        } else {
-            // data size greater than TRANSPARENT_MAX_SIZE, so slice
-            int numTransData = data.length / TRANSPARENT_DATA_MAX_SIZE;
-            int currIndex = 0;
-            for (int transDataCnt = 0; transDataCnt < numTransData; transDataCnt++) {
-                addTransparentDataChunk(data, currIndex, TRANSPARENT_DATA_MAX_SIZE);
-                currIndex += TRANSPARENT_DATA_MAX_SIZE;
-            }
-            int left = data.length - currIndex;
-            addTransparentDataChunk(data, currIndex, left);
+    public void addTransparentData(EncodedChars encodedChars) throws IOException {
+
+        // data size greater than TRANSPARENT_MAX_SIZE, so slice
+        int numTransData = encodedChars.getLength() / TRANSPARENT_DATA_MAX_SIZE;
+        int currIndex = 0;
+        for (int transDataCnt = 0; transDataCnt < numTransData; transDataCnt++) {
+            addTransparentDataChunk(encodedChars, currIndex, TRANSPARENT_DATA_MAX_SIZE);
+            currIndex += TRANSPARENT_DATA_MAX_SIZE;
         }
-    }
+        int left = encodedChars.getLength() - currIndex;
+        addTransparentDataChunk(encodedChars, currIndex, left);
 
-    private void addTransparentDataChunk(byte[] data) throws IOException {
-        addTransparentDataChunk(data, 0, data.length);
     }
 
-    private void addTransparentDataChunk(byte[] data, int offset, int length) throws IOException {
-        if (length > TRANSPARENT_MAX_SIZE) {
-            // Check that we are not exceeding the maximum length
-            throw new IllegalArgumentException(
-                    "Transparent data is longer than " + TRANSPARENT_MAX_SIZE + " bytes");
-        }
+
+
+    private void addTransparentDataChunk(EncodedChars encodedChars, int offset, int length) throws IOException {
         newControlSequence();
-        write(data, offset, length);
+        encodedChars.writeTo(baout, offset, length);
         commit(chained(TRN));
     }
 
Index: src/java/org/apache/fop/afp/ptoca/TextDataInfoProducer.java
===================================================================
--- src/java/org/apache/fop/afp/ptoca/TextDataInfoProducer.java	(revision 1147766)
+++ src/java/org/apache/fop/afp/ptoca/TextDataInfoProducer.java	(working copy)
@@ -22,6 +22,7 @@
 import java.io.IOException;
 
 import org.apache.fop.afp.AFPTextDataInfo;
+import org.apache.fop.afp.fonts.CharactersetEncoder;
 
 /**
  * {@link PtocaProducer} implementation that interprets {@link AFPTextDataInfo} objects.
@@ -55,8 +56,7 @@
         // Add transparent data
         String textString = textDataInfo.getString();
         String encoding = textDataInfo.getEncoding();
-        byte[] data = textString.getBytes(encoding);
-        builder.addTransparentData(data);
+        builder.addTransparentData(CharactersetEncoder.encodeSBCS(textString, encoding, false));
     }
 
 }
Index: src/documentation/content/xdocs/trunk/output.xml
===================================================================
--- src/documentation/content/xdocs/trunk/output.xml	(revision 1147766)
+++ src/documentation/content/xdocs/trunk/output.xml	(working copy)
@@ -743,6 +743,12 @@
           <a href="fonts.html#embedding"><code>referenced-fonts</code> section of the configuration file</a>.
           However, the default fonts shown above will not be embedded.
         </p>
+        <p>
+          For double byte EBCDIC encoded character sets, there is an optional tag that must be set to prevent
+          characters from being miscoded. This defaults to "false" if not specified.</p>
+          <source><![CDATA[
+        <afp-font type="CIDKeyed" codepage="T10835  " encoding="Cp937" characterset="CZTKAI" ebcdic-dbcs="true"/>]]>
+          </source>
       </section>
       <section id="afp-renderer-resolution-config">
         <title>Output Resolution</title>
