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

📄 wireformatmessagebinary.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            streamParts.add(new ByteArrayInputStream(header));            for (binaryElementProxy anElement : elements) {                streamParts.add(anElement.getStream());            }            InputStream theStream = new SequenceInputStream(Collections.enumeration(streamParts));            if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) {                LOG.finer(                        MessageFormat.format("Returning {0}@{1} for {2}", theStream.getClass().getName()                        ,                        System.identityHashCode(theStream), message));            }            return theStream;        }        /**         * {@inheritDoc}         */        public void sendToStream(OutputStream sendTo) throws IOException {            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.fine("Sending " + message + " to " + sendTo.getClass().getName() + "@" + System.identityHashCode(sendTo));            }            sendTo.write(header);            Iterator eachElement = elements.listIterator();            while (eachElement.hasNext()) {                binaryElementProxy anElement = (binaryElementProxy) eachElement.next();                anElement.sendToStream(sendTo);            }        }        /**         * {@inheritDoc}         */        public long getByteLength() {            long size = 0;            size += header.length;            for (binaryElementProxy element : elements) {                binaryElementProxy anElement = element;                size += anElement.getByteLength();            }            return size;        }        /**         *  {@inheritDoc}         */        public MimeMediaType getContentEncoding() {            return null;        }        /**         * Scans the source message to build a HashMap of the namespaces used         * in the message and assign and id to each namespace.         */        private void assignNamespaceIds() {            int id = 0;            Iterator namespaces = message.getMessageNamespaces();            // insert the predefined namespaces.            namespaceIDs.put("", id++);            this.namespaces.add("");            namespaceIDs.put("jxta", id++);            this.namespaces.add("jxta");            // insert items in the vector if they are not found in the map            while (namespaces.hasNext()) {                String namespace = (String) namespaces.next();                if (namespaceIDs.get(namespace) == null) {                    namespaceIDs.put(namespace, id++);                    this.namespaces.add(namespace);                }            }            if (id >= 256) {                throw new IllegalStateException("WireFormatMessageBinary does not support more than 255 namespaces");            }        }        /**         * Builds the wire format header for the message.         *         * @throws IOException if for some reason the header cannot be built.         */        private void buildHeader() throws IOException {            ByteArrayOutputStream headerBytes = new ByteArrayOutputStream(256);            DataOutputStream header = new DataOutputStream(headerBytes);            header.writeBytes("jxmg");            header.writeByte(MESSAGE_VERSION);            header.writeShort(namespaces.size() - 2);            for (int eachNamespace = 2; eachNamespace < namespaces.size(); eachNamespace++) {                byte[] elementName = namespaces.get(eachNamespace).getBytes("UTF8");                header.writeShort(elementName.length);                header.write(elementName, 0, elementName.length);            }            header.writeShort(elements.size());            header.flush();            header.close();            headerBytes.flush();            headerBytes.close();            this.header = headerBytes.toByteArray();        }    }    /**     * Proxy for a message element. Handles the serialization of the element     * meta information.     */    static class binaryElementProxy {        final byte namespaceid;        binaryElementProxy sig;        MessageElement element;        byte[] header;        binaryElementProxy(byte namespaceid, MessageElement element) throws IOException {            this.namespaceid = namespaceid;            this.element = element;            MessageElement sig = element.getSignature();            if (null != sig) {                this.sig = new binaryElementProxy(namespaceid, sig);            }            buildHeader();        }        void buildHeader() throws IOException {            byte[] elementName = element.getElementName().getBytes("UTF8");            byte[] elementType = null;            if (!MimeMediaType.AOS.equals(element.getMimeType())) {                elementType = element.getMimeType().toString().getBytes("UTF8");            }            // FIXME  20020504 bondolo@jxta.org Do something with encodings.            ByteArrayOutputStream headerBytes = new ByteArrayOutputStream(256);            DataOutputStream header = new DataOutputStream(headerBytes);            header.writeBytes("jxel");            header.writeByte(namespaceid);            header.writeByte(((null != elementType) ? HAS_TYPE : 0) | ((null != sig) ? HAS_SIGNATURE : 0));            header.writeShort(elementName.length);            header.write(elementName, 0, elementName.length);            if (null != elementType) {                header.writeShort(elementType.length);                header.write(elementType, 0, elementType.length);            }            // FIXME content encoding should go here            long dataLen = element.getByteLength();            if (dataLen > Integer.MAX_VALUE) {                throw new IllegalStateException("WireFormatMessageBinary does not support elements longer than 4GB");            }            header.writeInt((int) dataLen);            header.flush();            header.close();            headerBytes.flush();            headerBytes.close();            this.header = headerBytes.toByteArray();        }        public long getByteLength() {            long size = 0;            size += header.length;            size += element.getByteLength();            if (null != sig) {                size += sig.getByteLength();            }            return size;        }        public ByteBuffer[] getByteBuffers() {            List<ByteBuffer> partBuffers = new ArrayList<ByteBuffer>();            partBuffers.add(ByteBuffer.wrap(header));                        partBuffers.add(ByteBuffer.wrap(element.getBytes(false)));            if (null != sig) {                partBuffers.addAll(Arrays.asList(sig.getByteBuffers()));            }            return partBuffers.toArray(new ByteBuffer[partBuffers.size()]);                   }                public InputStream getStream() throws IOException {            List<InputStream> streamParts = new ArrayList<InputStream>();            streamParts.add(new ByteArrayInputStream(header));            streamParts.add(element.getStream());            if (null != sig) {                streamParts.add(sig.getStream());            }            return new SequenceInputStream(Collections.enumeration(streamParts));        }        public void sendToStream(OutputStream sendTo) throws IOException {            sendTo.write(header);            element.sendToStream(sendTo);            if (null != sig) {                sig.sendToStream(sendTo);            }        }    }    /**     * The message we are serializing.     */    private final Message msg;    /**     * The mod count of the message when we started. Used for detecting     * (illegal) modifications.     */    private final int msgModCount;    /**     * The mime type of the encoded message.     */    private final MimeMediaType type;    /**     * The mime type of the content encoding for this message.     */    private final MimeMediaType contentEncoding;    /**     * The serialization peer to the Message.     */    private final binaryMessageProxy msgProxy;    /**     * Creates a new instance of WireFormatMessageBinary. Called only by the     * Instantiator.     *     * @param msg                      the message being serialized     * @param type                     the mime mediatype being requested.     * @param preferedContentEncodings The ranked content encodings preferred by     *                                 the recipient.     * @throws java.io.IOException if an io error occurs     */    WireFormatMessageBinary(Message msg, MimeMediaType type, MimeMediaType[] preferedContentEncodings) throws IOException {        if (null == msg) {            throw new IllegalArgumentException("Null message!");        }        this.msg = msg;        this.msgModCount = msg.getMessageModCount();        if (null == type) {            throw new IllegalArgumentException("Null mime type!");        }        int matchedIdx = -1;        for (int eachType = 0; eachType < myTypes.length; eachType++) {            if (type.equalsIngoringParams(myTypes[eachType])) {                matchedIdx = eachType;                break;            }        }        if (-1 == matchedIdx) {            throw new IllegalArgumentException("Unsupported mime type!");        }        // FIXME  20020504 bondolo@jxta.org Check the mimetype params to make        // sure we can support them.        this.type = type;        // FIXME  20020504 bondolo@jxta.org Do something with encodings.        this.contentEncoding = myContentEncodings[0];        msgProxy = new binaryMessageProxy(msg, type);    }    /**     * {@inheritDoc}     */    @Override    public boolean equals(Object obj) {        throw new UnsupportedOperationException("don't do this");    }    /*     * The cost of just having a finalize routine is high. The finalizer is     * a bottleneck and can delay garbage collection all the way to heap     * exhaustion. Leave this comment as a reminder to future maintainers.     * Below is the reason why finalize is not needed here.     *     * No critical non-memory resource held.     protected void finalize( ) {     }     */    /**     * {@inheritDoc}     */    @Override    public int hashCode() {        throw new UnsupportedOperationException("don't do this");    }    /**     * {@inheritDoc}     */    public String getFileExtension() {        return "???";    }    /**     * {@inheritDoc}     */    public MimeMediaType getMimeType() {        return type;    }    /**     * {@inheritDoc}     */    public InputStream getStream() throws IOException {        if (msg.getMessageModCount() != msgModCount) {            throw new IllegalStateException("message was unexpectedly modified!");        }        msg.modifiable = false;        try {            InputStream result = msgProxy.getStream();            return result;        } finally {            msg.modifiable = true;        }    }    /**     * {@inheritDoc}     */    public ByteBuffer[] getByteBuffers() {        if (msg.getMessageModCount() != msgModCount) {            throw new IllegalStateException("message was unexpectedly modified!");        }        msg.modifiable = false;        try {            ByteBuffer[] result = msgProxy.getByteBuffers();            return result;        } finally {            msg.modifiable = true;        }    }    /**     * {@inheritDoc}     */    public void sendToStream(OutputStream sendTo) throws IOException {        if (msg.getMessageModCount() != msgModCount) {            throw new IllegalStateException("message was unexpectedly modified!");        }        msg.modifiable = false;        try {            msgProxy.sendToStream(sendTo);        } finally {            msg.modifiable = true;        }    }    /**     * {@inheritDoc}     */    public long getByteLength() {        if (msg.getMessageModCount() != msgModCount) {            throw new IllegalStateException("message was unexpectedly modified!");        }        return msgProxy.getByteLength();    }    /**     * {@inheritDoc}     */    public MimeMediaType getContentEncoding() {        return contentEncoding;    }}

⌨️ 快捷键说明

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