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

📄 messageelement.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    @Override    public synchronized String toString() {        String result;        if (null != cachedToString) {            result = cachedToString.get();            if (null != result) {                return result;            }        }        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("creating toString of " + getClass().getName() + '@' + Integer.toHexString(hashCode()));        }        String charset = type.getParameter("charset");        StringBuilder theString = new StringBuilder();        Reader asString;        try {            if (null == charset) {                asString = new InputStreamReader(getStream());            } else {                try {                    asString = new InputStreamReader(getStream(), charset);                } catch (UnsupportedEncodingException caught) {                    throw new IllegalStateException("Unsupported charset : " + charset);                }            }            char[] characters = new char[256];            do {                int res = asString.read(characters);                if (res < 0) {                    break;                }                theString.append(characters, 0, res);            } while (true);            result = theString.toString();            cachedToString = new SoftReference<String>(result);            return result;        } catch (IOException caught) {            if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {                LOG.log(Level.SEVERE, "Could not generate string for element. ", caught);            }            throw new IllegalStateException("Could not generate string for element. " + caught);        }    }    /**     * Returns the name of the MessageElement. Unnamed elements will return     * the empty string ("");     *     * @return String containing the name of the MessageElement.     */    public String getElementName() {        return name;    }    /**     * {@inheritDoc}     * <p/>     * <p/>Will return "Application/Octet-Stream" if no type was originally     * specified.     */    public MimeMediaType getMimeType() {        return type;    }    /**     * {@inheritDoc}     * <p/>     * <p/>We use the "unknown" extension and leave it to sub-classes to     * extend this. If we had a mailcap facility we could do better     * classification based on mimetype.     */    public String getFileExtension() {        return "???";    }    /**     * Returns the size of the element data in bytes.     *     * @return long containing the size of the element data.     */    public synchronized long getByteLength() {        if (cachedGetByteLength >= 0) {            return cachedGetByteLength;        }        CountingOutputStream countBytes = new CountingOutputStream(new DevNullOutputStream());        try {            sendToStream(countBytes);            cachedGetByteLength = countBytes.getBytesWritten();            return cachedGetByteLength;        } catch (IOException caught) {            throw new IllegalStateException("Could not get length of element : " + caught.toString());        }    }    /**     * Returns a byte array which contains the element data. The byte array     * returned <b>may be shared amongst all copies of the element</b>,     * do not modify it. The <code>copy</code> parameter allows you to request a     * private, modifiable copy of the element data.     * <p/>     * <p/>This implementation builds the byte array from the stream.     *     * @param copy If true then the result can be modified without damaging the state of this     *             MessageElement. If false, then the result may be a shared copy of the data and     *             should be considered read-only.     * @return byte[] Contents of message element.     */    public synchronized byte[] getBytes(boolean copy) {        byte[] result;        if (null != cachedGetBytes) {            result = cachedGetBytes.get();            if (null != result) {                if (copy) {                    byte[] theCopy = new byte[result.length];                    System.arraycopy(theCopy, 0, result, 0, result.length);                } else {                    return result;                }            }        }        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("creating getBytes of " + getClass().getName() + '@' + Integer.toHexString(hashCode()));        }        long len = getByteLength();        if (len > Integer.MAX_VALUE) {            if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {                LOG.severe("MessageElement is too large to be stored in a byte array.");            }            throw new IllegalStateException("MessageElement is too large to be stored in a byte array.");        }        result = new byte[(int) len];        try {            DataInput di = new DataInputStream(getStream());            di.readFully(result);        } catch (IOException caught) {            if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {                LOG.log(Level.SEVERE, "Failed to get bytes of Message Element. ", caught);            }            throw new IllegalStateException("Failed to get bytes of Message Element. " + caught);        }        // if this is supposed to be a shared buffer then we can cache it.        if (!copy) {            cachedGetBytes = new SoftReference<byte[]>(result);        }        return result;    }    /**     * {@inheritDoc}     * <p/>     * <p/>This version probably has sub-optimal performance. Sub-classes     * should override this implementation.     */    public void sendToStream(OutputStream sendTo) throws IOException {        copyInputStreamToOutputStream(getStream(), sendTo);    }    /**     * Returns the element containing the digest/digital signature for     * this element     *     * @return Element containing the digital signature.     */    public MessageElement getSignature() {        return sig;    }    /**     * Associate a transient property with this element. If there was a previous     * value for the key provided then it is returned.     * <p/>     * <p/>Element properties are useful for managing the state of element     * during processing. Element properties are not transmitted with the     * message element when the message element is sent as part of a message.     * <p/>     * <p/>The setting of particular keys may be controlled by a Java Security     * Manager. Keys of type 'java.lang.Class' are checked against the caller of     * this method. Only callers which are instances of the key class may modify     * the property. This check is not possible through reflection. All other     * types of keys are unchecked.     *     * @param key   the property key     * @param value the value for the property     * @return previous value for the property or null if no previous     */    public synchronized Object setElementProperty(Object key, Object value) {        /*         if( key instanceof java.lang.Class ) {         Class keyClass = (Class) key;         SecurityManager secure =  new SecurityManager() {         public boolean checkCallerOfClass( Class toCheck ) {         Class [] context = getClassContext();         return toCheck.isAssignableFrom( context[2] );         }         };         if( !secure.checkCallerOfClass( keyClass ) ) {         throw new SecurityException( "You can't set that key from this context." );         }         }         */        if (null == properties) {            properties = new HashMap<Object,Object>();        }                return properties.put(key, value);    }    /**     * Retrieves a transient property from the set for this element.     *      * <p/>Element properties are useful for managing the state of element     * during processing. Element properties are not transmitted with the     * message element when the message element is sent as part of a message.     *     * @param key the property key.     * @return value for the property or null if there is no property for this     *         key.     */    public Object getElementProperty(Object key) {        if (null == properties) {            return null;        }                return properties.get(key);    }    /**     * Copies an input stream to an output stream with buffering.     *     * @param source The stream to copy from.     * @param sink   The stream to send the data to.     * @throws IOException if there is a problem copying the data     */    protected static void copyInputStreamToOutputStream(InputStream source, OutputStream sink) throws IOException {        int c;        byte[] buf = new byte[4096];        do {            c = source.read(buf);            if (-1 == c) {                break;            }            sink.write(buf, 0, c);        } while (true);    }}

⌨️ 快捷键说明

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