Monday, November 26, 2007

Read Data from the FTPInputStream

public byte[] getDataInBytes() throws IOException {
    final int BUFFER_SIZE = 1024 * 64;
    byte[] fileInBytes = new byte[BUFFER_SIZE];
    int read = 0;
    int w = 0;
    int len;
    FTPInputStream ftpInStream = null;
    try {
        final String ftpPoolName = "FTPPoolName";
        final String ftpFilePath = "****";
        ftpInStream = new FTPInputStream(ftpPoolName, ftpFilePath, "fileName");
        do {
            w += read;
            len = fileInBytes.length - w;
                if (len <= 0) {
                    byte[] b = new byte[fileInBytes.length + BUFFER_SIZE];
                System.arraycopy(fileInBytes, 0, b, 0, w);
                fileInBytes = b;
                len = fileInBytes.length - w;
            }
} while ((read = ftpInStream.read(fileInBytes, w, len)) != -1);
} finally {
if (ftpInStream != null) {
ftpInStream.close();
}
}
if (fileInBytes.length > w) {
byte[] b = new byte[w];
System.arraycopy(fileInBytes, 0, b, 0, w);
fileInBytes = b;
}
return fileInBytes;
}