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

📄 userlist_screen.java

📁 J2ME下开发的P2P搜索程序,功能较基础,较简单,可供学习和参考使用.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  				     catch(Exception e)
  				    {
  					
  				    }
  				}
  				catch(Exception e)
  				{
  					
  				}
  		   
  		   
  		   }
  
// If browse user is pressed...
  		  if(browse_user.pressed(x,y))
             {
	// Two boolean flags are initialized.. 
  			   boolean connection_flag = false;
  			   boolean ip_flag  = false;
  			   
  			   folder_vector = new Vector();
  			   text_for_display = "";
  			   folder_data = "";
  			   file_data = "";
  			   counter = 1;
	// Option entered by the user is taken 
  			   String user_number = option.getText();
  
  			   try
  			   {
	// Converted to integer..  				 	
  				 int userID = Integer.parseInt(user_number); 
  				try
  				{
	// The ip_address of the user selected is taken out 
	// from the vector.
  					address = (String)ip_address.elementAt(userID-1);
  				 try
  				  {
 // A Socket connection is made on a port with the Listener 
  					socket =
  		  		        (StreamConnection)Connector.open("socket://"+address+":7070",
  						                Connector.READ_WRITE,true);	
  
  
// If the socket is null then the connection is not established..
  					if (socket != null) 
  						{
  					System.out.println("Connection is established to localhost on port 7070...");
  						}
  							
 	//  Opening the Input and Output Streams to the Socket.
  				
  					socket_inputstream = socket.openInputStream();
  					socket_outputstream = socket.openOutputStream();
  				   }
  				   catch(IOException ae)
  				   {
  				   System.out.println("Couldn't open socket:");
  				   }
	// An XMLReuest which is to be sent ot the Listener is formed..
 				String xmlRequest = "<?xml version='1.0' encoding='utf-8'?><p2p_lng>	<request type=\"SHOWFILES\">	</request></p2p_lng>";
  							
	// It is converted to byte array
  				byte [] byteXmlRequest = xmlRequest.getBytes();

	// The length of the byte array is taken out.

  				int byteXmlRequest_length = byteXmlRequest.length;
  
/**
 * 	A function appropriate length is called which will make
 *	this request equal to 1024 bytes in length this is done in
 *	order to make compatibilty with the C# listeners.
 */ 

  				byteXmlRequest = appropriatelength(byteXmlRequest,byteXmlRequest_length);
  							
	// The xmlrequest is sent via socket connection to the Listener
	// machine..
  				socket_outputstream.write(byteXmlRequest);
	// The stream of response by the server is then passed on to 
	// the xmlparser for parsing..
    			   xmlparser = new XmlParser(new InputStreamReader(socket_inputstream));
  
	// Function used for parsing is called...
  				 parseData();
  
	//	And the ip_flag is made true..
  				  ip_flag = true;
  				}
  				catch (Exception e)
  				{
  						System.out.println(e);
	// If any exception occurs then the ip_flag is made false..
  					    ip_flag = false;
  					    option.setText("");
  				}	
  					
  		
  				 connection_flag = true;
  			   }
  			   catch (Exception e)
  			   {
  				   connection_flag = false;
  				   option.setText("");
  			   }
  			   
	// If ip_flag as well as as the connection_flag are true then 
  			   if (connection_flag && ip_flag)
  			   {
	// Remove the focus from the TextField and kill the TextField 
	// blinking caret
  				  option.loseFocus();
  				  option.killCaret();
	// To show a new class fist unregister all the controls of the 
	// existing class.
  				  unregister();
	/// Class showfiles is called with the parameters..
  				  new showfiles(text_for_display,folder_vector, address, "ROOT",false);	
  			   }
  
  		   }
  		
  		  
/**
 *  If exit button is pressed then the game is closed and the 
 *	control is returned to the OS.
 */ 
  		  if(exit.pressed(x,y))
             {
               System.exit(0);
             }
  			
 // Event handling for the ScrollTextBox.
  		  if (userlist.contains(x,y))
               userlist.handlePenMove(x,y);
          		 
         }
  
 // Event handling for the ScrollTextBox.
  	public void penMove(int x, int y)
      {
           if (userlist.contains(x,y))
           userlist.handlePenMove(x,y);
      }
  
// Function parseData (it will hold the callbacks generated by the 
// XMLParsing.
  		void parseData()
  		{
	// Move in loop till the XML document ends..
  		  do
  		   {
  		    try
  		     {	
  				event = xmlparser.read ();
  				if(event.getType()==Xml.START_TAG)
  				{
		// The Start Tag is identified
  					StartTag stag = (StartTag)event;
  					String name = stag.getName();
  				
	/**
	 * If the Tag encountered is fileinfo then the Attributes of the   
	 * tag are taken using the function getValue(). and are added to 
	 * there appropriate position.	
	 */ 
  					if (name.equals("fileinfo"))
  					{
  						
  	String filename = stag.getValue("filename");
  	 
    // A check is made for the filename and folders and 
	// They are stored in seperate String varaiables.

  			if (!(filename.charAt(filename.length()-1)  == '\\'))
  			{
  			filename = filename.substring(filename.lastIndexOf('\\')+1);
  			file_data = file_data+filename+"\n";
  
  			}
  			else
  			{
  			folder_data = folder_data + counter +".  "+filename+"\n";

	// The folders are also stored in a vector variable of the
	// name folder_vector.

  			folder_vector.addElement((Object)filename);
  			counter++;
  			}
  
  
  					}
  					
  				}
  						
  
  		   }
  		   catch(IOException ex)
  		   {		
  				System.out.println("Exception occured");
  		   }
  		}
  		while (!(event instanceof EndDocument));
  		
  		System.out.println("**** END OF DOCUMENT ****");
		// end of document
		// Socket Connection and Input and OutputStreams are  
		// closed...
  		try
  		{
  		socket.close();
  		socket_inputstream.close();
  		socket_outputstream.close();
  		}
  		catch(IOException e)
  		{
  			System.out.println("user list"+ e);
  			
  		}
	// Numbering is done on the file name stored in the String
	// variable by the name file_data.
  		file_data = counter+".  "+file_data;
  		counter++;
  		StringBuffer file_data_buffer = new StringBuffer();
  
  		for (int i = 0;i<file_data.length()-1 ;i++ )
  		{
  			if (file_data.charAt(i) == '\n')
  			{
  				file_data_buffer = file_data_buffer.append(file_data.charAt(i));
  				file_data_buffer = file_data_buffer.append(counter+".  ");
  				counter++;
  			}
  			else
  			{
  				file_data_buffer = file_data_buffer.append(file_data.charAt(i));
  			}
  			
  		}		
  		file_data = file_data_buffer.toString();
  
	//  the Final text to be displayed in the displayed in the 
	//  varaiable textFor Display...
  		text_for_display = "       Folders    \n"+folder_data+"\n"+"       Files \n"+file_data;
  		
  	 }
  	
	// This function is called to make the program compatible with 
	// the C# Listeners.
  		public byte [] appropriatelength(byte[] file_bytes, int file_size)
  		{
  				int count = 0;
  				byte b[] = new byte[1024];
  				int remaining = 1024-file_size;
  
  				for (int i = 0;i<file_bytes.length ;i++ )
  				{
  					b[i] = file_bytes[i];
  				}
  				
  				char a[] = new char[remaining];
  		
		// Length is known therefore 1024-length bytes are
		// filled with empty string... 
  				for (int i = 0;i<remaining ;i++ )
  				{
  				a[i] = 13;
  				}
  
  				String tempw = new String(a);
  				byte d[] = tempw.getBytes();
  
  				for (int i=file_size;i<1024 ;i++ )
  				{
  				b[i] = d[(i-file_size)];
  				}
  				
  				return (b);
  		}			// End Appropriate length.....
 }	// End class userlist_screen.... 

⌨️ 快捷键说明

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