【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-ConnectionID.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package multiplexer;
import java.io.*;
/**
* Instances of this class identify a virtual connection.
*/
class ConnectionID {
private byte[] id;
/**
* This constructor is intended to be called by a
* <code>ConnectionIDFactory</code> object that creates
* unique identifying information
* @param myId An array of bytes with content that uniquely
* identifies the local end of the actual
* connection.
* @param serialNum A value that uniquely identifies a
* virtual connection created on this end
* of the actual connection.
*/
ConnectionID(byte[] myId, int serialNum) {
int idLength = myId.length
+ ConnectionIDFactory.SERIAL_NUMBER_LENGTH;
id = new byte[idLength];
int serialNumberOffset = myId.length;
System.arraycopy(myId, 0, id, 0, serialNumberOffset);
switch (ConnectionIDFactory.SERIAL_NUMBER_LENGTH ) {
case 4:
id[serialNumberOffset+3] = (byte)(serialNum>>24);
case 3:
id[serialNumberOffset+2] = (byte)(serialNum>>16);
case 2:
id[serialNumberOffset+1] = (byte)(serialNum>>8);
case 1:
id[serialNumberOffset] = (byte)serialNum;
} // switch
} // constructor(byte[], int)
/**
* The constructor is called internally by the
* <code>readConnectionID</code> method to create
* <code>ConnectionID</code> objects from data in an input
* stream.
* @param myId An array of bytes with content that uniquely
* identifies the local end of the actual
* connection. This array is used directly by
* the new object and is not copied.
*/
private ConnectionID(byte[] myId) {
this.id = myId;
} // constructor(byte[])
/**
* Read a ConnectionID from the given <code>InputStream</code>.
* @param in The InputStream to read from.
* @param byteSize The number of bytes of id informationto
* read. This is needed because each end of
* an actual connection may use a different
* number of bytes.
*/
static ConnectionID read(InputStream in,
int size) throws IOException {
byte[] id = new byte[size];
if (in.read(id, 0, size) != size) {
throw new IOException();
} // if
return new ConnectionID(id);
} // readConnectionID(InputStream)
/**
* Write the bytes of identifying information in this
* ConnectionID to the given OutputStream.
*/
void write(OutputStream out) throws IOException {
out.write(id);
} // writeConnectionId(OutputStream)
/**
* Return true if this method's argument is a ConnectionID
* that contains the same byte values as this ConnectionID
* object.
*/
public boolean equals(Object obj) {
if (obj instanceof ConnectionID) {
ConnectionID other = (ConnectionID)obj;
if (id.length == other.id.length) {
for (int i=0; i<id.length; i++) {
if (id[i]!=other.id[i]) {
return false;
} // if
} // for
return true;
} // if length
} // if
return false;
} // equals(Object)
/**
* Return a hashcode based on the contents of this object.
*/
public int hashCode() {
long h = 1234;
for (int i = id.length; --i >= 0; )
h ^= id[i] * (i + 1);
return (int)((h >> 32) ^ h);
} // hashCode()
/**
* Return a string representation of this ConnectionID.
*/
public String toString() {
StringBuffer buf = new StringBuffer(id.length*4+15);
buf.append("ConnectionID[");
if (id.length>0) {
buf.append(Integer.toString(id[0]));
for (int i=1; i<id.length; i++) {
buf.append(',').append(Integer.toString(id[i]));
} // for
} // if
buf.append(']');
return buf.toString();
} // toString()
} // class ConnectionID
上一篇:
ConnectionHelper.java
下一篇:
超市进销存管理系统的开发