1 16 17 package net.sf.jftp.gui.tasks; 20 21 import net.sf.jftp.JFtp; 22 23 import net.sf.jftp.config.*; 25 26 import java.awt.*; 27 import java.awt.event.*; 28 29 import java.io.*; 30 31 import java.lang.*; 32 33 import java.util.*; 34 35 import javax.swing.*; 36 37 38 public class LastConnections 39 { 40 public static String SENTINEL = new String ("********************"); 41 private static JFtp jftp; 42 43 public LastConnections(JFtp jftp) 46 { 47 this.jftp = jftp; 48 49 } 51 52 public static void writeToFile(String [][] a, int capacity) 60 { 61 try 62 { 63 File f1 = new File(Settings.appHomeDir); 64 f1.mkdir(); 65 66 File f2 = new File(Settings.last_cons); 67 f2.createNewFile(); 68 69 FileOutputStream fos; 70 PrintStream out; 71 72 fos = new FileOutputStream(Settings.last_cons); 73 out = new PrintStream(fos); 74 75 for(int i = 0; i < capacity; i++) 78 { 79 int j = 0; 81 out.println(a[i][j]); 82 83 while((j < JFtp.CAPACITY) && !(a[i][j].equals(SENTINEL))) 84 { 85 j++; 86 out.println(a[i][j]); 87 88 } 91 92 } 94 95 106 jftp.updateMenuBar(); 107 } 108 catch(Exception e) 109 { 110 e.printStackTrace(); 111 } 112 } 113 114 public static String [][] readFromFile(int capacity) 116 { 117 String [][] retVal = new String [capacity][JFtp.CONNECTION_DATA_LENGTH]; 120 121 try 124 { 125 File f1 = new File(Settings.appHomeDir); 126 f1.mkdir(); 127 128 File f2 = new File(Settings.last_cons); 129 130 if(!f2.exists()) 133 { 134 init(capacity); 135 } 136 137 147 RandomAccessFile raf = new RandomAccessFile(f2, "r"); 148 149 String [] oldValues = new String [capacity]; 153 String firstSection = new String (""); 154 boolean oldVersion = true; 155 156 for(int i = 0; i < capacity; i++) 158 { 159 if(capacity < JFtp.CAPACITY) 160 { 161 oldVersion = false; 162 163 break; 164 } 165 166 oldValues[i] = raf.readLine(); 167 firstSection = oldValues[i].substring(0, 3); 168 169 if(!(firstSection.equals("FTP")) && 173 !(firstSection.equals("SFT")) && 174 !(firstSection.equals("SMB")) && 175 !(firstSection.equals("NFS")) && 176 !(firstSection.equals("nul"))) 177 { 178 oldVersion = false; 180 } 181 182 if(!oldVersion) 183 { 184 break; 186 } 187 } 188 189 raf = new RandomAccessFile(f2, "r"); 191 192 if(oldVersion) 193 { 194 for(int i = 0; i < capacity; i++) 196 { 197 oldValues[i] = raf.readLine(); 198 } 199 200 changeFile(oldValues); 201 } 202 203 raf = new RandomAccessFile(f2, "r"); 205 206 for(int i = 0; i < capacity; i++) 207 { 208 int j = 0; 209 retVal[i][j] = raf.readLine(); 210 211 while((j < JFtp.CONNECTION_DATA_LENGTH) && 214 !(retVal[i][j].equals(SENTINEL))) 215 { 216 j++; 217 retVal[i][j] = raf.readLine(); 218 219 } 223 224 } 227 228 } 230 catch(Exception ex) 231 { 232 ex.printStackTrace(); 233 } 234 235 return retVal; 236 } 237 238 public static String [][] prepend(String [] newString, int capacity, 240 boolean newConnection) 241 { 242 String [][] lastCons = new String [capacity][JFtp.CONNECTION_DATA_LENGTH]; 245 246 lastCons = readFromFile(capacity); 247 248 for(int i = 0; i < capacity; i++) 249 { 250 int j = 0; 251 252 while(!(lastCons[i][j].equals(SENTINEL))) 253 { 254 j++; 258 } 259 260 } 262 263 String [] temp = new String [JFtp.CONNECTION_DATA_LENGTH]; 266 267 int j = 0; 268 269 while(!(lastCons[0][j].equals(SENTINEL))) 272 { 273 temp[j] = lastCons[0][j]; 275 276 j++; 281 } 282 283 temp[j] = SENTINEL; 284 285 j = 0; 286 287 while(!(newString[j].equals(SENTINEL))) 288 { 289 lastCons[0][j] = newString[j]; 290 j++; 291 } 292 293 lastCons[0][j] = SENTINEL; 294 295 j++; 298 299 while(j < JFtp.CONNECTION_DATA_LENGTH) 300 { 301 lastCons[0][j] = ""; 302 j++; 303 } 304 305 for(int i = 0; i < capacity; i++) 307 { 308 if((i + 1) != capacity) 311 { 312 j = 0; 314 315 while(!(temp[j].equals(SENTINEL))) 318 { 319 newString[j] = temp[j]; 320 321 j++; 323 } 324 325 newString[j] = SENTINEL; 326 327 j = 0; 330 331 while(!(lastCons[i + 1][j].equals(SENTINEL))) 333 { 334 temp[j] = lastCons[i + 1][j]; 336 337 j++; 347 } 348 349 temp[j] = SENTINEL; 351 352 j = 0; 355 356 while(!(newString[j].equals(SENTINEL))) 358 { 359 lastCons[i + 1][j] = newString[j]; 360 361 j++; 363 } 364 365 lastCons[i + 1][j] = SENTINEL; 366 367 369 377 378 } 380 381 } 383 384 for(int i = 0; i < capacity; i++) 386 { 387 j = 0; 388 389 while(!lastCons[i][j].equals(SENTINEL)) 390 { 391 if(lastCons[i][j].equals(SENTINEL)) 393 { 394 break; 395 } 396 397 j++; 398 } 399 400 } 402 403 if(newConnection) 405 { 406 writeToFile(lastCons, capacity); 407 } 408 409 return lastCons; 410 } 411 412 public static String [][] moveToFront(int position, int capacity) 414 { 415 String [][] lastCons = new String [capacity][JFtp.CONNECTION_DATA_LENGTH]; 419 String [][] newLastCons = new String [capacity][JFtp.CONNECTION_DATA_LENGTH]; 420 421 lastCons = readFromFile(capacity); 424 425 String [] temp = new String [JFtp.CONNECTION_DATA_LENGTH]; 427 428 int j = 0; 430 temp[j] = lastCons[position][j]; 431 432 while(!(lastCons[position][j].equals(SENTINEL))) 433 { 434 j++; 435 temp[j] = lastCons[position][j]; 436 } 437 438 j = 0; 439 440 while(!(lastCons[position][j].equals(SENTINEL))) 442 { 443 j++; 445 } 446 447 454 newLastCons = prepend(temp, position + 1, false); 455 456 for(int i = 0; i <= position; i++) 457 { 458 j = 0; 459 460 while(!(newLastCons[i][j].equals(SENTINEL))) 463 { 464 lastCons[i][j] = newLastCons[i][j]; 470 j++; 471 } 472 473 lastCons[i][j] = SENTINEL; 474 } 475 476 writeToFile(lastCons, capacity); 478 479 return lastCons; 480 } 481 482 public static int findString(String [] findVal, int capacity) 484 { 485 String [][] lastCons = new String [capacity][JFtp.CONNECTION_DATA_LENGTH]; 488 489 lastCons = readFromFile(capacity); 490 491 for(int i = 0; i < capacity; i++) 492 { 493 int j = 0; 494 495 while((j < JFtp.CAPACITY) && findVal[j].equals(lastCons[i][j]) && 496 !(lastCons[i][j].equals(SENTINEL)) && 497 !(findVal[j].equals(SENTINEL))) 498 { 499 j++; 504 505 } 508 509 if(findVal[j].equals(lastCons[i][j])) 510 { 511 return i; 515 } 516 else 517 { 518 } 523 } 524 525 return -1; 527 } 528 529 531 545 private static void init(int capacity) 546 { 547 try 549 { 550 FileOutputStream fos; 551 PrintStream out; 552 553 fos = new FileOutputStream(Settings.last_cons); 554 out = new PrintStream(fos); 555 556 for(int i = 0; i < capacity; i++) 557 { 558 out.println("null"); 559 out.println(SENTINEL); 560 } 561 562 fos.close(); 563 } 564 catch(Exception e) 565 { 566 e.printStackTrace(); 567 } 568 569 } 571 572 private static void changeFile(String [] oldValues) 574 { 575 StringTokenizer tokens; 576 577 String [][] newData = new String [JFtp.CAPACITY][JFtp.CONNECTION_DATA_LENGTH]; 578 579 for(int i = 0; i < JFtp.CAPACITY; i++) 582 { 583 tokens = new StringTokenizer(oldValues[i], " "); 585 586 int j = 0; 587 588 while((tokens.hasMoreTokens())) 589 { 590 newData[i][j] = tokens.nextToken(); 591 592 j++; 594 } 595 596 newData[i][j] = SENTINEL; 597 598 if(newData[i][0].equals("SFTP") && (j == 5)) 605 { 606 String temp = new String (""); 607 String temp2 = new String (""); 608 609 temp = newData[i][4]; 610 newData[i][4] = "22"; 611 612 temp2 = newData[i][5]; 613 614 newData[i][5] = temp; 615 616 newData[i][6] = SENTINEL; 620 621 628 } 629 630 } 632 633 writeToFile(newData, JFtp.CAPACITY); 635 } 636 637 } 639 640 641 | Popular Tags |