📄 wireformatmessagebinary.java
字号:
/************************************************************************ * * $Id: WireFormatMessageBinary.java,v 1.27 2006/06/21 19:39:18 bondolo Exp $ * * Copyright (c) 2001-2006 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.endpoint;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.io.SequenceInputStream;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.io.IOException;import java.io.EOFException;import org.apache.log4j.Logger;import org.apache.log4j.Level;import net.jxta.document.Document;import net.jxta.document.MimeMediaType;import net.jxta.endpoint.ByteArrayMessageElement;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.WireFormatMessage;import net.jxta.endpoint.WireFormatMessageFactory;import net.jxta.util.LimitInputStream;/** * A Wire Format Message which encodes the message into MIME Type * "application/x-jxta-msg". * * <p/>This implementation does nothing with encodings. * * <p/>This implementation does not use any MIME parameters attached to the * requesting mime type. * * @see net.jxta.endpoint.WireFormatMessageFactory * @see <a href="http://spec.jxta.org/nonav/v1.0/docbook/JXTAProtocols.html#msgs-fmts-jbm" target="_blank">JXTA Protocols Specification : Binary Message Format</a> **/public class WireFormatMessageBinary implements WireFormatMessage { /* * Log4J Logger */ private final static transient Logger LOG = Logger.getLogger( WireFormatMessageBinary.class.getName() ); // Flag bits protected static final byte HAS_TYPE = 0x01; protected static final byte HAS_ENCODING = 0x02; protected static final byte HAS_SIGNATURE = 0x04; protected static final int MESSAGE_VERSION = 0; /** * Our Mime Media Type(s) **/ private static final MimeMediaType [] myTypes = { MimeMediaType.valueOf( "application/x-jxta-msg" ) }; /** * These are the content encodings we support. **/ private static final MimeMediaType [] myContentEncodings = { // we support raw binary! null }; /** * Our instantiator for the factory. **/ public static final WireFormatMessageFactory.Instantiator INSTANTIATOR = new Instantiator(); /** * Our instantiator. **/ static class Instantiator implements WireFormatMessageFactory.Instantiator { /** * Creates new WireFormatMessageBinary Instantiator */ public Instantiator() { } /** * {@inheritDoc} **/ public MimeMediaType[] getSupportedMimeTypes() { return myTypes; } /** * {@inheritDoc} **/ public MimeMediaType[] getSupportedContentEncodings() { return myContentEncodings; } /** * {@inheritDoc} **/ public Message fromWire( InputStream is, MimeMediaType type, MimeMediaType contentEncoding ) throws IOException { // FIXME 20020504 bondolo@jxta.org Ignores type and contentEncoding completely. Message msg = new Message(); if ( LOG.isEnabledFor(Level.DEBUG) ) { LOG.debug( "Reading " + msg + " from " + is ); } DataInputStream dis = new DataInputStream( is ); HashMap idToNamespace = readHeader( dis ); int elementCnt = dis.readShort(); if ( LOG.isEnabledFor(Level.DEBUG) ) { LOG.debug( "Message element count " + elementCnt + " from " + is ); } int eachElement = 0; do { if ( LOG.isEnabledFor(Level.DEBUG) ) { LOG.debug( "Read element " + eachElement + " of " + elementCnt + " from " + is + " for " + msg ); } Object [] anElement = null; try { anElement = readMessageElement( dis, is ); } catch ( IOException failed ) { if ( LOG.isEnabledFor(Level.ERROR) ) { LOG.error( "Failure reading element " + eachElement + " of " + elementCnt + " from " + is + " for " + msg, failed ); } throw failed; } if( null == anElement ) { break; } String namespace = (String) idToNamespace.get(anElement[0]); if( null == namespace ) { if ( LOG.isEnabledFor(Level.ERROR) ) { LOG.error( "Element identified a namespace which was not defined for this message." ); } throw new IOException( "Element identified a namespace which was not defined for this message." ); } msg.addMessageElement( namespace, (MessageElement) anElement[1] ); eachElement++; if ( LOG.isEnabledFor(Level.DEBUG) ) { LOG.debug( "Add element (name='" + ((MessageElement) anElement[1]).getElementName() + "') #" + eachElement + " of #" + elementCnt + " elements from " + dis.toString() ); } } while ( true && ((0 == elementCnt) || (eachElement < elementCnt)) ); if( (elementCnt != 0) && (eachElement != elementCnt) ) { throw new IOException( "Found wrong number of elements in message." ); } return msg; } /** * {@inheritDoc} **/ public WireFormatMessage toWire(Message msg, MimeMediaType type, MimeMediaType[] preferedContentEncoding) { try { return new WireFormatMessageBinary( msg, type, preferedContentEncoding ); } catch ( IOException caught ) { throw new IllegalStateException( "Could not build wire format for message due to " + caught.getMessage() ); } } /** * Read in a message header from the provided data stream. * * @param dis the data stream to read from * @return hashmap containing the namespace id to namespace values * @throws IOException if EOF or other IOException is encountered * during the reading of the header. **/ private static HashMap readHeader( DataInputStream dis ) throws IOException { // Read message signature char [] msgsig = new char[4]; try { msgsig[0] = (char) dis.readByte(); } catch( EOFException failed ) { if ( LOG.isEnabledFor(Level.DEBUG) ) { LOG.debug( "EOF reading message at first byte of header.", failed ); } throw failed; } msgsig[1] = (char) dis.readByte(); msgsig[2] = (char) dis.readByte(); msgsig[3] = (char) dis.readByte(); if ( msgsig[0] != 'j' || msgsig[1] != 'x' || msgsig[2] != 'm' || msgsig[3] != 'g') { IOException failure = new IOException( "Not a message (incorrect signature '" + msgsig[0] + msgsig[1] + msgsig[2] + msgsig[3] + "') " ); if ( LOG.isEnabledFor(Level.ERROR) ) { LOG.error( failure ); } throw failure; } // Message version if (dis.readByte() != MESSAGE_VERSION ) { IOException failure = new IOException( "Message not version " + MESSAGE_VERSION ); if ( LOG.isEnabledFor(Level.ERROR) ) { LOG.error( failure, failure ); } throw failure; } int namespaceCnt = dis.readShort(); if( namespaceCnt > 253 ) { IOException failure = new IOException( "Message contains too many namespaces (>253)" ); if ( LOG.isEnabledFor(Level.ERROR) ) { LOG.error( failure, failure ); } throw failure; } HashMap id2namespace = new HashMap( 2 + namespaceCnt ); id2namespace.put(new Integer(0), "");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -