Path: blob/master/test/jdk/java/io/Serializable/longString/LongString.java
41153 views
/*1* Copyright (c) 1999, 2010, 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 421767625* @summary Ensure that object streams support serialization of long strings26* (strings whose UTF representation > 64k in length)27* @key randomness28*/2930import java.io.*;31import java.util.*;3233public class LongString {34public static void main(String[] args) throws Exception {35Random rand = new Random(System.currentTimeMillis());36ByteArrayOutputStream bout;37ByteArrayInputStream bin;38ObjectOutputStream oout;39ObjectInputStream oin;40FileInputStream fin;41File mesgf;4243// generate a long random string44StringBuffer sbuf = new StringBuffer();45for (int i = 0; i < 100000; i++)46sbuf.append((char) rand.nextInt(Character.MAX_VALUE + 1));47String str = sbuf.toString();4849// write and read long string50bout = new ByteArrayOutputStream();51oout = new ObjectOutputStream(bout);52oout.writeObject(str);53oout.flush();54bin = new ByteArrayInputStream(bout.toByteArray());55oin = new ObjectInputStream(bin);56String strcopy = (String) oin.readObject();57if (! str.equals(strcopy))58throw new Error("deserialized long string not equal to original");5960// test backwards compatibility61String mesg = "Message in golden file";62bout = new ByteArrayOutputStream();63oout = new ObjectOutputStream(bout);64oout.writeObject(mesg);65oout.flush();66byte[] buf1 = bout.toByteArray();6768mesgf = new File(System.getProperty("test.src", "."), "mesg.ser");69fin = new FileInputStream(mesgf);70bout = new ByteArrayOutputStream();71try {72while (fin.available() > 0)73bout.write(fin.read());74} finally {75fin.close();76}77byte[] buf2 = bout.toByteArray();7879if (! Arrays.equals(buf1, buf2))80throw new Error("incompatible string format (write)");8182fin = new FileInputStream(mesgf);83try {84oin = new ObjectInputStream(fin);85String mesgcopy = (String) oin.readObject();86if (! mesg.equals(mesgcopy))87throw new Error("incompatible string format (read)");88} finally {89fin.close();90}91}92}939495