⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idbytes.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
字号:
/************************************************************************
 *
 * $Id: IDBytes.java,v 1.1 2002/01/03 01:04:37 bondolo Exp $
 *
 * Copyright (c) 2001 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
 *    must not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Project JXTA.  For more
 * information on Project JXTA, please see
 * <http://www.jxta.org/>.
 *
 * This license is based on the BSD license adopted by the Apache Foundation.
 *********************************************************************************/

package net.jxta.impl.id.UUID;

import java.lang.reflect.Constructor;
import java.lang.String;
import java.net.URL;
import java.util.Arrays;
import java.util.zip.Checksum;                           // used in hashCode
import java.util.zip.CRC32;

import java.io.IOException;
import java.lang.IllegalArgumentException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.UnknownServiceException;
import java.security.NoSuchAlgorithmException;

import net.jxta.impl.id.UUID.UUID;
import net.jxta.impl.id.UUID.UUIDFactory;

/**
 * This class represents a JXTA ID. IDs are used to uniquely identify a
 * peer, a peer group and a pipe or any other objects manipulated by the
 * core. JXTA ID are represented as UUID.
 *
 * @see net.jxta.id.IDFactory
 * @see         net.jxta.impl.id.UUID.UUID
 * @see         net.jxta.impl.id.UUID.UUIDFactory
 *
 * @see         net.jxta.peergroup.PeerGroupID
 *
 * @version     $Revision: 1.1 $
 * @since JXTA 1.0
 */
public class IDBytes implements Cloneable, java.io.Serializable {

    protected byte [] bytes = null;

    public static final Instantiator INSTANTIATOR = new Instantiator();

    /**
     *  Constructor for the null case. This constructor initializes only the
     *  flag fields of the ID. This is not usually a useful
     *  operation since there are already constants. Provided for subclasses to
     *  make their own null constants.
     *
     * @since JXTA 1.0
     */

    IDBytes() {
        this.bytes = new byte[ IDFormat.IdByteArraySize ];
        this.bytes[IDFormat.flagsOffset + IDFormat.flagsIdTypeOffset] = 0;
    };


    /**
     * clone this object.
     *
     * Most of the time this object is held private and immutable, so there
     * is no need to clone it, but should the need arise (the object is used
     * in a mutable manner and shared), then this expensive clone method does
     * the correct thing.
     */
    protected Object clone() {
        IDBytes myClone = new IDBytes();
        myClone.bytes = (byte[]) bytes.clone();
        return myClone;
    }

    /**
     * Compares two IDs for equality.
     *
     *  @since JXTA 1.0
     *
     * @param target  the ID to be compared against.
     * @return	boolean true if IDs are equal, false otherwise.
     **/
    public boolean equals( Object target ) {
        if (this == target)
            return true;

        if (target instanceof IDBytes) {
            return Arrays.equals( bytes, ((IDBytes)target).bytes );
        }
        else
            return false;
    }

    /**
     * Public member calculates a hash code for this ID. Used by Hashmaps.
     *
     *  @since JXTA 1.0
     *
     * @return int Containing the hashcode of this ID.
     **/
    public int hashCode() {
        Checksum crc = new CRC32();

        crc.update( bytes, 0, bytes.length );
        return (int) crc.getValue();
    };

    /**
     * Public member which returns a string representation of the ID.
     * This implementations encodes the ID into a URI.
     *
     *  @since JXTA 1.0
     *
     * @return	String containting the URI
     **/
    public String toString( ) {
        return getUniqueValue().toString();
    };

    /**
     *
     *  Private replacement for toHexString since we need the leading 0 digits.
     *  Returns a String containing byte value encoded as 2 hex characters.
     *
     *  @param  theByte a byte containing the value to be encoded.
     *  @return	String containing byte value encoded as 2 hex characters.
     */
    private static String toHexDigits( byte theByte ) {
        final char [] HEXDIGITS = { '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        StringBuffer result = new StringBuffer(2);

        result.append( HEXDIGITS[(theByte >>> 4) & 15] );
        result.append( HEXDIGITS[theByte & 15] );

        return result.toString();
    }

    /**
     *
     *  Return an object containing the unique value of the ID. This object must
     *  provide implementations of toString() and hashCode() that are canonical
     *  and conisistent from run-to-run given the same input values. Beyond
     *  this nothing should be assumed about the nature of this object. For some
     *  implementations the object returned may be the same as provided.
     *
     *  @return	Object which can provide canonical representations of the ID.
     *
     *  @since JXTA 1.0
     */
    Object getUniqueValue() {
        StringBuffer    encoded = new StringBuffer();
        int             lastIndex;

        // find the last non-zero index.
        for( lastIndex = IDFormat.flagsOffset - 1; lastIndex > 0; lastIndex-- )
            if( 0 != bytes[lastIndex] )
                break;

        // build the string.
        for( int eachByte = 0; eachByte <= lastIndex; eachByte++ ) {
            encoded.append( toHexDigits(bytes[eachByte]).toUpperCase() );
        }

        // append the last two chars.
        encoded.append( toHexDigits(bytes[IDFormat.flagsOffset + IDFormat.flagsIdTypeOffset]).toUpperCase() );

        return encoded.toString();
    }

    /**
     * convert a long into the byte array
     *
     * @param offset offset
     * @paran value  value
     *
     *  @since JXTA 1.0
     */
    void longIntoBytes( int offset, long value ) {
        for( int eachByte = 0; eachByte < 8; eachByte ++ )
            bytes[eachByte + offset] = (byte) (value >> ((7 - eachByte) * 8L));

    }

    /**
     * return the long value
     *
     * @param offset
     * @return	long
     *
     *  @since JXTA 1.0
     */

    long bytesIntoLong( int offset ) {
        long result = 0L;

        for( int eachByte = 0; eachByte < 8; eachByte ++ )
            result |= ((long)(bytes[eachByte + offset] & 0xff)) << ((7 - eachByte) * 8L);

        return result;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -