5598N/A/*
5598N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
5598N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5598N/A *
5598N/A * This code is free software; you can redistribute it and/or modify it
5598N/A * under the terms of the GNU General Public License version 2 only, as
5598N/A * published by the Free Software Foundation.
5598N/A *
5598N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5598N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5598N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5598N/A * version 2 for more details (a copy is included in the LICENSE file that
5598N/A * accompanied this code).
5598N/A *
5598N/A * You should have received a copy of the GNU General Public License version
5598N/A * 2 along with this work; if not, write to the Free Software Foundation,
5598N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5598N/A *
5598N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5598N/A * or visit www.oracle.com if you need additional information or have any
5598N/A * questions.
5598N/A */
5598N/A
5598N/A/* @test
5598N/A * @bug 4997655 7000913
5598N/A * @summary (bf) CharBuffer.slice() on wrapped CharSequence results in wrong position
5598N/A */
5598N/A
5598N/Aimport java.nio.*;
5598N/A
5598N/Apublic class StringCharBufferSliceTest {
5598N/A public static void main( String[] args) throws Exception {
5598N/A System.out.println(
5598N/A ">>> StringCharBufferSliceTest-main: testing the slice method...");
5598N/A
5598N/A final String in = "for testing";
5598N/A
5598N/A System.out.println(
5598N/A ">>> StringCharBufferSliceTest-main: testing with the position 0.");
5598N/A
5598N/A CharBuffer buff = CharBuffer.wrap(in);
5598N/A test(buff, buff.slice());
5598N/A
5598N/A System.out.println(
5598N/A ">>> StringCharBufferSliceTest-main: testing with new position.");
5598N/A
5598N/A buff.position(2);
5598N/A test(buff, buff.slice());
5598N/A
5598N/A System.out.println(
5598N/A ">>> StringCharBufferSliceTest-main: testing with non zero initial position.");
5598N/A
5598N/A buff = CharBuffer.wrap(in, 3, in.length());
5598N/A test(buff, buff.slice());
5598N/A
5598N/A System.out.println(
5598N/A ">>> StringCharBufferSliceTest-main: testing slice result with get()");
5598N/A buff.position(4);
5598N/A buff.limit(7);
5598N/A CharBuffer slice = buff.slice();
5598N/A for (int i = 0; i < 3; i++) {
5598N/A if (slice.get() != buff.get()) {
5598N/A throw new RuntimeException("Wrong characters in slice result.");
5598N/A }
5598N/A }
5598N/A
5598N/A System.out.println(
5598N/A ">>> StringCharBufferSliceTest-main: testing slice result with get(int)");
5598N/A buff.position(4);
5598N/A buff.limit(7);
5598N/A slice = buff.slice();
5598N/A for (int i = 0; i < 3; i++) {
5598N/A if (slice.get(i) != buff.get(4 + i)) {
5598N/A throw new RuntimeException("Wrong characters in slice result.");
5598N/A }
5598N/A }
5598N/A
5598N/A System.out.println(
5598N/A ">>> StringCharBufferSliceTest-main: testing slice with result of slice");
5598N/A buff.position(0);
5598N/A buff.limit(buff.capacity());
5598N/A slice = buff.slice();
5598N/A for (int i=0; i<4; i++) {
5598N/A slice.position(i);
5598N/A CharBuffer nextSlice = slice.slice();
5598N/A if (nextSlice.position() != 0)
5598N/A throw new RuntimeException("New buffer's position should be zero");
5598N/A if (!nextSlice.equals(slice))
5598N/A throw new RuntimeException("New buffer should be equal");
5598N/A slice = nextSlice;
5598N/A }
5598N/A
5598N/A System.out.println(
5598N/A ">>> StringCharBufferSliceTest-main: testing toString.");
5598N/A buff.position(4);
5598N/A buff.limit(7);
5598N/A slice = buff.slice();
5598N/A if (!slice.toString().equals("tes")) {
5598N/A throw new RuntimeException("bad toString() after slice(): " + slice.toString());
5598N/A }
5598N/A
5598N/A System.out.println(
5598N/A ">>> StringCharBufferSliceTest-main: testing subSequence.");
5598N/A buff.position(4);
5598N/A buff.limit(8);
slice = buff.slice();
CharSequence subSeq = slice.subSequence(1, 3);
if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {
throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'");
}
System.out.println(
">>> StringCharBufferSliceTest-main: testing duplicate.");
buff.position(4);
buff.limit(8);
slice = buff.slice();
CharBuffer dupe = slice.duplicate();
if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e'
|| dupe.charAt(2) != 's' || dupe.charAt(3) != 't') {
throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'");
}
System.out.println(">>> StringCharBufferSliceTest-main: done!");
}
public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException {
boolean marked = false;
try {
slice.reset();
marked = true;
} catch (InvalidMarkException ime) {
// expected
}
if (marked ||
slice.position() != 0 ||
buff.remaining() != slice.limit() ||
buff.remaining() != slice.capacity()) {
throw new RuntimeException(
"Calling the CharBuffer.slice method failed.");
}
}
}