📄 showfiles.java
字号:
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..
try
{
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)
{
System.out.println(e);
}
// Function used for parsing is called...
parseData();
directory_flag = true;
}
catch (Exception e)
{
System.out.println(e);
directory_flag = false;
option.setText("");
}
connection_flag = true;
}
catch (Exception e)
{
connection_flag = false;
option.setText("");
}
// If dircetory_flag as well as as the connection_flag are true then
if (connection_flag && directory_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,host_address, directory_name,false);
}
} // End If....
else
{
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=\"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);
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);
} // End Else
}
// 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 showfiles...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -