jdk/src/java.base/share/specs/serialization/examples.md
changeset 45534 f0b3d467215e
parent 45473 03c5450b6e4a
parent 45533 e6707cd51e28
child 45535 4b19310ae4ee
equal deleted inserted replaced
45473:03c5450b6e4a 45534:f0b3d467215e
     1 ---
       
     2 # Copyright (c) 2005, 2017, 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 include-before: '[CONTENTS](index.html) | [PREV](exceptions.html) | NEXT'
       
    24 include-after: '[CONTENTS](index.html) | [PREV](exceptions.html) | NEXT'
       
    25 
       
    26 title: 'Java Object Serialization Specification: C - Example of Serializable Fields'
       
    27 ---
       
    28 
       
    29 -   [Example Alternate Implementation of
       
    30     java.io.File](#c.1-example-alternate-implementation-of-java.io.file)
       
    31 
       
    32 -------------------------------------------------------------------------------
       
    33 
       
    34 ## C.1 Example Alternate Implementation of java.io.File
       
    35 
       
    36 This appendix provides a brief example of how an existing class could be
       
    37 specified and implemented to interoperate with the existing implementation but
       
    38 without requiring the same assumptions about the representation of the file
       
    39 name as a *String*.
       
    40 
       
    41 The system class `java.io.File` represents a filename and has methods for
       
    42 parsing, manipulating files and directories by name. It has a single private
       
    43 field that contains the current file name. The semantics of the methods that
       
    44 parse paths depend on the current path separator which is held in a static
       
    45 field. This path separator is part of the serialized state of a file so that
       
    46 file name can be adjusted when read.
       
    47 
       
    48 The serialized state of a `File` object is defined as the serializable fields
       
    49 and the sequence of data values for the file. In this case, there is one of
       
    50 each.
       
    51 
       
    52 ```
       
    53 Serializable Fields:
       
    54     String path;     // path name with embedded separators
       
    55 Serializable Data:
       
    56     char            // path name separator for path name
       
    57 ```
       
    58 
       
    59 An alternate implementation might be defined as follows:
       
    60 
       
    61 ```
       
    62 class File implements java.io.Serializable {
       
    63     ...
       
    64     private String[] pathcomponents;
       
    65     // Define serializable fields with the ObjectStreamClass
       
    66 
       
    67     /**
       
    68      * @serialField path String
       
    69      *              Path components separated by separator.
       
    70      */
       
    71 
       
    72     private static final ObjectStreamField[] serialPersistentFields
       
    73         = { new ObjectStreamField("path", String.class) };
       
    74     ...
       
    75         /**
       
    76          * @serialData  Default fields followed by separator character.
       
    77          */
       
    78 
       
    79     private void writeObject(ObjectOutputStream s)
       
    80         throws IOException
       
    81     {
       
    82         ObjectOutputStream.PutField fields = s.putFields();
       
    83         StringBuffer str = new StringBuffer();
       
    84         for(int i = 0; i < pathcomponents; i++) {
       
    85             str.append(separator);
       
    86             str.append(pathcomponents[i]);
       
    87         }
       
    88         fields.put("path", str.toString());
       
    89         s.writeFields();
       
    90         s.writeChar(separatorChar); // Add the separator character
       
    91     }
       
    92     ...
       
    93 
       
    94     private void readObject(ObjectInputStream s)
       
    95         throws IOException
       
    96     {
       
    97         ObjectInputStream.GetField fields = s.readFields();
       
    98         String path = (String)fields.get("path", null);
       
    99         ...
       
   100         char sep = s.readChar(); // read the previous separator char
       
   101 
       
   102         // parse path into components using the separator
       
   103         // and store into pathcomponents array.
       
   104     }
       
   105 }
       
   106 ```
       
   107 
       
   108 -------------------------------------------------------------------------------
       
   109 
       
   110 *[Copyright](../../../legal/SMICopyright.html) &copy; 2005, 2017, Oracle
       
   111 and/or its affiliates. All rights reserved.*