Java Code Examples for java.io.OutputStreamWriter#getEncoding()

The following examples show how to use java.io.OutputStreamWriter#getEncoding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FontDescriptor.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public FontDescriptor(String nativeName, CharsetEncoder encoder,
                      int[] exclusionRanges){

    this.nativeName = nativeName;
    this.encoder = encoder;
    this.exclusionRanges = exclusionRanges;
    this.useUnicode = false;
    Charset cs = encoder.charset();
    // The following looks odd but its the only public way to get the
    // historical name if one exists and the canonical name otherwise.
    try {
        OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream(), cs);
        this.charsetName = osw.getEncoding();
        osw.close();
    } catch (IOException ioe) {
    }
}
 
Example 2
Source File: FontDescriptor.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public FontDescriptor(String nativeName, CharsetEncoder encoder,
                      int[] exclusionRanges){

    this.nativeName = nativeName;
    this.encoder = encoder;
    this.exclusionRanges = exclusionRanges;
    this.useUnicode = false;
    Charset cs = encoder.charset();
    // The following looks odd but its the only public way to get the
    // historical name if one exists and the canonical name otherwise.
    try {
        OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream(), cs);
        this.charsetName = osw.getEncoding();
        osw.close();
    } catch (IOException ioe) {
    }
}
 
Example 3
Source File: XmlUtils.java    From GpsPrune with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the default system encoding using a writer
 * @param inWriter writer object
 * @return string defining encoding
 */
public static String getEncoding(OutputStreamWriter inWriter)
{
	String encoding = inWriter.getEncoding();
	try {
		encoding =  Charset.forName(encoding).name();
	}
	catch (Exception e) {} // ignore failure to find encoding
	return encoding;
}
 
Example 4
Source File: StringUtil.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
/** Get default encoding. */
public static String getDefaultEncoding()
{
    String encoding = System.getProperty("file.encoding");
    // Java 1.5 docs for System.getProperties do not guarantee the
    // existance of file.encoding
    if (encoding != null)
        return encoding;
    OutputStreamWriter out =
        new OutputStreamWriter(new ByteArrayOutputStream());
    return out.getEncoding();
}
 
Example 5
Source File: Encodings.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 6
Source File: Encodings.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 7
Source File: Encodings.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 8
Source File: Encodings.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 9
Source File: Encodings.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 10
Source File: Encodings.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 11
Source File: Encodings.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 12
Source File: Encodings.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 13
Source File: Encodings.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 14
Source File: Encodings.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 15
Source File: KafkaEagleJDK.java    From kafka-eagle with Apache License 2.0 4 votes vote down vote up
/** Get default charset. */
private static String getDefaultCharSet() {
	OutputStreamWriter writer = new OutputStreamWriter(new ByteArrayOutputStream());
	String enc = writer.getEncoding();
	return enc;
}
 
Example 16
Source File: Encodings.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 17
Source File: CharSetTest.java    From document-management-software with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static String getDefaultCharSet() {
    OutputStreamWriter writer = new OutputStreamWriter(new ByteArrayOutputStream());
    String enc = writer.getEncoding();
    return enc;
}
 
Example 18
Source File: Encodings.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 19
Source File: Encodings.java    From native-obfuscator with GNU General Public License v3.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
 
Example 20
Source File: Encodings.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
static void go(String enc, String str, final byte[] bytes, boolean bidir)
    throws Exception
{
    final Charset charset = Charset.forName(enc);

    /* String(byte[] bs, String enc) */
    if (!(new String(bytes, enc).equals(str)))
        throw new Exception(enc + ": String constructor failed");

    /* String(byte[] bs, Charset charset) */
    if (!(new String(bytes, charset).equals(str)))
        throw new Exception(charset + ": String constructor failed");

    /* String(byte[] bs, int off, int len, Charset charset) */
    String start = str.substring(0, 2);
    String end = str.substring(2);
    if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
        if (!(new String(bytes, 0, 4, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    } else if (enc.equals("UTF-16")) {
        if (!(new String(bytes, 0, 6, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
    } else {
        if (!(new String(bytes, 0, 2, charset).equals(start)))
            throw new Exception(charset + ": String constructor failed");
        if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
            throw new Exception(charset + ": String constructor failed");
    }

    /* InputStreamReader */
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    InputStreamReader r = new InputStreamReader(bi, enc);
    String inEnc = r.getEncoding();
    int n = str.length();
    char[] cs = new char[n];
    for (int i = 0; i < n;) {
        int m;
        if ((m = r.read(cs, i, n - i)) < 0)
            throw new Exception(enc + ": EOF on InputStreamReader");
        i += m;
    }
    if (!(new String(cs).equals(str)))
        throw new Exception(enc + ": InputStreamReader failed");

    if (!bidir) {
        System.err.println(enc + " --> " + inEnc);
        return;
    }

    /* String.getBytes(String enc) */
    byte[] bs = str.getBytes(enc);
    if (!equals(bs, bytes))
        throw new Exception(enc + ": String.getBytes failed");

    /* String.getBytes(Charset charset) */
    bs = str.getBytes(charset);
    if (!equals(bs, bytes))
        throw new Exception(charset + ": String.getBytes failed");

    // Calls to String.getBytes(Charset) shouldn't automatically
    // use the cached thread-local encoder.
    if (charset.name().equals("UTF-16BE")) {
        String s = new String(bytes, charset);
        // Replace the thread-local encoder with this one.
        byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
        if (bytes.length != bb.length) {
            // Incidental test.
            throw new RuntimeException("unequal length: "
                                       + bytes.length + " != "
                                       + bb.length);
        } else {
            boolean diff = false;
            // Expect different byte[] between UTF-16LE and UTF-16BE
            // even though encoder was previously cached by last call
            // to getBytes().
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] != bb[i])
                    diff = true;
            }
            if (!diff)
                throw new RuntimeException("byte arrays equal");
        }
    }

    /* OutputStreamWriter */
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(bo, enc);
    String outEnc = w.getEncoding();
    w.write(str);
    w.close();
    bs = bo.toByteArray();
    if (!equals(bs, bytes))
        throw new Exception(enc + ": OutputStreamWriter failed");

    System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}