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

📄 wireformatmessagefactory.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**     * This class is in fact a singleton. This is the instance that backs the     * static methods.     */    private static WireFormatMessageFactory factory = new WireFormatMessageFactory();    /**     * Private constructor. This class is not meant to be instantiated except     * by itself.     */    private WireFormatMessageFactory() {}    /**     *  Registers the pre-defined set of WireFormatMessage sub-classes so that     *  this factory can construct them.     *     *  @return true if at least one of the WireFormatMessage sub-classes could     *  be registered otherwise false.     */    private synchronized boolean loadProviders() {        if (!factory.loadedProperty) {            factory.loadedProperty = registerProviders(WireFormatMessage.class.getName());        }                return factory.loadedProperty;    }        /**     * {@inheritDoc}     */    @Override    protected Map<MimeMediaType,Instantiator> getAssocTable() {        return encodings;    }    /**     * {@inheritDoc}     */    @Override    public Class<Instantiator> getClassOfInstantiators() {        // our key is the doctype names.        return Instantiator.class;    }    /**     * {@inheritDoc}     */    @Override    public Class getClassForKey() {        // our key is the mime types.        return MimeMediaType.class;    }    /**     * {@inheritDoc}     */    @Override    protected boolean registerAssoc(String className) {        boolean registeredSomething = false;        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("Registering : " + className);        }        try {            Class msgClass = Class.forName(className);            Instantiator instantiator = (Instantiator) (msgClass.getField("INSTANTIATOR").get(null));            MimeMediaType[] mimeTypes = instantiator.getSupportedMimeTypes();            for (MimeMediaType mimeType : mimeTypes) {                if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) {                    LOG.finer("   Registering Type : " + mimeType);                }                registeredSomething |= registerInstantiator(mimeType, instantiator);            }        } catch (Exception all) {            if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {                LOG.log(Level.WARNING, "Failed to register \'" + className + "\'", all);            }        }        return registeredSomething;    }    /**     * Register an instantiator object a mime-type of documents to be     * constructed.     *     * @param mimetype     the mime-type associated.     * @param instantiator the instantiator that wants to be registered..     * @return boolean true   if the instantiator for this mime-type is now     *         registered. If there was already an instantiator this mime-type then     *         false will be returned.     * @throws SecurityException there were permission problems registering     *                           the instantiator.     */    public static boolean registerInstantiator(MimeMediaType mimetype, Instantiator instantiator) {        boolean registered = factory.registerAssoc(mimetype, instantiator);        return registered;    }    /**     * Constructs an instance of {@link WireFormatMessage} matching the type     * specified by the <CODE>type</CODE> parameter.     *     * @param msg               the message for which a serialization is desired.     * @param type              the the serialization form desired. This can include     *                          mime parameters to control options.     * @param preferedEncodings An array of acceptable message encodings     *                          in descending order of preference. any or none of these encoding options     *                          may be used. May be null for unencoded messages.     * @return a proxy object for the abstract message which is a     *         representation of the message in its serialized form.     */    public static WireFormatMessage toWire(Message msg, MimeMediaType type, MimeMediaType[] preferedEncodings) {        factory.loadProviders();        Instantiator instantiator = factory.getInstantiator(type.getBaseMimeMediaType());        return instantiator.toWire(msg, type, preferedEncodings);    }    /**     * Constructs an instance of <CODE>Message</CODE> from matching the type     * specified by the <CODE>type</CODE> parameter.     *     * @param is              The message stream. Message serializations must either use     *                        internal data or EOF to determine the length of the stream.     * @param type            Declared message type of the stream including any optional     *                        configuration parameters.     * @param contentEncoding Content encoding (including optional parameters)     *                        which has been applied to the message. May be null for unencoded messages.     * @return the new abstract message.     * @throws java.io.IOException if an io error occurs     */    public static Message fromWire(InputStream is, MimeMediaType type, MimeMediaType contentEncoding) throws IOException {        factory.loadProviders();        Instantiator instantiator;        try {            instantiator = factory.getInstantiator(type.getBaseMimeMediaType());        } catch (NoSuchElementException badType) {            throw new IOException("Unable to deserialize message of type: " + type);        }        return instantiator.fromWire(is, type, contentEncoding);    }    /**     * Constructs an instance of <CODE>Message</CODE> from matching the type     * specified by the <CODE>type</CODE> parameter.     *     * @param buffer          The message buffer. Message serializations must either use     *                        internal data or EOF to determine the length of the stream.     * @param type            Declared message type of the stream including any optional     *                        configuration parameters.     * @param contentEncoding Content encoding (including optional parameters)     *                        which has been applied to the message. May be null for unencoded messages.     * @return the new abstract message.     * @throws java.io.IOException if an io error occurs     */    public static Message fromBuffer(ByteBuffer buffer, MimeMediaType type, MimeMediaType contentEncoding) throws IOException {        factory.loadProviders();        Instantiator instantiator;        try {            instantiator = factory.getInstantiator(type.getBaseMimeMediaType());        } catch (NoSuchElementException badType) {            throw new IOException("Unable to deserialize message of type: " + type);        }        return instantiator.fromBuffer(buffer, type, contentEncoding);    }}

⌨️ 快捷键说明

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