Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import java.io.ByteArrayInputStream; |
| 18 | import java.io.ByteArrayOutputStream; |
| 19 | import java.io.IOException; |
| 20 | import java.io.ObjectInputStream; |
| 21 | import java.io.ObjectOutputStream; |
| 22 | import java.io.Serializable; |
| 23 | |
| 24 | /** |
| 25 | * Exercise serialization. |
| 26 | */ |
| 27 | public class Main { |
| 28 | |
| 29 | public static void main(String[] args) { |
| 30 | testObjectSerialization(); |
| 31 | } |
| 32 | |
| 33 | static void testObjectSerialization() { |
| 34 | byte[] serialData; |
| 35 | |
| 36 | try { |
| 37 | serialData = createStream(); |
| 38 | checkStream(serialData); |
| 39 | } catch (IOException ioe) { |
| 40 | throw new RuntimeException(ioe); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static byte[] createStream() throws IOException { |
| 45 | ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); |
| 46 | ObjectOutputStream objStream = new ObjectOutputStream(byteStream); |
| 47 | |
| 48 | Sub sub = new Sub('X'); |
| 49 | objStream.writeObject(sub); |
| 50 | byte[] bytes = byteStream.toByteArray(); |
| 51 | |
| 52 | objStream.close(); |
| 53 | byteStream.close(); |
| 54 | return bytes; |
| 55 | } |
| 56 | |
| 57 | static void checkStream(byte[] input) throws IOException { |
| 58 | ByteArrayInputStream byteStream = new ByteArrayInputStream(input); |
| 59 | ObjectInputStream objStream = new ObjectInputStream(byteStream); |
| 60 | |
| 61 | Sub sub; |
| 62 | try { |
| 63 | sub = (Sub) objStream.readObject(); |
| 64 | } catch (ClassNotFoundException cnfe) { |
| 65 | throw new RuntimeException(cnfe); |
| 66 | } |
| 67 | |
| 68 | objStream.close(); |
| 69 | byteStream.close(); |
| 70 | |
| 71 | sub.check(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | class Base implements Serializable { |
| 76 | private static final long serialVersionUID = 12345; |
| 77 | |
| 78 | Boolean one; |
| 79 | Integer two; |
| 80 | String three; |
| 81 | |
| 82 | public Base() { |
| 83 | one = true; |
| 84 | two = Integer.valueOf(2); |
| 85 | three = "three"; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | class Sub extends Base { |
| 90 | private static final long serialVersionUID = 54321; |
| 91 | |
| 92 | Double four; |
| 93 | Float five; |
| 94 | private Byte six = 26; |
| 95 | Character seven = '7'; |
| 96 | Short eight; |
| 97 | long nine; |
| 98 | public Character thing; |
| 99 | |
| 100 | public Sub(char thing) { |
| 101 | four = 4.0; |
| 102 | five = 5.0f; |
| 103 | six = 6; |
| 104 | eight = 8; |
| 105 | nine = 9; |
| 106 | this.thing = thing; |
| 107 | } |
| 108 | |
| 109 | public void check() { |
| 110 | System.out.println("one=" + one + " two=" + two + " three=" + three |
| 111 | + " four=" + four + " five=" + five + " six=" + six |
| 112 | + " seven=" + seven + " eight=" + eight + " nine=" + nine |
| 113 | + " thing=" + thing); |
| 114 | } |
| 115 | } |
| 116 | |