public void startRead() throws Exception { // checkClosed(); readedLines = 0; paused = false; buffer = new ArrayList<String>(); client = new FTPClient(); client.setType(FTPClient.TYPE_BINARY); client.connect(host.getHost(), host.getPort()); client.login(host.getUser(), host.getPassword()); ftpFileSize = client.fileSize(logFile.getPath()); isClosed = false; final PipedOutputStream pos = new PipedOutputStream(); reader = new BufferedReader(new InputStreamReader(new PipedInputStream(pos), host.defaultEncoding)); // FTP read thread ftpReadThread = new Thread( new Runnable() { @Override public void run() { System.out.println("4"); java.util.Date md = null; try { String nextLine; while (!isClosed) { System.out.println("Ftp read thread run"); if (!isPaused()) { md = client.modifiedDate(logFile.getPath()); System.out.println("Modification date :" + md + "; and size: " + ftpFileSize); if (client.fileSize(logFile.getPath()) != ftpFileSize) { client.download(logFile.getPath(), pos, ftpFileSize, null); ftpFileSize = client.fileSize(logFile.getPath()); if ((nextLine = reader.readLine()) != null && !isClosed) { // buffer.add(String.format("%6d: %s", // (buffer.size()+1), nextLine)); buffer.add(nextLine); } } } Thread.sleep(500); } } catch (Exception e) { try { close(); } catch (Exception e1) { e1.printStackTrace(); } e.printStackTrace(); } } }); // Connection monitoring thread new Thread( new Runnable() { public void run() { System.out.println("6"); while (client.isConnected() && !isClosed) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } if (!isClosed) { System.out.println("Connection failed!"); ftpReadThread.interrupt(); try { close(); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("Stopped FTP connection monitor thread."); } } }, "Conn monitor") .start(); System.out.println("1"); // readThread.start(); Thread.sleep(500); System.out.println("2"); ftpReadThread.start(); System.out.println("3"); }
@Override public String getName() { return host.getHost() + logFile.getPath() + logFile.getName(); }