jdk/src/share/classes/java/rmi/server/UID.java
author henryjen
Tue, 10 Jun 2014 16:18:54 -0700
changeset 24865 09b1d992ca72
parent 10049 f3eb0df61c12
permissions -rw-r--r--
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo Reviewed-by: mduigou, lancea, alanb, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10049
f3eb0df61c12 7049774: UID construction appears to hang if time changed backwards
coffeys
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.rmi.server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.DataInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.DataOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A <code>UID</code> represents an identifier that is unique over time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * with respect to the host it is generated on, or one of 2<sup>16</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * "well-known" identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>The {@link #UID()} constructor can be used to generate an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * identifier that is unique over time with respect to the host it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * generated on.  The {@link #UID(short)} constructor can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * create one of 2<sup>16</sup> well-known identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>A <code>UID</code> instance contains three primitive values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li><code>unique</code>, an <code>int</code> that uniquely identifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the VM that this <code>UID</code> was generated in, with respect to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * host and at the time represented by the <code>time</code> value (an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * example implementation of the <code>unique</code> value would be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * process identifier),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  or zero for a well-known <code>UID</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <li><code>time</code>, a <code>long</code> equal to a time (as returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * by {@link System#currentTimeMillis()}) at which the VM that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>UID</code> was generated in was alive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * or zero for a well-known <code>UID</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <li><code>count</code>, a <code>short</code> to distinguish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <code>UID</code>s generated in the same VM with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>time</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>An independently generated <code>UID</code> instance is unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * over time with respect to the host it is generated on as long as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the host requires more than one millisecond to reboot and its system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * clock is never set backward.  A globally unique identifier can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * constructed by pairing a <code>UID</code> instance with a unique host
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * identifier, such as an IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author      Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author      Peter Jones
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 10049
diff changeset
    69
 * @since       1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public final class UID implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static int hostUnique;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static boolean hostUniqueSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static long lastTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static short lastCount = Short.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /** indicate compatibility with JDK 1.1.x version of class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final long serialVersionUID = 1086053664494604050L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * number that uniquely identifies the VM that this <code>UID</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * was generated in with respect to its host and at the given time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private final int unique;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * a time (as returned by {@link System#currentTimeMillis()}) at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * the VM that this <code>UID</code> was generated in was alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private final long time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * 16-bit number to distinguish <code>UID</code> instances created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * in the same VM with the same time value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final short count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Generates a <code>UID</code> that is unique over time with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * respect to the host that it was generated on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public UID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (!hostUniqueSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                hostUnique = (new SecureRandom()).nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                hostUniqueSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            unique = hostUnique;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (lastCount == Short.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                boolean interrupted = Thread.interrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    long now = System.currentTimeMillis();
10049
f3eb0df61c12 7049774: UID construction appears to hang if time changed backwards
coffeys
parents: 5506
diff changeset
   121
                    if (now == lastTime) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        // wait for time to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        try {
10049
f3eb0df61c12 7049774: UID construction appears to hang if time changed backwards
coffeys
parents: 5506
diff changeset
   124
                            Thread.sleep(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    } else {
10049
f3eb0df61c12 7049774: UID construction appears to hang if time changed backwards
coffeys
parents: 5506
diff changeset
   129
                        // If system time has gone backwards increase
f3eb0df61c12 7049774: UID construction appears to hang if time changed backwards
coffeys
parents: 5506
diff changeset
   130
                        // original by 1ms to maintain uniqueness
f3eb0df61c12 7049774: UID construction appears to hang if time changed backwards
coffeys
parents: 5506
diff changeset
   131
                        lastTime = (now < lastTime) ? lastTime+1 : now;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        lastCount = Short.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (interrupted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            time = lastTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            count = lastCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Creates a "well-known" <code>UID</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * There are 2<sup>16</sup> possible such well-known ids.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p>A <code>UID</code> created via this constructor will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * clash with any <code>UID</code>s generated via the no-arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param   num number for well-known <code>UID</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public UID(short num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        unique = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        time = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        count = num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Constructs a <code>UID</code> given data read from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private UID(int unique, long time, short count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this.unique = unique;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        this.time = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this.count = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Returns the hash code value for this <code>UID</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return  the hash code value for this <code>UID</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return (int) time + (int) count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Compares the specified object with this <code>UID</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * This method returns <code>true</code> if and only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * specified object is a <code>UID</code> instance with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <code>unique</code>, <code>time</code>, and <code>count</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * values as this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param   obj the object to compare this <code>UID</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return  <code>true</code> if the given object is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * this one, and <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (obj instanceof UID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            UID uid = (UID) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return (unique == uid.unique &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    count == uid.count &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    time == uid.time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns a string representation of this <code>UID</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return  a string representation of this <code>UID</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return Integer.toString(unique,16) + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            Long.toString(time,16) + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            Integer.toString(count,16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Marshals a binary representation of this <code>UID</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * a <code>DataOutput</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <p>Specifically, this method first invokes the given stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * {@link DataOutput#writeInt(int)} method with this <code>UID</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <code>unique</code> value, then it invokes the stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * {@link DataOutput#writeLong(long)} method with this <code>UID</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <code>time</code> value, and then it invokes the stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * {@link DataOutput#writeShort(int)} method with this <code>UID</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <code>count</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param   out the <code>DataOutput</code> instance to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * this <code>UID</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @throws  IOException if an I/O error occurs while performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public void write(DataOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        out.writeInt(unique);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        out.writeLong(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        out.writeShort(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Constructs and returns a new <code>UID</code> instance by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * unmarshalling a binary representation from an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <code>DataInput</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <p>Specifically, this method first invokes the given stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * {@link DataInput#readInt()} method to read a <code>unique</code> value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * then it invoke's the stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * {@link DataInput#readLong()} method to read a <code>time</code> value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * then it invoke's the stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * {@link DataInput#readShort()} method to read a <code>count</code> value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * and then it creates and returns a new <code>UID</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * that contains the <code>unique</code>, <code>time</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <code>count</code> values that were read from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param   in the <code>DataInput</code> instance to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <code>UID</code> from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return  unmarshalled <code>UID</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @throws  IOException if an I/O error occurs while performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public static UID read(DataInput in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int unique = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        long time = in.readLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        short count = in.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return new UID(unique, time, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
}