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

📄 httpconnectionwrapper.java

📁 优秀方便使用的J2ME HTTP包装类, LOG可以输出到RMS, 进行真机调试.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        catch( IOException e ){
            rethrow( "openOutputStream", e );
        }
        return _streamOut;
    }
    
    public DataOutputStream openDataOutputStream() throws IOException {
        return new DataOutputStream( openOutputStream() );
    }
    
    //------------------------------------------------------------------------
    // Methods that can be invoked as long as the state is not Closed    

    public void close() throws IOException {
        if( _state == CLOSED_STATE ){
            throw makeException( "close", MSG_ALREADY_CLOSED );
        }
        
        info( "Entering closed state" );
        
        if( _streamIn != null && !_streamIn.isClosed() ){
            warning( "The input stream is still open, be sure to close it" );
        }
        
        if( _streamOut != null && !_streamOut.isClosed() ){
            warning( "The output stream is still open, be sure to close it" );
        }
        
        _state = CLOSED_STATE;
        
        try {
            _original.close();
        }
        catch( IOException e ){
            rethrow( "close", e );
        }
    }

    public String getRequestMethod() {
        notIfClosedNoThrow( "getRequestMethod" );
        String method = _original.getRequestMethod();
        info( "getRequestMethod() = " + method );
        return method;
    }

    public String getRequestProperty(String str) {
        notIfClosedNoThrow( "getRequestProperty" );
        String value = _original.getRequestProperty( str );
        info( "getRequestProperty( " + str + " ) = " + value );
        return value;
    }

    public String getURL() {
        notIfClosedNoThrow( "getURL" );
        String url = _original.getURL();
        info( "getURL() = " + url );
        return url;
    }

    public String getProtocol() {
        notIfClosedNoThrow( "getProtocol" );
        String protocol = _original.getProtocol();
        info( "getProtocol() = " + protocol );
        return protocol;
    }

    public String getHost() {
        notIfClosedNoThrow( "getHost" );
        String host = _original.getHost();
        info( "getHost() = " + host );
        return host;
    }

    public String getFile() {
        notIfClosedNoThrow( "getFile" );
        String file = _original.getFile();
        info( "getFile() = " + file );
        return file;
    }

    public String getRef() {
        notIfClosedNoThrow( "getRef" );
        String ref = _original.getRef();
        info( "getRef() = " + ref );
        return ref;
    }

    public int getPort() {
        notIfClosedNoThrow( "getPort" );
        int port = _original.getPort();
        info( "getPort() = " + port );
        return port;
    }

    public String getQuery() {
        notIfClosedNoThrow( "getQuery" );
        String query = _original.getQuery();
        info( "getQuery() = " + query );
        return query;
    }

    //----------------------------------------------------------------------
    // State management methods

    /**
     * Creates an IOException based on a method name and the given message.
     * 
     * @param method the method that is the root of the exception.
     * @param msg    the exception message.
     *
     * @return IOException the new exception.
     */
    
    private IOException makeException( String method, String msg ) {
        StringBuffer b = new StringBuffer();
        b.append( _idString );
        b.append( method );
        b.append( ": " );
        b.append( msg );
        
        String m = b.toString();
        
        logger.warning( m );
        return new IOException( m );
    }

    /** 
     * Rethrows an exception after logging it.
     *
     * @param method the method invoked when the exception occurred.
     * @param e      the exception.
     *
     * @throws IOException the original exception.
     */
    
    private void rethrow( String method, IOException e ) throws IOException {
        StringBuffer b = new StringBuffer();
        b.append( _idString );
        b.append( method );
        b.append( " throws exception " );
        b.append( e.toString() );
        
        logger.warning( b.toString() );
        throw e;
    }
    
    /**
     * Ensures that a method is invoked in the setup state only.
     *
     * @param method the method invoked.
     *
     * @throws IOException if not in setup state.
     */
    
    private void onlyInSetup( String method ) throws IOException {
        fine( "Invoking " + method );
        if( _state != SETUP_STATE ){
            throw makeException( method, MSG_NOT_SETUP_STATE );
        }
    }
    
    /**
     * Ensures that a method is not invoked in closed state.
     *
     * @param method the method invoked.
     * 
     * @throws IOException if in closed state.
     */
    
    private void notIfClosed( String method ) throws IOException {
        fine( "Invoking " + method );
        if( _state == CLOSED_STATE ){
            throw makeException( method, MSG_ALREADY_CLOSED );
        }
    }
    
    /**
     * Ensures that a method is not invoked in closed state, but
     * swallows the resulting exception if any.
     *
     * @param method the method invoked.
     */
    
    private void notIfClosedNoThrow( String method ) {
        try {
            notIfClosed( method );
        }
        catch( IOException e ){
            // do nothing with it
        }
    }
    
    /** 
     * Transitions from setup to connected state.
     *
     * @param method the method invoked.
     *
     * @throws IOException if in closed state.
     */
    
    private void transitionToConnected( String method ) throws IOException {
        fine( "Invoking " + method );
        if( _state == SETUP_STATE ){
            info( "Entering connected state" );
            _state = CONNECTED_STATE;
        }
        
        if( _state != CONNECTED_STATE ){
            throw makeException( method, MSG_ALREADY_CLOSED );
        }
    }
    
    /** 
     * Transitions from setup to connected state, but swallows
     * the resulting exception if any.
     *
     * @param method the method invoked.
     */
    
    private void transitionToConnectedNoThrow( String method ) {
        try {
            transitionToConnected( method );
        }
        catch( IOException e ){
            // do nothing
        }
    }

    /**
     * Logs the message at the INFO level.
     *
     * @param msg the message to log.
     */
    
    private void info( String msg ) {
        logger.info( _idString + msg );
    }
    
    /**
     * Logs the message at the FINE level.
     *
     * @param msg the message to log.
     */
    
    private void fine( String msg ) {
        logger.fine( _idString + msg );
    }
    
    /**
     * Logs the message at the WARNING level.
     *
     * @param msg the message to log.
     */
    
    private void warning( String msg ) {
        logger.warning( _idString + msg );
    }
    
    //--------------------------------------------------------------------------------------------
    // A simple wrapper for tracking when an input stream is closed.
    
    private class InputStreamWrapper extends InputStream {
        private boolean     _isClosed;
        private InputStream _original;
        
        public InputStreamWrapper( InputStream original ) {
            _original = original;
        }
        
        public void close() throws IOException {
            info( "Closing input stream" );
            
            _isClosed = true;
            _original.close();
        }
        
        public boolean isClosed() {
            return _isClosed;
        }
        
        public int read() throws IOException {
            return _original.read();
        }
    }
    
    //--------------------------------------------------------------------------------------------
    // A simple wrapper for tracking when an output stream is closed or flushed.
    
    private class OutputStreamWrapper extends OutputStream {   
        private boolean      _isClosed;
        private OutputStream _original;
        
        /** Creates a new instance of OutputStreamWrapper */
        public OutputStreamWrapper( OutputStream original ) {
            _original = original;
        }
        
        public void close() throws IOException {
            info( "Closing output stream" );
            
            if( _state == SETUP_STATE ){
                transitionToConnected( "OutputStream.close" );
            }
           
            _isClosed = true;
            _original.close();
        }
        
        public void flush() throws IOException {
            info( "Flushing output stream" );
            
            if( _state == SETUP_STATE ){
                transitionToConnected( "OutputStream.flush" );
            }
            
            _original.flush();
        }
        
        public boolean isClosed() {
            return _isClosed;
        }
        
        public void write(int param) throws IOException {
            _original.write( param );
        }
    }
}

⌨️ 快捷键说明

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