下面列出了java.io.InputStreamReader#getEncoding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}