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 main(String[] args)29 public static void main(String[] args) { 30 testObjectSerialization(); 31 } 32 testObjectSerialization()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 createStream()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 Inner inner = new Inner(0xCAFEF00D); 51 objStream.writeObject(inner); 52 byte[] bytes = byteStream.toByteArray(); 53 54 objStream.close(); 55 byteStream.close(); 56 return bytes; 57 } 58 checkStream(byte[] input)59 static void checkStream(byte[] input) throws IOException { 60 ByteArrayInputStream byteStream = new ByteArrayInputStream(input); 61 ObjectInputStream objStream = new ObjectInputStream(byteStream); 62 63 Sub sub; 64 Inner inner; 65 try { 66 sub = (Sub) objStream.readObject(); 67 inner = (Inner) objStream.readObject(); 68 } catch (ClassNotFoundException cnfe) { 69 throw new RuntimeException(cnfe); 70 } 71 72 objStream.close(); 73 byteStream.close(); 74 75 sub.check(); 76 inner.check(); 77 } 78 79 static class Inner implements Serializable { 80 private static final long serialVersionUID = 319009; 81 private final int x; Inner(int x)82 public Inner (int x) { 83 this.x = x; 84 } 85 check()86 public void check() { 87 System.out.println("x=" + Integer.toHexString(x)); 88 } 89 } 90 } 91 92 class Base implements Serializable { 93 private static final long serialVersionUID = 12345; 94 95 Boolean one; 96 Integer two; 97 String three; 98 Base()99 public Base() { 100 one = true; 101 two = Integer.valueOf(2); 102 three = "three"; 103 } 104 } 105 106 class Sub extends Base { 107 private static final long serialVersionUID = 54321; 108 109 Double four; 110 Float five; 111 private Byte six = 26; 112 Character seven = '7'; 113 Short eight; 114 long nine; 115 public Character thing; 116 Sub(char thing)117 public Sub(char thing) { 118 four = 4.0; 119 five = 5.0f; 120 six = 6; 121 eight = 8; 122 nine = 9; 123 this.thing = thing; 124 } 125 check()126 public void check() { 127 System.out.println("one=" + one + " two=" + two + " three=" + three 128 + " four=" + four + " five=" + five + " six=" + six 129 + " seven=" + seven + " eight=" + eight + " nine=" + nine 130 + " thing=" + thing); 131 } 132 } 133