< prev index next >

## src/java.base/share/classes/java/util/Random.java

Print this page

    1162     /**
    1163      * Serializable fields for Random.
    1164      *
    1165      * @serialField    seed long
    1166      *              seed for random computations
    1167      * @serialField    nextNextGaussian double
    1168      *              next Gaussian to be returned
    1169      * @serialField      haveNextNextGaussian boolean
    1170      *              nextNextGaussian is valid
    1171      */
    1172     @java.io.Serial
    1173     private static final ObjectStreamField[] serialPersistentFields = {
    1174         new ObjectStreamField("seed", Long.TYPE),
    1175         new ObjectStreamField("nextNextGaussian", Double.TYPE),
    1176         new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
    1177     };
    1178
    1179     /**
    1180      * Reconstitute the {@code Random} instance from a stream (that is,
    1181      * deserialize it).

    1182      */
    1183     @java.io.Serial
    1184     private void readObject(java.io.ObjectInputStream s)
    1185         throws java.io.IOException, ClassNotFoundException {
    1186
    1187         ObjectInputStream.GetField fields = s.readFields();
    1188
    1189         // The seed is read in as {@code long} for
    1190         // historical reasons, but it is converted to an AtomicLong.
    1191         long seedVal = fields.get("seed", -1L);
    1192         if (seedVal < 0)
    1193           throw new java.io.StreamCorruptedException(
    1194                               "Random: invalid seed");
    1195         resetSeed(seedVal);
    1196         nextNextGaussian = fields.get("nextNextGaussian", 0.0);
    1197         haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
    1198     }
    1199
    1200     /**
    1201      * Save the {@code Random} instance to a stream.

    1202      */
    1203     @java.io.Serial
    1204     private synchronized void writeObject(ObjectOutputStream s)
    1205         throws IOException {
    1206
    1207         // set the values of the Serializable fields
    1208         ObjectOutputStream.PutField fields = s.putFields();
    1209
    1210         // The seed is serialized as a long for historical reasons.
    1211         fields.put("seed", seed.get());
    1212         fields.put("nextNextGaussian", nextNextGaussian);
    1213         fields.put("haveNextNextGaussian", haveNextNextGaussian);
    1214
    1215         // save them
    1216         s.writeFields();
    1217     }
    1218
    1219     // Support for resetting seed while deserializing
    1220     private static final Unsafe unsafe = Unsafe.getUnsafe();
    1221     private static final long seedOffset;
    1162     /**
    1163      * Serializable fields for Random.
    1164      *
    1165      * @serialField    seed long
    1166      *              seed for random computations
    1167      * @serialField    nextNextGaussian double
    1168      *              next Gaussian to be returned
    1169      * @serialField      haveNextNextGaussian boolean
    1170      *              nextNextGaussian is valid
    1171      */
    1172     @java.io.Serial
    1173     private static final ObjectStreamField[] serialPersistentFields = {
    1174         new ObjectStreamField("seed", Long.TYPE),
    1175         new ObjectStreamField("nextNextGaussian", Double.TYPE),
    1176         new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
    1177     };
    1178
    1179     /**
    1180      * Reconstitute the {@code Random} instance from a stream (that is,
    1181      * deserialize it).
    1182      *
    1183      * @param  s the {@code ObjectInputStream} from which data is read
    1184      * @throws IOException if an I/O error occurs
    1185      * @throws ClassNotFoundException if a serialized class cannot be loaded
    1186      */
    1187     @java.io.Serial
    1188     private void readObject(java.io.ObjectInputStream s)
    1189         throws java.io.IOException, ClassNotFoundException {
    1190
    1191         ObjectInputStream.GetField fields = s.readFields();
    1192
    1193         // The seed is read in as {@code long} for
    1194         // historical reasons, but it is converted to an AtomicLong.
    1195         long seedVal = fields.get("seed", -1L);
    1196         if (seedVal < 0)
    1197           throw new java.io.StreamCorruptedException(
    1198                               "Random: invalid seed");
    1199         resetSeed(seedVal);
    1200         nextNextGaussian = fields.get("nextNextGaussian", 0.0);
    1201         haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
    1202     }
    1203
    1204     /**
    1205      * Save the {@code Random} instance to a stream.
    1206      *
    1207      * @param  s the {@code ObjectOutputStream} to which data is written
    1208      * @throws IOException if an I/O error occurs
    1209      */
    1210     @java.io.Serial
    1211     private synchronized void writeObject(ObjectOutputStream s)
    1212         throws IOException {
    1213
    1214         // set the values of the Serializable fields
    1215         ObjectOutputStream.PutField fields = s.putFields();
    1216
    1217         // The seed is serialized as a long for historical reasons.
    1218         fields.put("seed", seed.get());
    1219         fields.put("nextNextGaussian", nextNextGaussian);
    1220         fields.put("haveNextNextGaussian", haveNextNextGaussian);
    1221
    1222         // save them
    1223         s.writeFields();
    1224     }
    1225
    1226     // Support for resetting seed while deserializing
    1227     private static final Unsafe unsafe = Unsafe.getUnsafe();
    1228     private static final long seedOffset;

< prev index next >