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