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

📄 stringmessageelement.java

📁 jxta平台的开发包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }        /**     *  {@inheritDoc}     **/    public String toString( ) {        return data;    }        /**     *  {@inheritDoc}     **/    public synchronized byte[] getBytes( boolean copy ) {        byte [] cachedBytes = null;                if( null != cachedGetBytes ) {            cachedBytes = (byte []) cachedGetBytes.get();        }                if( null == cachedBytes ) {            if (LOG.isEnabledFor(Level.DEBUG)){                LOG.debug( "Creating getBytes of " + getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(this))  );            }                        String charset = type.getParameter( "charset" );                        try {                cachedBytes = data.getBytes( charset );            } catch( UnsupportedEncodingException caught ) {                if (LOG.isEnabledFor(Level.WARN)) {                    LOG.warn( "MessageElement Data could not be generated", caught );                }                IllegalStateException failure = new IllegalStateException( "MessageElement Data could not be generated" );                failure.initCause( caught );                throw failure;            }                        cachedGetBytes = new SoftReference( cachedBytes );        }                if( !copy ) {            return cachedBytes;        }                byte [] bytesCopy = (byte[]) cachedBytes.clone();                return bytesCopy;    }        /**     *  {@inheritDoc}     **/    public long getCharLength() {        return data.length();    }        /**     *  {@inheritDoc}     **/    public synchronized char[] getChars( boolean copy ) {        char [] cachedChars = null;                if( null != cachedGetChars ) {            cachedChars = (char []) cachedGetChars.get();        }                if( null == cachedChars ) {            if (LOG.isEnabledFor(Level.DEBUG)){                LOG.debug( "creating cachedGetChars of " + getClass().getName() + '@' + Integer.toHexString(hashCode())  );            }                        cachedChars = new char [ data.length() ];                        data.getChars( 0, data.length(), cachedChars, 0 );                        // if this is supposed to be a shared buffer then we can cache it.                        cachedGetChars = new SoftReference( cachedChars );        }                if( !copy ) {            return cachedChars;        }                char [] copyChars = (char[]) cachedChars.clone();                return copyChars;    }        /**     *  {@inheritDoc}     **/    public InputStream getStream() throws IOException {        byte cachedBytes[] = null;                synchronized( this ) {            if (null != cachedGetBytes)  {                cachedBytes = (byte []) cachedGetBytes.get();            }        }                if( null != cachedBytes ) {            return new ByteArrayInputStream( cachedBytes );        } else {            String charset = type.getParameter( "charset" );                        return new CharSequenceInputStream( data, charset );        }    }        /**     *  {@inheritDoc}     *     *  @return InputStream of the stream containing element data.     *  @throws IOException when there is a problem getting a reader.     **/    public Reader getReader() throws IOException {                return new StringReader( data );    }        /**     *  {@inheritDoc}     **/    public void sendToStream( OutputStream sendTo ) throws IOException {        sendTo.write( getBytes(false) );    }        /**     *  {@inheritDoc}     **/    public void sendToWriter( Writer sendTo ) throws IOException {        sendTo.write( data );    }        /**     *     **/    private static class CharSequenceInputStream extends InputStream {                private final CharBuffer charData;                private final CharsetEncoder conversion;                private boolean marked = false;        private byte mark_multiByteChar[];        private int mark_position;                private byte multiByteChar[];        private int position;                /**         *         **/        public CharSequenceInputStream( CharSequence s, String encoding ) {            charData = CharBuffer.wrap(s);                        Charset encodingCharset = Charset.forName( encoding );                        conversion = encodingCharset.newEncoder();            conversion.onMalformedInput( CodingErrorAction.REPLACE );            conversion.onUnmappableCharacter( CodingErrorAction.REPLACE );                        int maxBytes = new Float( conversion.maxBytesPerChar() ).intValue();                        multiByteChar = new byte[maxBytes];            position = multiByteChar.length;        }                /**         *  {@inheritDoc}         **/        public void mark( int ignored ) {            charData.mark();            mark_multiByteChar = (byte[]) multiByteChar.clone();            mark_position = position;            marked = true;        }                /**         *  {@inheritDoc}         **/        public boolean markSupported( ) {            return true;        }                /**         *  {@inheritDoc}         **/        public void reset() throws IOException {                        if( !marked ) {                throw new IOException( "reset() called before mark()" );            }                        charData.reset();            multiByteChar = (byte[]) mark_multiByteChar.clone();            position = mark_position;        }                /**         *  {@inheritDoc}         **/        public int read() throws IOException{            // prefill the buffer            while( multiByteChar.length == position ) {                int readsome = read( multiByteChar, 0, multiByteChar.length );                                if( -1 == readsome ) {                    return -1;                }                                position = multiByteChar.length - readsome;                                if( (0 != position) && (0 != readsome) ) {                    System.arraycopy( multiByteChar, 0, multiByteChar, position, readsome );                }            }                        return( multiByteChar[position++] & 0xFF );        }                /**         *  {@inheritDoc}         **/        public int read( byte[] buffer ) throws IOException {            return read( buffer, 0, buffer.length );        }                /**         *  {@inheritDoc}         **/        public int read( byte[] buffer, int offset, int length ) throws IOException {            // handle partial characters;            if( multiByteChar.length != position ) {                int copying = Math.min( length, multiByteChar.length - position );                System.arraycopy( multiByteChar, position, buffer, offset, copying );                position += copying;                return copying;            }                        ByteBuffer bb = ByteBuffer.wrap( buffer, offset, length );                        int before = bb.remaining();                        CoderResult result = conversion.encode( charData, bb, true );                        int readin = before - bb.remaining();                        if( CoderResult.UNDERFLOW == result ) {                if( 0 == readin ) {                    return -1;                } else {                    return readin;                }            }                        if( CoderResult.OVERFLOW == result ) {                return readin;            }                        result.throwException();                        // NEVERREACHED            return 0;        }    }}

⌨️ 快捷键说明

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