midterm.java

来自「j2me学习 简单例子」· Java 代码 · 共 470 行 · 第 1/2 页

JAVA
470
字号
        portField = null;
    }
    
    public void onShowInput()
    {
        scrollOptions.setSelectedIndex( 0, canvas.isScrolling() );
        display.setCurrent( inputForm );
    }
    
    public void onDoInput()
    {
        canvas.setScrolling( scrollOptions.isSelected( 0 ) );

        StringBuffer buffer = new StringBuffer( inputField.getString() );
    
        if ( inputOptions.isSelected( 0 ) ) // ENTER
        {
            buffer.append( '\n' );
        }
        
        if ( inputOptions.isSelected( 2 ) ) // ESC
        {
            buffer.insert( 0, (char) 27 );
        }
        
        byte[] data = toASCII( buffer.toString() ); 

        if ( inputOptions.isSelected( 1 ) ) // CTRL
        {
            // convert any alpha characters to control characters
            byte b;
            for ( int i = 0; i < data.length; i++ )
            {
                b = data[i];
                if ( b > 96 && b < 123 ) b = (byte) (b - 32);
                if ( b > 63 && b <  96 ) b = (byte) (b - 64);
                data[i] = b;
            }
        }

        if ( data.length > 0 )
        {
            canvas.send( data );
        }
    }   
    
    public void onHideInput()
    {
        display.setCurrent( canvas );
        inputField.setString( "" ); // clear last command if any
        // inputOptions.setSelectedIndex( 0, false ); // ENTER // convenience
        inputOptions.setSelectedIndex( 1, false ); // CTRL
        inputOptions.setSelectedIndex( 2, false ); // ESC
    }
    
    // interface CommandListener
    
    public void commandAction( Command aCommand, Displayable aDisplayable )
    {
        if ( aCommand == openCommand )
        {
            onDoLogin();
        }
        else
        if ( aCommand == closeCommand )
        {
            onShowLogin();
        }
        else
        if ( aCommand == connectCommand )
        {
            connect();
        }
        else
        if ( aCommand == disconnectCommand )
        {
            disconnect();
        }
        else
        if ( aCommand == exitCommand )
        {
            destroyApp( true );
            notifyDestroyed();
        }
        else
        if ( aCommand == inputCommand )
        {
            onShowInput();
        }
        else
        if ( aCommand == okCommand )
        {
            onDoInput();
            onHideInput();
        }
        else
        if ( aCommand == cancelCommand )
        {
            onHideInput();
        }
    }
    
    // interface Runnable
    
    public void run()
    {
        String connectString = "socket://" + host + ':' + port;
        
        try
        {
            canvas.receive( toASCII( "Connecting...\n" ) );
            
            connection = new TelnetConnection( (StreamConnection) 
                Connector.open( connectString, Connector.READ_WRITE, true ),
                canvas.getColumns(), canvas.getRows(), canvas.getTerminalType() );
            input = connection.openInputStream();
            output = connection.openOutputStream();
            canvas.setOutputStream( output );
            
            try
            {        
                if ( user != null )
                {
                    waitUntil( input, new String[] { "ogin:", "serid:" }, true );
                    output.write( toASCII( user + '\n' ) );
                    canvas.receive( toASCII( user + '\n' ) );
                    output.flush();

                    if ( pass != null )
                    {
                        waitUntil( input, new String[] { "assword:" }, true );
                        output.write( toASCII( pass + '\n' ) );
                        canvas.receive( toASCII( "\n" ) ); // let's not echo password
                        output.flush();
                    }
                }

                waitUntil( input, new String[] { "Done" }, true );
            }
            catch ( IOException ioe )
            {
                System.err.println( "Error while communicating: " + ioe.toString() );
                canvas.receive( toASCII( "\nLost connection." ) );
            }
            catch ( Throwable t )
            {
                System.err.println( "Unexpected error while communicating: " + t.toString() );
                canvas.receive( toASCII( "\nUnexpected error: " + t.toString() ) );
            }
        }
        catch ( IllegalArgumentException iae )
        {
            System.err.println( "Invalid host: " + host );
            canvas.receive( toASCII( "Invalid host: " + host ) );
        }        
        catch ( ConnectionNotFoundException cnfe )
        {
            System.err.println( "Connection not found: " + connectString );
            canvas.receive( toASCII( "Connection not found: " + connectString ) );
        }        
        catch ( IOException ioe )
        {
            System.err.println( "Error on connect: " + ioe.toString() );
            canvas.receive( toASCII( "Error on connect: " + ioe.toString() ) );
        }        
        catch ( Throwable t )
        {
            System.err.println( "Unexpected error on connect: " + t.toString() );
            canvas.receive( toASCII( "Unexpected error on connect: " + t.toString() ) );
        }        
        
        disconnect();
        canvas.receive( toASCII( "\nDisconnected.\n" ) );
    }
    
    /**
    * Returns the String that matched, or null if end of input.
    */
    private String waitUntil( 
        InputStream inInput, String[] inMatches, boolean append )
    throws IOException
    {
        int c, i;
        
        // no input or zero matches returns immediately
        if ( inInput == null || inMatches == null || inMatches.length == 0 ) 
        {
            return null;
        }

        // zero-length strings return immediately
        for ( i = 0; i < inMatches.length; i++ )
        {
            if ( inMatches[i].length() == 0 ) return inMatches[i];
        }

        // create match trackers (init to zeroes)
        int[] lastMatch = new int[ inMatches.length ];
        
        // now read input and check for match
        while ( ( c = inInput.read() ) != -1 )
        {
            if ( append ) canvas.receive( (byte) c );
                        
            for ( i = 0; i < inMatches.length; i++ )
            {
                if ( inMatches[i].charAt( lastMatch[i] ) == c )
                {
                    // increment match count
                    if ( ++lastMatch[i] == inMatches[i].length() )
                    {
                        // matched complete string: return
                        return inMatches[i];
                    }
                }
                else // no match
                {
                    // reset match count
                    lastMatch[i] = 0;
                }
            }
        }
        
        // end of input
        return null;
    }
    
    private final static byte[] toASCII( String inString )
    {
        char[] chars = inString.toCharArray();
        byte[] bytes = new byte[ chars.length ];
        for ( int i = 0; i < bytes.length; i++ ) bytes[i] = (byte) chars[i];
        return bytes;
    }
}

⌨️ 快捷键说明

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