⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpfile.java

📁 用java实现的摄像头编程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      throws SecurityException
   {  FtpFile[] fs = null; 
      String line; 
      BufferedReader ibuf  = null;

      int listtype = client.getContext().getListCommandMode();
      try
      {  boolean error=false;
	 Vector fv = new Vector();

	 ibuf = new BufferedReader(new InputStreamReader(new FtpListInputStream(this)));
	 while ((line = ibuf.readLine()) != null)
	 {  try
	    {  switch(listtype)
	       {
	       case FtpContext.LIST:
		  fv.addElement(FtpFile.examineListLine(this,line)); 
		  break;
	       case FtpContext.NAME_LIST:
		  if(line.startsWith("/") && line.endsWith(":"))
		     break;
		  if(line.indexOf("//")!=-1)
		     line = line.substring(line.lastIndexOf('/')+1);
	       case FtpContext.NAME_LIST_LS_F:
	       case FtpContext.NAME_LIST_LS_P:
		  if(line.length()>0)
		     fv.addElement(FtpFile.examineNameListLine(this,line,listtype)); 
		  break;
	       case FtpContext.NAME_LIST_LS_LA:
		  fv.addElement(FtpFile.examineUnixListLine(this,line)); 
		  break;
	       }
	    }
	    catch(NoSuchElementException e)
	    {  if(!error && (e.getMessage()==null || !e.getMessage().equals("skip")))
	       {  client.printlog("\n   < File: Invalid List Format ! >" +
		                 (e.getMessage()!=null?"\n   "+e.getMessage():"")+
			         "\n   Line: "+line+
	                         "\n   Try: 'NAME_LIST' List Command");
	          error = true; } }
	 }
         fs = new FtpFile[fv.size()];
         fv.copyInto(fs);
      }
      catch(IOException e)
         {  client.printlog("< File: Can't list directory! >\n"+e); }
      finally
      {  try 
	 {  Reader r; 
	    if(ibuf!=null) { r=ibuf; ibuf=null; r.close(); }
	 } catch(IOException e) 
	    { client.printerr(e); }
      }
      return fs;
   }

   public CoFile[] listCoFiles(CoFilenameFilter filter)
      throws SecurityException
   {  FtpFile[] fs = (FtpFile[])listCoFiles(); 
      if(filter!=null)
      {  Vector fv = new Vector();
         for(int i=0;i<fs.length;i++)
	    if(filter.accept(this,fs[i].getName()))
	       fv.addElement(fs[i]);
         fs = new FtpFile[fv.size()];
         fv.copyInto(fs); }
      return fs;
   }

   private static FtpFile examineListLine(FtpFile path,String line)
      throws NoSuchElementException
   {  if("0123456789".indexOf(line.charAt(0))<0)
	 /* unix list format never strarts with number */
	 return FtpFile.examineUnixListLine(path,line); 
      /* windows list format always starts with number */
      else return FtpFile.examineWinListLine(path,line); }

   private static FtpFile examineNameListLine(FtpFile path,String line,int listtype)
      throws NoSuchElementException
   {  FtpFile ff = new FtpFile();
      ff.client = path.client;
      switch(listtype)
      {
      case FtpContext.NAME_LIST:
         /**************************************
         * file folder executable link special *
         **************************************/
	 if(line.indexOf('.') != -1)
	    ff.access   = "-?????????";
	 else ff.access = "d?????????";
	 break;
      case FtpContext.NAME_LIST_LS_P:
         /***************************************
         * file folder/ executable link special *
         ***************************************/
	 if(line.endsWith("/"))
	 {  ff.access = "d?????????";
	    line = line.substring(0,line.length()-1);
	 } else ff.access   = "-?????????";
	 break;
      case FtpContext.NAME_LIST_LS_F:
         /*****************************************
         * file folder/ executable* link@ special *
         *****************************************/
	 if(line.endsWith("/"))
	 {  ff.access = "d?????????";
	    line = line.substring(0,line.length()-1);
	 } else 
	 if(line.endsWith("*"))
	 {  ff.access = "-??x??x??x";
	    line = line.substring(0,line.length()-1);
	 } else 
	 if(line.endsWith("@"))
	 {  ff.access = "l?????????";
	    line = line.substring(0,line.length()-1);
	 } else
	    ff.access = "-?????????";
	 break;
      }
      ff.owner       = "";
      ff.group       = "";
      ff.size        = -1; 
      ff.date        = 0L;
      ff.path        = path.getAbsolutePath();

      if(!ff.path.endsWith("/"))
	 ff.path = ff.path + '/' + line;
      else ff.path = ff.path + line;

      ff.sortSetup(line);       

      /* Skip current '.' and parent '..' directory aliases. */
      if(ff.getName().compareTo(".")==0 || ff.getName().compareTo("..")==0)
	 throw new NoSuchElementException("skip");      
      return ff;
   }

   private static FtpFile examineWinListLine(FtpFile path,String line) 
      throws NoSuchElementException
   {  FtpFile ff = new FtpFile();
      ff.client = path.client;
      /**********************************************
      * 10-16-01  11:35PM                 1479 file *
      * 10-16-01  11:37PM       <DIR>          awt  *
      **********************************************/
      try 
      {	 StringTokenizer toker = new StringTokenizer(line);
	 ff.date          = examineWinListDate
	                   (toker.nextToken(),  /* date */
			    toker.nextToken()); /* time */
	 String size2dir  = toker.nextToken();  /* size or dir */
	 if(size2dir.equals("<DIR>"))
	 {  ff.access     = "d?????????";
            ff.size       = -1; 
	 } else {
	    ff.access     = "-?????????";
            ff.size       = Long.parseLong(size2dir); } 
	 String name      = toker.nextToken("").trim(); /* name */
         
	 ff.owner         = "";
	 ff.group         = "";

	 ff.path = path.toString();  

	 if(!ff.path.endsWith("/"))
	    ff.path = ff.path + '/' + name;
	 else ff.path = ff.path + name;	 
	 
	 ff.sortSetup(name);
      } 
      catch(NumberFormatException e)
         { throw new NoSuchElementException("Win-List: Invalid Number Format"); }
      /* Skip current '.' and parent '..' directory aliases. */
      if(ff.getName().compareTo(".")==0 || ff.getName().compareTo("..")==0)
	 throw new NoSuchElementException("skip");
      return ff;
   }

   private static long examineWinListDate(String date, String time)
   {  /**********************
      * 10-16-01    11:35PM *
      * 10-16-2001  11:35PM *
      **********************/
      Calendar c = Calendar.getInstance();
      try
      {  StringTokenizer toker = new StringTokenizer(date,"-");
         int m = Integer.parseInt(toker.nextToken()),
	     d = Integer.parseInt(toker.nextToken()),
	     y = Integer.parseInt(toker.nextToken());
	 if(y>=70) y+=1900; else y+=2000;
         toker = new StringTokenizer(time,":APM");
	 c.set(y,m,d,(time.endsWith("PM")?12:0)+
	             Integer.parseInt(toker.nextToken()),
	             Integer.parseInt(toker.nextToken()));
      }
      catch(NumberFormatException e)
         { throw new NoSuchElementException("Win-List: Invalid Date Format"); }
      return c.getTime().getTime();
   }

   private static FtpFile examineUnixListLine(FtpFile path,String line) 
      throws NoSuchElementException
   {  FtpFile ff = new FtpFile();
      ff.client = path.client;

      /*********************************************************************
      * -rw-r--r--   1 owner   group       239 Nov  9  1998 file           *
      * crw-rw-rw-   1 root    sys      11, 42 Aug  3  2000 sun_device     * 
      * crw-------   1 root    sys  137 0x0089 Nov 25 11:39 hpux_device    * 
      * drw-r--r--   1 owner   group        58 Nov 12 13:51 folder         *
      * lrw-r--r--   1 owner   group        58 Nov 12 13:51 link -> source *
      * -rw-r--r--   1 4                    58 Nov 12 13:51 uu_file        *
      * crw-------   1 4            137 0x0089 Nov 25 11:39 uu_device      * 
      * drw-r--r--   1 4                    58 Nov 12 13:51 uu_folder      *
      * lrw-r--r--   1 4                    58 Nov 12 13:51 uu_link -> src *
      **********************************************************************/
      try 
      {	 if(line.indexOf("->")>=0)
	    line = line.substring(0,line.indexOf("->"));
	 StringTokenizer toker = new StringTokenizer(line);
	 ff.access        = toker.nextToken();  /* access */
                            toker.nextToken();  /* links */
	 ff.owner         = toker.nextToken();  /* owner */
	 ff.group         = toker.nextToken();  /* group */
	 String      size = toker.nextToken();  /* size */
	 if(size.endsWith(","))
	    size = size.substring(0,size.indexOf(","));
	 String uu = size;
	 if(ff.access.startsWith("c")) 
	    uu = toker.nextToken();             /* device */
         /* if uu.charAt(0) is not digit try uu_file format */
	 if("0123456789".indexOf(uu.charAt(0))<0)
	    { size = ff.group; ff.group = ""; }
	 ff.size          = Integer.parseInt(size);
         ff.date          = examineUnixListDate
	 (("0123456789".indexOf(uu.charAt(0))<0?uu
	                   :toker.nextToken()), /* month */
	                    toker.nextToken(),  /* day */
	                    toker.nextToken()); /* time or year */
	 String name      = toker.nextToken("").trim(); /* name */

         ff.path = path.toString();  
	 if(!ff.path.endsWith("/"))
	    ff.path = ff.path + '/' + name;
	 else ff.path = ff.path + name;	 
	 ff.sortSetup(name);
      } 
      catch(NumberFormatException e)
         { throw new NoSuchElementException("Unix-List: Invalid Number Format"); }
      catch(NoSuchElementException e)
      {  /* Skip 'total n' message. */
	 try
	 {  StringTokenizer toker = new StringTokenizer(line);
	    if(!toker.nextToken().equals("total"))
	       throw e;
            Long.parseLong(toker.nextToken());
            if(!toker.hasMoreTokens())
	      throw new NoSuchElementException("skip");
	    else throw e;
	 } catch(NumberFormatException x)
	    { throw e; }
      }
      /* Skip current '.' and parent '..' directory aliases. */
      if(ff.getName().compareTo(".")==0 || ff.getName().compareTo("..")==0)
	 throw new NoSuchElementException("skip");
      return ff;
   }
   
   private static long examineUnixListDate(String month, String day, String year2time)
   {  /***************
      * Nov  9  1998 *
      * Nov 12 13:51 *
      ***************/
      Calendar c = Calendar.getInstance();
      month = month.toUpperCase();
      try
      {  for(int m=0;m<12;m++)
	    if(month.equals(months[m]))
	    {  if(year2time.indexOf(':')!= -1)
	       {  /* current year */
	          c.setTime(new Date(System.currentTimeMillis()));
		  StringTokenizer toker 
		     = new StringTokenizer(year2time,":");
		  /* date and time */
		  c.set(c.get(Calendar.YEAR), m, 
			Integer.parseInt(day), 
			Integer.parseInt(toker.nextToken()), 
			Integer.parseInt(toker.nextToken()));
	       } else 
		  /* date */
	          c.set(Integer.parseInt(year2time),m, 
			Integer.parseInt(day),0,0);
	       break; }
      }
      catch(NumberFormatException e)
         { throw new NoSuchElementException("Unix-List: Invalid Date Format"); }
      return c.getTime().getTime();
   }

   public String toString()   
      { return getAbsolutePath(); }   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -