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

The following examples show how to use java.io.InputStreamReader#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: 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 2
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);
}
 
Example 3
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 4
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 5
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 6
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 7
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 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 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 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 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 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 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 14
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);
}