Path: blob/master/test/jdk/java/io/Serializable/available/Available.java
41153 views
/*1* Copyright (c) 2000, 2001, 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/* @test24* @bug 440057125* @summary Verify that ObjectInputStream.available() functions properly when26* called at the beginning of a data block, and that it does not27* attempt any read operations that may potentially block.28*/2930import java.io.*;3132public class Available {33public static void main(String[] args) throws Exception {34ObjectOutputStream oout;35ByteArrayOutputStream bout;36byte[] buf;3738// write stream containing short data block39oout = new ObjectOutputStream(bout = new ByteArrayOutputStream());40oout.write(new byte[100]);41oout.close();42buf = bout.toByteArray();4344// 4 byte stream header + 2 byte block header + data45if ((getAvailable(buf, 4) != 0) || (getAvailable(buf, 5) != 0)) {46throw new Error();47}48for (int i = 0; i < 100; i++) {49if (getAvailable(buf, 6 + i) != i) {50throw new Error();51}52}5354// write stream containing long data block55oout = new ObjectOutputStream(bout = new ByteArrayOutputStream());56oout.write(new byte[500]);57oout.close();58buf = bout.toByteArray();5960// 4 byte stream header + 5 byte block header + data61for (int i = 4; i < 9; i++) {62if (getAvailable(buf, i) != 0) {63throw new Error();64}65}66for (int i = 0; i < 500; i++) {67if (getAvailable(buf, 9 + i) != i) {68throw new Error();69}70}71}7273/**74* Given a byte array containing a serialized stream, creates a copy of the75* given data truncated to the specified length, then returns the result of76* a call to available() on an ObjectInputStream created on top of the77* truncated data. As a side effect, a StreamCorrupted or EOFException78* will get thrown if the available() call attempts to read past the79* underlying stream's available data.80*/81static int getAvailable(byte[] data, int truncateLen) throws IOException {82byte[] trunc = new byte[truncateLen];83System.arraycopy(data, 0, trunc, 0, truncateLen);84return new ObjectInputStream(85new ByteArrayInputStream(trunc)).available();86}87}888990