📄 messageimpl.java
字号:
int offset,
int len)
{
MessageElement el = getElement (nsname);
if (el != null) {
// Remove the element
removeElement (nsname);
}
// Create a new element
el = newMessageElement(nsname,
null,
bytes,
offset,
len);
addElement(el);
}
/**
* Return an Enumeration of all namespace names used in this message.
*
* @return Enumeration of all namespaces used in this message.
*/
public Enumeration getNamespaces()
{
return namespace2elements.keys();
}
/**
* Chck for a message element with the given name.
*
* @return true if the element is present, flase otherwise.
*/
public boolean hasElement(String nsname)
{
return getElement(nsname) != null;
}
/**
* Retrieve an element by name in the specified namespace from the message.
*
* @param nsname contains the name of the element to get
* @return Element The named element, else null.
*
* @since JXTA 1.0
*/
public MessageElement getElement(String nsname) {
String names[] = MessageElement.parseName(nsname);
String namespace = names[0];
String name = names[1];
Hashtable elements = (Hashtable)namespace2elements.get(namespace);
if (elements == null)
return null;
return (MessageElement)elements.get(name);
}
/**
* Return the raw size of the message. That is roughly the size
* of the data that came in from the network, disregarding elements that
* may have been added or replaced after that. The result is meaningfull
* only before elements are manipulated in such a way.
* @return long the size
*/
public long getRawSize() {
// All data is in one byte array. Any element is an offset/size pair
// into that array. getBytesOffset() returns that array. The raw
// message size is its length.
MessageElement el = (MessageElement) order.firstElement();
byte[] bytes = el.getBytesOffset();
return bytes.length;
}
/**
* Add a MessageElement into the message.
*
* @param element the Element to add to the message.
*
* @since JXTA 1.0
*/
public void addElement(MessageElement element) {
String names[] = MessageElement.parseName(element.getName());
String namespace = names[0];
String name = names[1];
// If the elment already exists in message, just
// replace it in the vector.
MessageElement el = getElement(element.getName());
if (el != null) {
int i = order.indexOf(el);
order.setElementAt(element, i);
} else
order.addElement(element);
// Now put it in the namespace hashtable.
Hashtable namespaceElements = (Hashtable)namespace2elements.get(namespace);
if (namespaceElements == null) {
namespaceElements = new Hashtable();
namespace2elements.put(namespace, namespaceElements);
}
namespaceElements.put(name, element);
}
/**
* Returns an enumeration of all of the elements contained in this message.
* Elements from all namespaces are returned.
* <P>
* The Enumeration returned is not synchronized with the message. If you
* modify the state of the Message, the enumeration will not reflect your
* changes.
* <P>
* Sames as getElementsInLifoOrder()
*
* @return Enumeration of Elements.
*
* @since JXTA 1.0
*/
public MessageElementEnumeration getElements() {
return getElementsInLifoOrder();
}
/**
* Get MessageElements in First-In-First-Out order.
*
* @deprecated
*/
public MessageElementEnumeration getElementsInFifoOrder() {
return new MessageElementEnumerationImpl(order.elements());
}
/**
* @deprecated
**/
public MessageElementEnumeration getElementsInLifoOrder() {
Vector v = new Vector();
Enumeration e = order.elements();
while(e.hasMoreElements()) {
v.add(0, e.nextElement());
}
return new MessageElementEnumerationImpl(v.elements());
}
/**
* Return a StringEnumeration for all the message
* element names in this message.
*/
public StringEnumeration getNames()
{
return new StringEnumeration(new Enumeration() {
private Enumeration elements = getElementsInLifoOrder();
public boolean hasMoreElements()
{
return elements.hasMoreElements();
}
public Object nextElement()
{
MessageElement element = (MessageElement) elements.nextElement();
return element.getName();
}
});
}
/**
* Returns an enumeration of all of the elements contained in this message
* in the given namespace.
*
* The Enumeration returned is not synchronized with the message. If you
* modify the state of the Message, the enumeration will not reflect your
* changes.
*
* @return Enumeration of Elements.
*
* @since JXTA 1.0
*/
public MessageElementEnumeration getElements(final String namespace)
{
return new MessageElementEnumerationImpl(new Enumeration() {
private Hashtable elementsTable = (Hashtable)namespace2elements.get(namespace);
private Enumeration elements = elementsTable == null ? null : elementsTable.elements();
public boolean hasMoreElements()
{
return elements != null && elements.hasMoreElements();
}
public Object nextElement()
{
if (elements == null || !elements.hasMoreElements()) {
return null;
}
return elements.nextElement();
}
});
}
/**
* Duplicates the Message. This duplicate is a real copy.
*
* @return Message a Message that is a copy of the original message
*
* @since JXTA 1.0
*/
public Object clone()
{
Message m = new MessageImpl();
Enumeration e = getElements();
while(e.hasMoreElements()) {
MessageElement el = (MessageElement)e.nextElement();
// MessageElements are immutable, so reuse the element.
m.addElement(el);
}
return m;
}
/**
* True if the elements are in the same order and all equal.
*/
public boolean equals(Object o)
{
Message m2 = (Message)o;
MessageElementEnumeration e1 = getElements();
MessageElementEnumeration e2 = m2.getElements();
while(e1.hasMoreElements()) {
if (!e2.hasMoreElements())
return false;
MessageElement el1 = e1.nextMessageElement();
MessageElement el2 = e2.nextMessageElement();
if (!el1.equals(el2))
return false;
}
return !e2.hasMoreElements();
}
public EndpointAddress getSourceAddress()
{
EndpointAddress srcAddr = null;
byte[] bytes = getBytes("jxta:EndpointSourceAddress");
if (bytes != null) {
srcAddr = new Address(bytes);
}
return srcAddr;
}
public EndpointAddress getDestinationAddress()
{
EndpointAddress dstAddr = null;
byte[] bytes = getBytes("jxta:EndpointDestinationAddress");
if (bytes != null) {
dstAddr = new Address(bytes);
}
return dstAddr;
}
/**
* @deprecated
**/
public void setSourceAddress(EndpointAddress srcAddress) {
setBytes("jxta:EndpointSourceAddress",
srcAddress.toString ().getBytes ());
}
/**
* @deprecated
**/
public void setDestinationAddress(EndpointAddress dstAddress) {
setBytes("jxta:EndpointDestinationAddress",
dstAddress.toString ().getBytes ());
}
/**
* Set the string on to the message.
* <P>
* Convenience method.
* @deprecated
*/
public void setString(String elementName, String s)
{
setBytes(elementName, s.getBytes());
}
/**
* Get the element from the message as a string.
* @return the string, or null if the element is
* not present.
* <P>
* Convenience method.
* @deprecated
**/
public String getString(String elementName)
{
byte[] bytes = getBytes(elementName);
if (bytes == null)
return null;
return new String(bytes);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -