EncodingInfo.java 3.93 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.day.jcr.vault.util.xml.serialize;

import com.day.jcr.vault.util.xml.xerces.util.EncodingMap;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;

public class EncodingInfo {
    private static Method fgGetConverterMethod = null;
    private static Method fgCanConvertMethod = null;
    private static boolean fgConvertersAvailable = false;
    private Object[] fArgsForMethod = null;
    String ianaName;
    String javaName;
    int lastPrintable;
    Object fCharToByteConverter = null;
    boolean fHaveTriedCToB = false;
    Charset nioCharset = null;
    CharsetEncoder nioCharEncoder = null;

    public EncodingInfo(String ianaName, String javaName, int lastPrintable) {
        this.ianaName = ianaName;
        this.javaName = EncodingMap.getIANA2JavaMapping(ianaName);
        this.lastPrintable = lastPrintable;
        try {
            this.nioCharset = Charset.forName(this.javaName);
            if (this.nioCharset.canEncode()) {
                this.nioCharEncoder = this.nioCharset.newEncoder();
            }
        }
        catch (IllegalCharsetNameException ie) {
            this.nioCharset = null;
            this.nioCharEncoder = null;
        }
        catch (UnsupportedCharsetException ue) {
            this.nioCharset = null;
            this.nioCharEncoder = null;
        }
    }

    public String getIANAName() {
        return this.ianaName;
    }

    public Writer getWriter(OutputStream output) throws UnsupportedEncodingException {
        if (this.javaName != null) {
            return new OutputStreamWriter(output, this.javaName);
        }
        this.javaName = EncodingMap.getIANA2JavaMapping(this.ianaName);
        if (this.javaName == null) {
            return new OutputStreamWriter(output, "UTF8");
        }
        return new OutputStreamWriter(output, this.javaName);
    }

    public boolean isPrintable(char ch) {
        if (ch <= this.lastPrintable) {
            return true;
        }
        if (this.nioCharEncoder != null) {
            return this.nioCharEncoder.canEncode(ch);
        }
        if (this.fCharToByteConverter == null) {
            if (this.fHaveTriedCToB || !fgConvertersAvailable) {
                return false;
            }
            if (this.fArgsForMethod == null) {
                this.fArgsForMethod = new Object[1];
            }
            try {
                this.fArgsForMethod[0] = this.javaName;
                this.fCharToByteConverter = fgGetConverterMethod.invoke(null, this.fArgsForMethod);
            }
            catch (Exception e) {
                this.fHaveTriedCToB = true;
                return false;
            }
        }
        try {
            this.fArgsForMethod[0] = new Character(ch);
            return (Boolean)fgCanConvertMethod.invoke(this.fCharToByteConverter, this.fArgsForMethod);
        }
        catch (Exception e) {
            this.fCharToByteConverter = null;
            this.fHaveTriedCToB = false;
            return false;
        }
    }

    public static void testJavaEncodingName(String name) throws UnsupportedEncodingException {
        byte[] bTest = new byte[]{118, 97, 108, 105, 100};
        String s = new String(bTest, name);
    }

    static {
        try {
            Class clazz = Class.forName("sun.io.CharToByteConverter");
            fgGetConverterMethod = clazz.getMethod("getConverter", String.class);
            fgCanConvertMethod = clazz.getMethod("canConvert", Character.TYPE);
            fgConvertersAvailable = true;
        }
        catch (Exception exc) {
            fgGetConverterMethod = null;
            fgCanConvertMethod = null;
            fgConvertersAvailable = false;
        }
    }
}