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

📄 messageelement.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                if( null != cachedToString ) {            result = (String) cachedToString.get();                        if (null != result)                return result;        }                if (LOG.isEnabledFor(Level.DEBUG)){            LOG.debug( "creating toString of " + getClass().getName() + '@' + Integer.toHexString(hashCode()) );        }                String charset = type.getParameter( "charset" );                StringBuffer theString = new StringBuffer();                Reader asString = null;                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( result );            return result;        } catch ( IOException caught ) {            if (LOG.isEnabledFor(Level.ERROR)) {                LOG.error( "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/>Will return "Application/Octet-Stream" if no type was originally     *   specified.     **/    public MimeMediaType getMimeType() {        return type;    }        /**     * {@inheritDoc}     *     * <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/>This implementation builds the byte array from the stream.     *     * @return byte[] Contents of message element.     * @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.     **/    public synchronized byte[] getBytes( boolean copy ) {        byte [] result = null;                if( null != cachedGetBytes ) {            result = (byte []) 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 (LOG.isEnabledFor(Level.DEBUG)){            LOG.debug( "creating getBytes of " + getClass().getName() + '@' + Integer.toHexString(hashCode())  );        }                long len = getByteLength();                if( len > Integer.MAX_VALUE ) {            if (LOG.isEnabledFor(Level.ERROR))                LOG.error( "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 (LOG.isEnabledFor(Level.ERROR)){                LOG.error( "ailed 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( result );        }                return result;    }        /**     *  {@inheritDoc}     *     * <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/>Element properties are useful for managing the state of element     *  during processing. Element properties are not transmited with the     *  message element when the message element is sent as part of a message.     *     *  <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." );      }    }     */                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 transmited 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 ) {                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 + -