286N/A/*
286N/A * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/A/*
286N/A * @test
286N/A * @bug 4127657
286N/A * @summary Check for correct handling of parameters to
286N/A * XXXXWriter.write(b, off, len).
286N/A *
286N/A */
286N/A
286N/Aimport java.io.*;
286N/A
286N/Apublic class WriteParams {
286N/A static int values[] = {Integer.MIN_VALUE, -1, 0, 1, 4, 16, 31,
286N/A 32, 33, Integer.MAX_VALUE};
286N/A static char b[][] = {null, new char[32]};
286N/A
286N/A static void test(Writer wtr) throws Exception {
286N/A int i = 0, j = 0, k = 0;
286N/A boolean nullPtr = false, indexOutBnd = false;
286N/A
286N/A for (i = 0; i < b.length; i++) {
286N/A for ( j = 0; j < values.length; j++) {
286N/A for ( k = 0; k < values.length; k++) {
286N/A
286N/A nullPtr = (b[i] == null);
286N/A
286N/A int bufLen = nullPtr ? 0 : b[i].length;
286N/A indexOutBnd = ((values[j] + values[k]) < 0)
286N/A || (values[j] < 0)
286N/A || (values[j] > bufLen)
286N/A || (values[k] < 0)
286N/A || ((values[j] + values[k]) > bufLen);
286N/A
286N/A try {
286N/A wtr.write(b[i], values[j], values[k]);
286N/A } catch (NullPointerException e) {
286N/A if (!nullPtr) {
286N/A throw new Exception
286N/A ("should not throw NullPointerException");
286N/A }
286N/A continue;
286N/A } catch (IndexOutOfBoundsException e) {
286N/A if (!indexOutBnd) {
286N/A throw new Exception
286N/A ("should not throw IndexOutOfBoundsException");
286N/A }
286N/A continue;
286N/A }
286N/A
286N/A if (nullPtr || indexOutBnd) {
286N/A throw new Exception("Should have thrown an exception");
286N/A }
286N/A }
286N/A }
286N/A }
286N/A }
286N/A
286N/A public static void main(String args[]) throws Exception{
286N/A StringWriter sw = new StringWriter();
286N/A
286N/A test(sw);
286N/A
286N/A test(new BufferedWriter(sw));
286N/A
286N/A test(new CharArrayWriter());
286N/A
286N/A test(new OutputStreamWriter(System.err));
286N/A
286N/A test(new PipedWriter(new PipedReader()));
286N/A }
286N/A}
286N/A