0N/A/*
2362N/A * Copyright (c) 2005, 2006, 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
0N/A * published by the Free Software Foundation.
0N/A *
0N/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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary it is a new version of an old test which was
0N/A * /src/share/test/serialization/piotest.java
0N/A * Test of serialization when there is
0N/A * exceptions on the I/O stream
0N/A *
0N/A * @build PrimitivesTest
0N/A * @run main SerializeWithException
0N/A */
0N/A
0N/Aimport java.io.*;
0N/A
0N/Apublic class SerializeWithException {
0N/A public static void main (String argv[]) {
0N/A System.err.println("\nRegression test for testing of " +
0N/A "serialization when there is exceptions on the I/O stream \n");
0N/A
0N/A try {
0N/A int i = 123456;
0N/A byte b = 12;
0N/A short s = 45;
0N/A char c = 'A';
0N/A long l = 1234567890000L;
0N/A float f = 3.14159f;
0N/A double d = f*2;
0N/A boolean z = true;
0N/A String string = "The String";
0N/A PrimitivesTest prim = new PrimitivesTest();
0N/A
0N/A /* For each of the byte offsets from 0 to 100,
0N/A do the pickling but expect an exception */
0N/A for (int offset = 0; offset < 200; offset++) {
0N/A ExceptionOutputStream ostream;
0N/A boolean expect_exception = false;
0N/A IOException exception = null;
0N/A
0N/A try {
0N/A expect_exception = true;
0N/A exception = null;
0N/A
0N/A ostream = new ExceptionOutputStream();
0N/A ostream.setExceptionOffset(offset);
0N/A ObjectOutputStream p = new ObjectOutputStream(ostream);
0N/A
0N/A p.writeInt(i);
0N/A p.writeByte(b);
0N/A p.writeShort(s);
0N/A p.writeChar(c);
0N/A p.writeLong(l);
0N/A p.writeFloat(f);
0N/A p.writeDouble(d);
0N/A p.writeBoolean(z);
0N/A p.writeUTF(string);
0N/A p.writeObject(string);
0N/A
0N/A p.writeObject(prim);
0N/A p.flush();
0N/A expect_exception = false;
0N/A } catch (IOException ee) {
0N/A exception = ee;
0N/A }
0N/A
0N/A if (expect_exception && exception == null) {
0N/A System.err.println("\nIOException did not occur at " +
0N/A "offset " + offset);
0N/A throw new Error();
0N/A }
0N/A if (!expect_exception && exception != null) {
0N/A System.err.println("\n " + exception.toString() +
0N/A " not expected at offset " + offset);
0N/A throw new Error();
0N/A }
0N/A }
0N/A System.err.println("\nTEST PASSED");
0N/A } catch (Exception e) {
0N/A System.err.print("TEST FAILED: ");
0N/A e.printStackTrace();
0N/A throw new Error();
0N/A }
0N/A }
0N/A}
0N/A
0N/Aclass ExceptionOutputStream extends OutputStream {
0N/A private int exceptionOffset = 0;
0N/A private int currentOffset = 0;
0N/A
0N/A /**
0N/A * Writes a byte to the buffer.
0N/A * @param b the byte
0N/A */
0N/A public void write(int b) throws IOException {
0N/A if (currentOffset >= exceptionOffset) {
0N/A throw new IOException("Debug exception");
0N/A }
0N/A currentOffset++;
0N/A }
0N/A
0N/A
0N/A public void setExceptionOffset(int offset) {
0N/A exceptionOffset = offset;
0N/A currentOffset = 0;
0N/A }
0N/A}