📄 searchfiles.java
字号:
/**
* Importing the basic packages required by various classes during
* the execution of the program.
*/
import com.sun.kjava.*;
import java.lang.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.*;
/**
* The packages below are included because these packages are
* required by the XML classes.
*/
import org.kxml.*;
import org.kxml.io.*;
import org.kxml.parser.*;
// Declaration of a class searchfiles used for client level
// searching.
public class searchfiles extends Spotlet
{
// Declaring button variables.
private Button search;
private Button cancel;
// Declaring textField variables.
private TextField option;
String parent_information;
String text_for_display;
/**
* Declaraing variables to folder_data where the folder information
* is stored similerly the variable file_data where the file
* information is stored.
*/
String folder_data;
String file_data;
int counter;
// Storing the parent address..
String host_address;
// Declaring StreamConnection variable used for Sockets..
StreamConnection socket = null;
// Declaring InputStreams and OutputStreams used by the socket
// connections.
InputStream socket_inputstream;
OutputStream socket_outputstream;
Vector folder_vector;
/**
* Declaraing variables for the xmlparser and parse event(Where
* the call backs after the parsing will be stored.
*/
AbstractXmlParser xmlparser;
ParseEvent event;
// searchfiles constructor
searchfiles(String address, String parent_information)
{
// initializing the parameters with the varaiables declared
// earlier.
host_address = address;
this.parent_information = parent_information;
// The current graphics is obtained...
Graphics graphics = Graphics.getGraphics();
/**
* Initializing the variable of the graphics class and clearing
* the drawing region by calling the appropriate functions of
* the Graphics class namely (resetDrawRegion() and clearScreen()).
*/
graphics.resetDrawRegion();
graphics.clearScreen();
/**
* Displaying a string (Title) at a specified position by using
* the member function of the Graphics class
*/
graphics.drawString("Search for Files / Folders",30,15);
// To make the spotlet as the current spotlet..
register(NO_EVENT_OPTIONS);
/**
* Initializing the button object "search", placing it on the screen
* at the appropriate position and then painting(drawing it).
*/
search = new Button("Search",25,95);
search.paint();
/**
* Initializing the button object "cancel", placing it on the screen
* at the appropriate position and then painting(drawing it).
*/
cancel = new Button("Cancel",65,95);
cancel.paint();
/**
* Initializing the TextFiled object option, placing it on the screen
* at the appropriate position ,painting(drawing it) and making the
* caret blink by setting the TextField as the current field.
*/
option = new TextField("Search Criteria:",10,45,140,15);
option.paint();
option.setUpperCase(true);
option.setFocus();
} // End Constructor..
// Event Handling function. (KeyDown)..
public void keyDown(int x)
{
// Handle the input if the TextField is in focus.
if(option.hasFocus())
{
option.handleKeyDown(x);
}
}
// Event Handling function. (penDown)..
public void penDown(int x, int y)
{
// If search button is pressed..
if(search.pressed(x,y))
{
folder_vector = new Vector();
// The variables are assigned ..
text_for_display = "";
folder_data = "";
file_data = "";
counter = 1;
try
{
// A Socket connection is made on a port with the Listener(host address)
socket = (StreamConnection)Connector.open("socket://"+host_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...
socket_inputstream = socket.openInputStream();
socket_outputstream = socket.openOutputStream();
}
catch(IOException ae)
{
System.out.println("Couldn't open socket:");
}
// An XMLRequest which is to be sent to the Listener is formed..
String xmlRequest = "<?xml version='1.0' encoding='utf-8'?> <p2p_lng><request type=\"SEARCH\"><scope type=\""+parent_information+option.getText()+"\" mask=''></scope></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);
try
{
// 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));
}
catch(IOException e)
{
}
// Function used for parsing is called...
parseData();
// 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,host_address, "LOST",false);
}
// If cancel is pressed...
if(cancel.pressed(x,y))
{
folder_vector = new Vector();
text_for_display = "";
folder_data = "";
file_data = "";
counter = 1;
try
{
// A Socket connection is made on a port with the Listener
socket = (StreamConnection)Connector.open("socket://"+host_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...
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=\"SEARCH\"><scope type=\""+parent_information+"*.*\" mask=''></scope></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);
try
{
// 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));
}
catch(IOException e)
{
}
// Function used for parsing is called...
parseData();
// 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,host_address, "ROOT",false);
}
}
// 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)
{
StartTag stag = (StartTag)event;
// The Start Tag is identified
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(e);
}
// Numbering is done on the file name stored in the String
// variable by the name file_data.
if (!file_data.equals(""))
{
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];
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 of the class searchfiles...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -