Path: blob/master/test/jdk/java/io/Serializable/notAvailable/NotAvailable.java
41153 views
/*1* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 408575626* @summary Ensure readObject() works when InputStream.available() is not implemented.27* In JDK 1.1.x, Win32 System Console available() thows IOException28* to denote that it is not implemented.29*/3031import java.io.*;3233public class NotAvailable {34public static void main(String[] args) throws Exception {35ByteArrayOutputStream baos;36ObjectOutput out;37try {38// Write class out39baos = new ByteArrayOutputStream();40out = new ObjectOutputStream(baos);41out.writeObject(new Class1(22,33));42out.writeObject(new Class1(22,33));43out.flush();44} catch (IOException e) {45e.printStackTrace();46throw e;47}4849ObjectInputStream in = null;50try {51// Read it back52ByteArrayInputStream bois =53new ByteArrayInputStream(baos.toByteArray()) {54/* simulate available() not being implemented. */55public int available() {56throw new Error("available() is not implemented");57}58};59in = new ObjectInputStream(bois);60Class1 cc1 = (Class1) in.readObject();61Class1 cc2 = (Class1) in.readObject();62} catch (IOException e) {63e.printStackTrace();64throw e;65} catch (ClassNotFoundException e) {66e.printStackTrace();67throw e;68} finally {69if (in != null)70in.close();71if (out != null)72out.close();73}74}75}7677class Class1 implements Serializable {78private static final long serialVersionUID = 1L;7980int a, b;8182public Class1(int aa, int bb) {83a = aa;84b = bb;85}86}878889