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

📄 advertisementutilities.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved. *   *  The Sun Project JXTA(TM) Software License *   *  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 Sun Microsystems, Inc. for JXTA(TM) technology."  *     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. *   *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United  *  States and other countries. *   *  Please see the license information page at : *  <http://www.jxta.org/project/www/license.html> for instructions on use of  *  the license in source files. *   *  ==================================================================== *   *  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.util;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.Document;import net.jxta.document.Element;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredTextDocument;import net.jxta.document.TextElement;import net.jxta.exception.JxtaException;import net.jxta.id.ID;import net.jxta.id.IDFactory;import net.jxta.peergroup.PeerGroup;import net.jxta.pipe.PipeID;import net.jxta.platform.ModuleClassID;import net.jxta.platform.ModuleSpecID;import net.jxta.protocol.ModuleClassAdvertisement;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.protocol.ModuleSpecAdvertisement;import net.jxta.protocol.PeerAdvertisement;import net.jxta.protocol.PipeAdvertisement;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.StringReader;import java.io.StringWriter;import java.net.URI;import java.net.URISyntaxException;/** * @deprecated Will be deprecated soon. Do not use these methods. They contain *             a number of incorrect assumption that cannot be corrected while maintaining *             backwards compatibility with programs which already use them. *             THIS CLASS IS SCHEDULED TO BE REMOVED AFTER 2.5 */@Deprecatedpublic final class AdvertisementUtilities {    /**     * Logger     */    // private static final transient Logger LOG = Logger.getLogger(AdvertisementUtilities.class.getName());    public static final StructuredTextDocument STANDARD_COMPATABILITY = (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument(            MimeMediaType.XMLUTF8, "Comp");    public static final String STANDARD_URI = "http://www.jxta.org/download/jxta.jar";    public static final String STANDARD_PROVIDER = "jxta.org";    static {        Element element = STANDARD_COMPATABILITY.createElement("Efmt", "JDK1.4.1");        STANDARD_COMPATABILITY.appendChild(element);        element = STANDARD_COMPATABILITY.createElement("Bind", "V2.0 Ref Impl");        STANDARD_COMPATABILITY.appendChild(element);    }    private AdvertisementUtilities() {}    /**     * Read a JXTA Advertisement from a File     *     * @param fileName The file containing the Advertisement     * @return An polymorphic Advertisement object     * @throws JxtaException if Unable to parse the Advertisement     */    public static Advertisement readAdvertisementFromFile(String fileName) throws JxtaException {        return readAdvertisementFromFile(new File(fileName));    }    /**     * Read a JXTA Advertisement from a File     *     * @param file The file containing the Advertisement     * @return An polymorphic Advertisement object     * @throws JxtaException if Unable to parse the Advertisement     */    public static Advertisement readAdvertisementFromFile(File file) throws JxtaException {        FileInputStream in = null;        try {            in = new FileInputStream(file);            return AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING, in);        } catch (IOException e) {            throw new JxtaException("Advertisement Load Failed: " + file, e);        } catch (Exception e) {            throw new JxtaException("Advertisement Load Failed: " + file, e);        } finally {            if (in != null) {                try {                    in.close();                } catch (IOException ignored) {// ignored                }            }        }    }    /**     * Save a JXTA Advertisement as an XML Document to a File     *     * @param adv      The Advertisement to be saved     * @param fileName The file to store the Advertisement     * @throws JxtaException if Unable to parse the Advertisement     */    public static void saveAdvertisementToFile(Advertisement adv, String fileName) throws JxtaException {        saveAdvertisementToFile(adv, new File(fileName));    }    /**     * Save a JXTA Advertisement as an XML Document to a File     *     * @param adv  The Advertisement to be saved     * @param file The file to store the Advertisement     * @throws JxtaException if Unable to parse the Advertisement     */    public static void saveAdvertisementToFile(Advertisement adv, File file) throws JxtaException {        OutputStream out = null;        try {            out = new FileOutputStream(file);            Document document = adv.getDocument(MimeMediaType.XML_DEFAULTENCODING);            document.sendToStream(out);        } catch (IOException e) {} finally {            if (out != null) {                try {                    out.close();                } catch (IOException ignored) {// ignored                }            }        }    }    /**     * Save a JXTA Advertisement as an XML Document to an array of bytes     *     * @param advertisement The Advertisement to be saved     * @throws JxtaException if Unable to parse the Advertisement     * @deprecated This method should not be used because it produces a result     *             who's encoding is not predictable and may (will) differ from JVM to JVM.     */    @Deprecated    public static byte[] advertisementToBytes(Advertisement advertisement) throws JxtaException {        try {            Document document = advertisement.getDocument(MimeMediaType.XML_DEFAULTENCODING);            ByteArrayOutputStream bo = new ByteArrayOutputStream();            document.sendToStream(bo);            return bo.toByteArray();        } catch (IOException e) {            throw new JxtaException("Error converting a document to bytes", e);        }    }    /**     * Convert an array of bytes containing an XML encoded String to an JXTA Advertisement     *     * @param buf The source of the advertisement     * @return The Advertisement     * @deprecated This method should not be used because it interprets the     *             input using the local default encoding which is not predictable and     *             may (will) differ from JVM to JVM.     */    @Deprecated    public static Advertisement bytesToAdvertisement(byte buf[]) {        try {            InputStream in = new ByteArrayInputStream(buf);            Advertisement advertisement = AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING, in);            return advertisement;        } catch (IOException e) {            return null;        } // This will never be thrown    }    /**     * Save a JXTA Advertisement to a String     *     * @param advertisement The Advertisement to be converted     * @param mimeType      Type of document to be created     */    public static String advertisementToText(Advertisement advertisement, String mimeType) {        try {            StructuredTextDocument doc = (StructuredTextDocument) advertisement.getDocument(new MimeMediaType(mimeType));            StringWriter stringWriter = new StringWriter();            doc.sendToWriter(stringWriter);            return stringWriter.toString();        } catch (IOException e) {            return null;        } // This will never be thrown    }    /**     * Save a JXTA Advertisement to a Plain Text String     *     * @param advertisement The Advertisement to be converted     */    public static String advertisementToPlainText(Advertisement advertisement) {        return advertisementToText(advertisement, MimeMediaType.TEXT_DEFAULTENCODING.toString());    }    /**     * Save a JXTA Advertisement to an XML String     *     * @param advertisement The Advertisement to be converted     * @deprecated Equivalent to Advertisement.toString()     */    @Deprecated    public static String advertisementToXmlText(Advertisement advertisement) {        return advertisementToText(advertisement, MimeMediaType.XMLUTF8.toString());    }    /**     * Convert an array of bytes containing an XML encoded String to an JXTA Advertisement     *     * @param xmlTextAsBytes The source of the advertisement     * @return The Advertisement     * @throws JxtaException if Unable to parse the Advertisement     * @deprecated This method should not be used because it produces a result     *             who's encoding is not predictable and may (will) differ from JVM to JVM.     */    @Deprecated    public static Advertisement newAdvertisementFromXml(byte xmlTextAsBytes[]) throws JxtaException {        try {            return AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING                    ,                    new ByteArrayInputStream(xmlTextAsBytes));        } catch (Exception e) {            throw new JxtaException("Unable to create Advertisement from the provided XML", e);        }    }    /**     * Convert a String containing an XML encoded String to an JXTA Advertisement     *

⌨️ 快捷键说明

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