BashStreams.java revision 2567
0N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A/* @test
0N/A * @summary Stochastic test of charset-based streams
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.nio.charset.*;
0N/A
0N/A
0N/Apublic class BashStreams {
0N/A
0N/A static final PrintStream log = System.err;
0N/A
0N/A
0N/A static class CharacterGenerator {
0N/A
0N/A private final Random rand;
0N/A private final int max;
0N/A private final int limit;
0N/A private int count = 0;
0N/A
0N/A CharacterGenerator(long seed, String csn, int limit) {
0N/A rand = new Random(seed);
0N/A this.max = Character.MAX_CODE_POINT + 1;
0N/A this.limit = limit;
0N/A }
0N/A
0N/A private char[] saved = new char[10];
0N/A private int savedCount = 0;
0N/A
0N/A void push(char c) {
0N/A saved[savedCount++] = c;
0N/A count--;
0N/A }
0N/A
0N/A int count() {
0N/A return count;
0N/A }
0N/A
0N/A boolean hasNext() {
0N/A return count < limit;
0N/A }
0N/A
0N/A char next() {
0N/A if (count >= limit)
0N/A throw new RuntimeException("EOF");
0N/A if (savedCount > 0) {
0N/A savedCount--;
0N/A count++;
0N/A return saved[savedCount];
0N/A }
0N/A int c;
0N/A for (;;) {
0N/A c = rand.nextInt(max);
0N/A if ((Character.isBmpCodePoint(c)
0N/A && (Character.isSurrogate((char) c)
0N/A || (c == 0xfffe) || (c == 0xffff))))
0N/A continue;
0N/A if (Character.isSupplementaryCodePoint(c)
0N/A && (count == limit - 1))
0N/A continue;
0N/A break;
0N/A }
0N/A count++;
0N/A if (Character.isSupplementaryCodePoint(c)) {
0N/A count++;
0N/A push(Character.lowSurrogate(c));
0N/A return Character.highSurrogate(c);
0N/A }
0N/A return (char)c;
0N/A }
0N/A
0N/A }
0N/A
0N/A
0N/A static void mismatch(String csn, int count, char c, char d) {
0N/A throw new RuntimeException(csn + ": Mismatch at count "
0N/A + count
0N/A + ": " + Integer.toHexString(c)
0N/A + " != "
0N/A + Integer.toHexString(d));
0N/A }
0N/A
0N/A static void mismatchedEOF(String csn, int count, int cgCount) {
0N/A throw new RuntimeException(csn + ": Mismatched EOFs: "
0N/A + count
0N/A + " != "
0N/A + cgCount);
0N/A }
0N/A
0N/A
0N/A static class Sink // One abomination...
0N/A extends OutputStream
0N/A implements WritableByteChannel
0N/A {
686N/A
686N/A private final String csn;
0N/A private final CharacterGenerator cg;
0N/A private int count = 0;
0N/A
0N/A Sink(String csn, long seed) {
0N/A this.csn = csn;
0N/A this.cg = new CharacterGenerator(seed, csn, Integer.MAX_VALUE);
0N/A }
0N/A
0N/A public void write(int b) throws IOException {
0N/A write (new byte[] { (byte)b }, 0, 1);
0N/A }
0N/A
0N/A private int check(byte[] ba, int off, int len) throws IOException {
0N/A String s = new String(ba, off, len, csn);
0N/A int n = s.length();
0N/A for (int i = 0; i < n; i++) {
0N/A char c = s.charAt(i);
0N/A char d = cg.next();
0N/A if (c != d) {
0N/A if (c == '?') {
0N/A if (Character.isHighSurrogate(d))
0N/A cg.next();
0N/A continue;
0N/A }
0N/A mismatch(csn, count + i, c, d);
0N/A }
0N/A }
0N/A count += n;
0N/A return len;
0N/A }
0N/A
0N/A public void write(byte[] ba, int off, int len) throws IOException {
0N/A check(ba, off, len);
0N/A }
0N/A
0N/A public int write(ByteBuffer bb) throws IOException {
0N/A int n = check(bb.array(),
0N/A bb.arrayOffset() + bb.position(),
0N/A bb.remaining());
0N/A bb.position(bb.position() + n);
0N/A return n;
0N/A }
0N/A
0N/A public void close() {
0N/A count = -1;
0N/A }
0N/A
0N/A public boolean isOpen() {
0N/A return count >= 0;
0N/A }
0N/A
0N/A }
0N/A
0N/A static void testWrite(String csn, int limit, long seed, Writer w)
0N/A throws IOException
0N/A {
0N/A Random rand = new Random(seed);
0N/A CharacterGenerator cg = new CharacterGenerator(seed, csn,
0N/A Integer.MAX_VALUE);
0N/A int count = 0;
0N/A char[] ca = new char[16384];
0N/A
0N/A int n = 0;
0N/A while (count < limit) {
0N/A n = rand.nextInt(ca.length);
0N/A for (int i = 0; i < n; i++)
0N/A ca[i] = cg.next();
0N/A w.write(ca, 0, n);
0N/A count += n;
0N/A }
0N/A if (Character.isHighSurrogate(ca[n - 1]))
0N/A w.write(cg.next());
0N/A w.close();
0N/A }
0N/A
0N/A static void testStreamWrite(String csn, int limit, long seed)
0N/A throws IOException
0N/A {
0N/A log.println(" write stream");
0N/A testWrite(csn, limit, seed,
0N/A new OutputStreamWriter(new Sink(csn, seed), csn));
0N/A }
0N/A
0N/A static void testChannelWrite(String csn, int limit, long seed)
0N/A throws IOException
0N/A {
0N/A log.println(" write channel");
0N/A testWrite(csn, limit, seed,
0N/A Channels.newWriter(new Sink(csn, seed),
0N/A Charset.forName(csn)
0N/A .newEncoder()
0N/A .onMalformedInput(CodingErrorAction.REPLACE)
0N/A .onUnmappableCharacter(CodingErrorAction.REPLACE),
0N/A 8192));
0N/A }
0N/A
0N/A
0N/A static class Source // ... and another
0N/A extends InputStream
0N/A implements ReadableByteChannel
0N/A {
0N/A
0N/A private final String csn;
0N/A private final CharsetEncoder enc;
0N/A private final CharacterGenerator cg;
1801N/A private int count = 0;
1801N/A
1801N/A Source(String csn, long seed, int limit) {
1801N/A this.csn = csn.startsWith("\1") ? csn.substring(1) : csn;
1801N/A this.enc = Charset.forName(this.csn).newEncoder()
1801N/A .onMalformedInput(CodingErrorAction.REPLACE)
1801N/A .onUnmappableCharacter(CodingErrorAction.REPLACE);
1801N/A this.cg = new CharacterGenerator(seed, csn, limit);
1801N/A }
1801N/A
1801N/A public int read() throws IOException {
1801N/A byte[] b = new byte[1];
1801N/A read(b);
1801N/A return b[0];
1801N/A }
1801N/A
1801N/A private CharBuffer cb = CharBuffer.allocate(8192);
1801N/A private ByteBuffer bb = null;
1801N/A
1801N/A public int read(byte[] ba, int off, int len) throws IOException {
1801N/A if (!cg.hasNext())
1801N/A return -1;
1801N/A int end = off + len;
1801N/A int i = off;
1801N/A while (i < end) {
1801N/A if ((bb == null) || !bb.hasRemaining()) {
1801N/A cb.clear();
1801N/A while (cb.hasRemaining()) {
1801N/A if (!cg.hasNext())
1801N/A break;
1801N/A char c = cg.next();
1801N/A if (Character.isHighSurrogate(c)
1801N/A && cb.remaining() == 1) {
1801N/A cg.push(c);
1801N/A break;
1801N/A }
1801N/A cb.put(c);
1801N/A }
1801N/A cb.flip();
1801N/A if (!cb.hasRemaining())
1801N/A break;
1801N/A bb = enc.encode(cb);
1801N/A }
1801N/A int d = Math.min(bb.remaining(), end - i);
1801N/A bb.get(ba, i, d);
1801N/A i += d;
1801N/A }
1801N/A return i - off;
1801N/A }
1801N/A
1801N/A public int read(ByteBuffer bb) throws IOException {
1801N/A int n = read(bb.array(),
1801N/A bb.arrayOffset() + bb.position(),
1801N/A bb.remaining());
1801N/A if (n >= 0)
1801N/A bb.position(bb.position() + n);
1801N/A return n;
1801N/A }
1801N/A
1801N/A public void close() {
1801N/A count = -1;
1801N/A }
1801N/A
1801N/A public boolean isOpen() {
1801N/A return count != -1;
1801N/A }
1801N/A
1801N/A }
0N/A
static void testRead(String csn, int limit, long seed, int max,
Reader rd)
throws IOException
{
Random rand = new Random(seed);
CharacterGenerator cg = new CharacterGenerator(seed, csn, limit);
int count = 0;
char[] ca = new char[16384];
int n = 0;
while (count < limit) {
int rn = rand.nextInt(ca.length);
n = rd.read(ca, 0, rn);
if (n < 0)
break;
for (int i = 0; i < n; i++) {
char c = ca[i];
if (!cg.hasNext())
mismatchedEOF(csn, count + i, cg.count());
char d = cg.next();
if (c == '?') {
if (Character.isHighSurrogate(d)) {
cg.next();
continue;
}
if (d > max)
continue;
}
if (c != d)
mismatch(csn, count + i, c, d);
}
count += n;
}
if (cg.hasNext())
mismatchedEOF(csn, count, cg.count());
rd.close();
}
static void testStreamRead(String csn, int limit, long seed, int max)
throws IOException
{
log.println(" read stream");
testRead(csn, limit, seed, max,
new InputStreamReader(new Source(csn, seed, limit), csn));
}
static void testChannelRead(String csn, int limit, long seed, int max)
throws IOException
{
log.println(" read channel");
testRead(csn, limit, seed, max,
Channels.newReader(new Source(csn, seed, limit), csn));
}
static final int LIMIT = 1 << 21;
static void test(String csn, int limit, long seed, int max)
throws Exception
{
log.println();
log.println(csn);
testStreamWrite(csn, limit, seed);
testChannelWrite(csn, limit, seed);
testStreamRead(csn, limit, seed, max);
testChannelRead(csn, limit, seed, max);
}
public static void main(String[] args) throws Exception {
if (System.getProperty("os.arch").equals("ia64")) {
// This test requires 54 minutes on an Itanium-1 processor
return;
}
int ai = 0, an = args.length;
int limit;
if (ai < an)
limit = 1 << Integer.parseInt(args[ai++]);
else
limit = LIMIT;
log.println("limit = " + limit);
long seed;
if (ai < an)
seed = Long.parseLong(args[ai++]);
else
seed = System.currentTimeMillis();
log.println("seed = " + seed);
test("UTF-8", limit, seed, Integer.MAX_VALUE);
test("US-ASCII", limit, seed, 0x7f);
test("ISO-8859-1", limit, seed, 0xff);
}
}