📄 rfbproto.java
字号:
while (bytesRead!=-1)
{
counter += bytesRead;
myDeflater.setInput(byteBuffer, 0, bytesRead);
myDeflater.finish();
compressedSize = myDeflater.deflate(CompressionBuffer);
myDeflater.reset();
// If the compressed data is larger than the original one, we're dealing with
// already compressed data
if (compressedSize > bytesRead)
fCompress = false;
this.writeRfbFileTransferMsg(
contentType,
contentParam,
(fCompress ? 1 : 0),
(fCompress ? compressedSize-1 : bytesRead-1),
null
);
// Todo: Test write error !
os.write(
fCompress ? CompressionBuffer : byteBuffer,
0,
fCompress ? compressedSize : bytesRead
);
// Todo: test read error !
bytesRead = fis.read(byteBuffer);
// viewer.ftp.connectionStatus.setText("Sent: "+ counter + " bytes of "+ f.length() + " bytes");
viewer.ftp.jProgressBar.setValue((int)((counter * 100) / f.length()));
viewer.ftp.connectionStatus.setText(">>> Sending File: " + source + " - Size: " + f.length() + " bytes - Progress: " + ((counter * 100) / f.length()) + "%");
if (fAbort == true)
{
fAbort = false;
fError = true;
break;
}
try
{
Thread.sleep(5);
}
catch(InterruptedException e)
{
System.err.println("Interrupted");
}
}
writeRfbFileTransferMsg(fError ? rfbAbortFileTransfer : rfbEndOfFile, 0, 0, 0, null);
fis.close();
return (fError ? -1 : 1);
}
//This method is internally used to send the file to the server once the server is ready
void sendFile()
{
try
{
viewer.ftp.disableButtons();
int size = is.readInt();
int length = is.readInt();
for (int i = 0; i < length; i++)
{
System.out.print((char) is.readUnsignedByte());
}
int ret = writeRfbFileTransferMsgForSendFile(
rfbFilePacket,
0,
0,
0,
sendFileSource);
viewer.ftp.refreshRemoteLocation();
if (ret != 1)
{
viewer.ftp.connectionStatus.setText(" > Error - File NOT sent");
viewer.ftp.historyComboBox.insertItemAt(new String(" > Error - File: <" + sendFileSource) + "> was not correctly sent (aborted by user or error)",0);
}
else
{
viewer.ftp.connectionStatus.setText(" > File sent");
viewer.ftp.historyComboBox.insertItemAt(new String(" > File: <" + sendFileSource) + "> was sent to Remote Machine",0);
}
viewer.ftp.historyComboBox.setSelectedIndex(0);
viewer.ftp.enableButtons();
}
catch (IOException e)
{
System.err.println(e);
}
}
//Call this method to send a file from local pc to server
void offerLocalFile(String source, String destinationPath)
{
try
{
sendFileSource = source;
File f = new File(source);
// sf@2004 - Add support for huge files
long lSize = f.length();
int iLowSize = (int)(lSize & 0x00000000FFFFFFFF);
int iHighSize = (int)(lSize >> 32);
String temp = destinationPath + f.getName();
writeRfbFileTransferMsg(
rfbFileTransferOffer,
0,
iLowSize, // f.length(),
temp.length(),
temp);
// sf@2004 - Send the high part of the size
byte b[] = new byte[4];
byte by = 0;
long c = 0;
c = iHighSize & 0xFF000000;
by = (byte) (c >>> 24);
b[0] = by;
c = iHighSize & 0xFF0000;
by = (byte) (c >>> 16);
b[1] = by;
c = iHighSize & 0xFF00;
by = (byte) (c >>> 8);
b[2] = by;
c = iHighSize & 0xFF;
by = (byte) c;
b[3] = by;
os.write(b);
}
catch (IOException e)
{
System.err.println(e);
}
}
//Internally used.
//Handles acknowledgement that the file has been deleted on the server
void deleteRemoteFileFeedback() throws IOException
{
is.readInt();
int length = is.readInt();
String f = "";
for (int i = 0; i < length; i++)
{
f += (char)is.readUnsignedByte();
}
viewer.ftp.refreshRemoteLocation();
viewer.ftp.historyComboBox.insertItemAt(new String(" > Deleted File On Remote Machine: " + f.substring(0, f.length()-1)),0);
viewer.ftp.historyComboBox.setSelectedIndex(0);
}
//Call this method to delete a file at server
void deleteRemoteFile(String text)
{
try
{
String temp = text;
writeRfbFileTransferMsg(
rfbCommand,
rfbCFileDelete,
0,
temp.length(),
temp);
}
catch (IOException e)
{
System.err.println(e);
}
}
//Internally used.
// Handles acknowledgement that the directory has been created on the server
void createRemoteDirectoryFeedback() throws IOException
{
is.readInt();
int length = is.readInt();
String f="";
for (int i = 0; i < length; i++)
{
f += (char)is.readUnsignedByte();
}
viewer.ftp.refreshRemoteLocation();
viewer.ftp.historyComboBox.insertItemAt(new String(" > Created Directory on Remote Machine: " + f.substring(0, f.length()-1)),0);
viewer.ftp.historyComboBox.setSelectedIndex(0);
}
//Call this method to create a directory at server
void createRemoteDirectory(String text)
{
try
{
String temp = text;
writeRfbFileTransferMsg(
rfbCommand,
rfbCDirCreate,
0,
temp.length(),
temp);
}
catch (IOException e)
{
System.err.println(e);
}
}
//Call this method to get a file from the server
void requestRemoteFile(String text, String localPath)
{
try
{
String temp = text;
receivePath = localPath;
writeRfbFileTransferMsg(
rfbFileTransferRequest,
0,
1, // 0 : compression not supported - 1 : compression supported
temp.length(),
temp);
}
catch (IOException e)
{
System.err.println(e);
}
}
//Internally used when transferring file from server. Here, the server sends
//a rfb packet signalling that it is ready to send the file requested
void receiveFileHeader() throws IOException
{
fFileReceptionRunning = true;
fFileReceptionError = false;
viewer.ftp.disableButtons();
int size = is.readInt();
int length = is.readInt();
String tempName = "";
for (int i = 0; i < length; i++)
{
tempName += (char) is.readUnsignedByte();
}
// sf@2004 - Read the high part of file size (not yet in rfbFileTransferMsg for
// backward compatibility reasons...)
int sizeH = is.readInt();
long lSize = ((long)(sizeH) << 32) + size;
receiveFileSize = lSize;
viewer.ftp.connectionStatus.setText("Received: 0 bytes of " + lSize + " bytes");
fileSize=0;
fileChunkCounter = 0;
String fileName = receivePath;
fos = new FileOutputStream(fileName);
writeRfbFileTransferMsg(rfbFileHeader, 0, 0, 0, null);
}
//Internally used when transferring file from server. This method receives one chunk
//of the file
void receiveFileChunk() throws IOException
{
// sf@2004 - Size = 0 means file chunck not compressed
int size = is.readInt();
boolean fCompressed = (size != 0);
int length = is.readInt();
fileChunkCounter++;
// sf@2004 - allocates buffers for file chunck reception and decompression
byte[] ReceptionBuffer = new byte[length + 32];
// Read the incoming file data
// Todo: check error !
is.readFully(ReceptionBuffer,0, length);
if (fCompressed)
{
int bufSize = sz_rfbBlockSize + 1024; // Todo: set a more accurate value here
int decompressedSize = 0;
byte[] DecompressionBuffer = new byte[bufSize];
Inflater myInflater = new Inflater();
myInflater.setInput(ReceptionBuffer);
try
{
decompressedSize = myInflater.inflate(DecompressionBuffer);
}
catch (DataFormatException e)
{
System.err.println(e);
}
// Todo: check error !
fos.write(DecompressionBuffer, 0, decompressedSize);
fileSize += decompressedSize;
}
else
{
// Todo: check error !
fos.write(ReceptionBuffer, 0, length);
fileSize += length;
}
/*
for (int i = 0; i < length; i++)
{
fos.write(is.readUnsignedByte());
fileSize++;
}
*/
// viewer.ftp.connectionStatus.setText("Received: " + fileSize + " bytes of "+ receiveFileSize+ " bytes" );
viewer.ftp.jProgressBar.setValue((int)((fileSize * 100) / receiveFileSize));
viewer.ftp.connectionStatus.setText(">>> Receiving File: " + receivePath + " - Size: " + receiveFileSize + " bytes - Progress: " + ((fileSize * 100) / receiveFileSize) + "%");
if (fAbort == true)
{
fAbort = false;
fFileReceptionError = true;
writeRfbFileTransferMsg(rfbAbortFileTransfer, 0, 0, 0, null);
}
// sf@2004 - For old FT protocole only
/*
if(fileChunkCounter==10)
{
writeRfbFileTransferMsg(rfbFileHeader,0,0,0,null);
fileChunkCounter=0;
}
*/
}
//Internally used when transferring file from server. Server signals end of file.
void endOfReceiveFile(boolean fReceptionOk) throws IOException
{
int size = is.readInt();
int length = is.readInt();
fileSize=0;
fos.close();
viewer.ftp.refreshLocalLocation();
if (fReceptionOk && !fFileReceptionError)
{
viewer.ftp.connectionStatus.setText(" > File successfully received");
viewer.ftp.historyComboBox.insertItemAt(new String(" > File: <" + receivePath + "> received from Remote Machine" ),0);
}
else
{
// sf@2004 - Delete the incomplete receieved file for now (until we use Delta Transfer)
File f = new File(receivePath);
f.delete();
viewer.ftp.connectionStatus.setText(" > Error - File NOT received");
viewer.ftp.historyComboBox.insertItemAt(new String(" > Error - File: <" + receivePath + "> not correctly received from Remote Machine (aborted by user or error)") ,0);
}
fFileReceptionError = false;
fFileReceptionRunning = false;
viewer.ftp.historyComboBox.setSelectedIndex(0);
viewer.ftp.enableButtons();
}
//Call this method to read the contents of the server directory
void readServerDirectory(String text)
{
try
{
String temp = text;
writeRfbFileTransferMsg(
rfbDirContentRequest,
rfbRDirContent,
0,
temp.length(),
temp);
}
catch (IOException e)
{
System.err.println(e);
}
}
//Internally used to receive list of drives available on the server
void readFTPMsgDriveList() throws IOException
{
String str = "";
for (int i = 0; i < 4; i++)
{
is.readUnsignedByte();
}
int length = is.readInt();
for (int i = 0; i < length; i++)
{
char temp = (char) is.readUnsignedByte();
if (temp != '\0')
{
str += temp;
}
}
viewer.ftp.printDrives(str);
// sf@2004
// Finds the first readable drive and populates the local directory
viewer.ftp.changeLocalDirectory(viewer.ftp.getFirstReadableLocalDrive());
// Populate the remote directory
viewer.ftp.changeRemoteDrive();
viewer.ftp.refreshRemoteLocation();
}
//Internally used to receive directory content from server
//Here, the server marks the start of the directory listing
void readFTPMsgDirectoryList() throws IOException
{
is.readInt();
int length = is.readInt();
if (length == 0)
{
readFTPMsgDirectorydriveNotReady();
inDirectory2 = false;
}
else
{
// sf@2004 - New FT protocole sends remote directory name
String str = "";
for (int i = 0; i < length; i++)
{
char temp = (char) is.readUnsignedByte();
if (temp != '\0')
{
str += temp;
}
}
// viewer.ftp.changeRemoteDirectory(str);
}
}
//Internally used to receive directory content from server
//Here, the server sends one file/directory with it's attributes
void readFTPMsgDirectoryListContent() throws IOException
{
String fileName = "", alternateFileName = "";
byte contentType = 0;
int contentParamT = 0;
int contentParam = 0;
byte temp = 0;
int dwFileAttributes,
nFileSizeHigh,
nFileSizeLow,
dwReserved0,
dwReserved1;
long ftCreationTime, ftLastAccessTime, ftLastWriteTime;
char cFileName, cAlternateFileName;
int length = 0;
is.readInt();
length = is.readInt();
dwFileAttributes = is.readInt();
length -= 4;
ftCreationTime = is.readLong();
length -= 8;
ftLastAccessTime = is.readLong();
length -= 8;
ftLastWriteTime = is.readLong();
length -= 8;
nFileSizeHigh = is.readInt();
length -= 4;
nFileSizeLow = is.readInt();
length -= 4;
dwReserved0 = is.readInt();
length -= 4;
dwReserved1 = is.readInt();
length -= 4;
cFileName = (char) is.readUnsignedByte();
length--;
while (cFileName != '\0')
{
fileName += cFileName;
cFileName = (char) is.readUnsignedByte();
length--;
}
cAlternateFileName = (char) is.readByte();
length--;
while (length != 0)
{
alternateFileName += cAlternateFileName;
cAlternateFileName = (char) is.readUnsignedByte();
length--;
}
if (dwFileAttributes == 268435456
|| dwFileAttributes == 369098752
|| dwFileAttributes == 285212672
|| dwFileAttributes == 271056896
|| dwFileAttributes == 824705024
|| dwFileAttributes == 807927808
|| dwFileAttributes == 371720192
|| dwFileAttributes == 369623040)
{
fileName = " [" + fileName + "]";
remoteDirsList.add(fileName); // sf@2004
}
else
{
remoteFilesList.add(" " + fileName); // sf@2004
}
// a.add(fileName);
}
//Internally used to read directory content of server.
//Here, server signals end of directory.
void readFTPMsgDirectoryListEndContent() throws IOException
{
is.readInt();
int length = is.readInt();
// sf@2004
a.clear();
for (int i = 0; i < remoteDirsList.size(); i++)
a.add(remoteDirsList.get(i));
for (int i = 0; i < remoteFilesList.size(); i++)
a.add(remoteFilesList.get(i));
remoteDirsList.clear();
remoteFilesList.clear();
viewer.ftp.printDirectory(a);
}
//Internally used to signify the drive requested is not ready
void readFTPMsgDirectorydriveNotReady() throws IOException
{
System.out.println("Remote Drive unavailable");
viewer.ftp.connectionStatus.setText(" > WARNING - Remote Drive unavailable (possibly restricted access or media not present)");
viewer.ftp.remoteStatus.setText("WARNING: Remote Drive unavailable");
}
//Call this method to request the list of drives on the server.
void readServerDriveList()
{
try
{
viewer.rfb.writeRfbFileTransferMsg(
RfbProto.rfbDirContentRequest,
RfbProto.rfbRDrivesList,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -