1/*
2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 4217676
26 * @summary Ensure that object streams support serialization of long strings
27 *          (strings whose UTF representation > 64k in length)
28 * @key randomness
29 */
30
31import java.io.*;
32import java.util.*;
33
34public class LongString {
35    public static void main(String[] args) throws Exception {
36        Random rand = new Random(System.currentTimeMillis());
37        ByteArrayOutputStream bout;
38        ByteArrayInputStream bin;
39        ObjectOutputStream oout;
40        ObjectInputStream oin;
41        FileInputStream fin;
42        File mesgf;
43
44        // generate a long random string
45        StringBuffer sbuf = new StringBuffer();
46        for (int i = 0; i < 100000; i++)
47            sbuf.append((char) rand.nextInt(Character.MAX_VALUE + 1));
48        String str = sbuf.toString();
49
50        // write and read long string
51        bout = new ByteArrayOutputStream();
52        oout = new ObjectOutputStream(bout);
53        oout.writeObject(str);
54        oout.flush();
55        bin = new ByteArrayInputStream(bout.toByteArray());
56        oin = new ObjectInputStream(bin);
57        String strcopy = (String) oin.readObject();
58        if (! str.equals(strcopy))
59            throw new Error("deserialized long string not equal to original");
60
61        // test backwards compatibility
62        String mesg = "Message in golden file";
63        bout = new ByteArrayOutputStream();
64        oout = new ObjectOutputStream(bout);
65        oout.writeObject(mesg);
66        oout.flush();
67        byte[] buf1 = bout.toByteArray();
68
69        mesgf = new File(System.getProperty("test.src", "."), "mesg.ser");
70        fin = new FileInputStream(mesgf);
71        bout = new ByteArrayOutputStream();
72        try {
73            while (fin.available() > 0)
74                bout.write(fin.read());
75        } finally {
76            fin.close();
77        }
78        byte[] buf2 = bout.toByteArray();
79
80        if (! Arrays.equals(buf1, buf2))
81            throw new Error("incompatible string format (write)");
82
83        fin = new FileInputStream(mesgf);
84        try {
85            oin = new ObjectInputStream(fin);
86            String mesgcopy = (String) oin.readObject();
87            if (! mesg.equals(mesgcopy))
88                throw new Error("incompatible string format (read)");
89        } finally {
90            fin.close();
91        }
92    }
93}
94