Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util.resource;
 
 import java.io.File;
 import java.net.URI;
 import java.net.URL;
 
 
 
 /* ------------------------------------------------------------ */
File Resource. Handle resources of implied or explicit file type. This class can check for aliasing in the filesystem (eg case insensitivity). By default this is turned on, or it can be controlled by calling the static method

See also:
FileResource.setCheckAliases(boolean)
 
 public class FileResource extends URLResource
 {
     private static final Logger LOG = Log.getLogger(FileResource.class);
     private static boolean __checkAliases = true;
 
     /* ------------------------------------------------------------ */
     private File _file;
     private transient URL _alias=null;
     private transient boolean _aliasChecked=false;
 
     /* ------------------------------------------------------------------------------- */
    
setCheckAliases.

Parameters:
checkAliases True of resource aliases are to be checked for (eg case insensitivity or 8.3 short names) and treated as not found.
 
     public static void setCheckAliases(boolean checkAliases)
     {
         =checkAliases;
     }
 
     /* ------------------------------------------------------------------------------- */
    
getCheckAliases.

Returns:
True of resource aliases are to be checked for (eg case insensitivity or 8.3 short names) and treated as not found.
 
     public static boolean getCheckAliases()
     {
         return ;
     }
     
     /* -------------------------------------------------------- */
     public FileResource(URL url)
         throws IOExceptionURISyntaxException
     {
         super(url,null);
 
         try
         {
             // Try standard API to convert URL to file.
              =new File(new URI(url.toString()));
         }
         catch (Exception e)
         {
             .ignore(e);
             try
             {
                 // Assume that File.toURL produced unencoded chars. So try
                 // encoding them.
                 String file_url="file:"+URIUtil.encodePath(url.toString().substring(5));           
                 URI uri = new URI(file_url);
                 if (uri.getAuthority()==null
                      = new File(uri);
                else
                     = new File("//"+uri.getAuthority()+URIUtil.decodePath(url.getFile()));
            }
            catch (Exception e2)
            {
                .ignore(e2);
                // Still can't get the file.  Doh! try good old hack!
                checkConnection();
                Permission perm = .getPermission();
                 = new File(perm==null?url.getFile():perm.getName());
            }
        }
        if (.isDirectory())
        {
            if (!.endsWith("/"))
                =+"/";
        }
        else
        {
            if (.endsWith("/"))
                =.substring(0,.length()-1);
        }
    }
    /* -------------------------------------------------------- */
    FileResource(URL urlURLConnection connectionFile file)
    {
        super(url,connection);
        =file;
        if (.isDirectory() && !.endsWith("/"))
            =+"/";
    }
    
    /* -------------------------------------------------------- */
    @Override
    public Resource addPath(String path)
        throws IOException,MalformedURLException
    {
        URLResource r=null;
        String url=null;
        path = org.eclipse.jetty.util.URIUtil.canonicalPath(path);
       
        if ("/".equals(path))
            return this;
        else if (!isDirectory())
        {
            r=(FileResource)super.addPath(path);
            url=r._urlString;
        }
        else
        {
            if (path==null)
                throw new MalformedURLException();   
            
            // treat all paths being added as relative
            String rel=path;
            if (path.startsWith("/"))
                rel = path.substring(1);
            
            url=URIUtil.addPaths(,URIUtil.encodePath(rel));
            r=(URLResource)Resource.newResource(url);
        }
        
        String encoded=URIUtil.encodePath(path);
        int expected=r.toString().length()-encoded.length();
        int index = r._urlString.lastIndexOf(encodedexpected);
        
        if (expected!=index && ((expected-1)!=index || path.endsWith("/") || !r.isDirectory()))
        {
            if (!(r instanceof BadResource))
            {
                ((FileResource)r).=new URL(url);
                ((FileResource)r).=true;
            }
        }                             
        return r;
    }
   
    
    /* ------------------------------------------------------------ */
    @Override
    public URL getAlias()
    {
        if ( && !)
        {
            try
            {    
                String abs=.getAbsolutePath();
                String can=.getCanonicalPath();
                
                if (abs.length()!=can.length() || !abs.equals(can))
                    =Resource.toURL(new File(can));
                
                =true;
                
                if (!=null && .isDebugEnabled())
                {
                    .debug("ALIAS abs="+abs);
                    .debug("ALIAS can="+can);
                }
            }
            catch(Exception e)
            {
                .warn(.,e);
                return getURL();
            }                
        }
        return ;
    }
    
    /* -------------------------------------------------------- */
    
Returns true if the resource exists.
    @Override
    public boolean exists()
    {
        return .exists();
    }
        
    /* -------------------------------------------------------- */
    
Returns the last modified time
    @Override
    public long lastModified()
    {
        return .lastModified();
    }
    /* -------------------------------------------------------- */
    
Returns true if the respresenetd resource is a container/directory.
    @Override
    public boolean isDirectory()
    {
        return .isDirectory();
    }
    /* --------------------------------------------------------- */
    
Return the length of the resource
    @Override
    public long length()
    {
        return .length();
    }
        
    /* --------------------------------------------------------- */
    
Returns the name of the resource
    @Override
    public String getName()
    {
        return .getAbsolutePath();
    }
        
    /* ------------------------------------------------------------ */
    
Returns an File representing the given resource or NULL if this is not possible.
    @Override
    public File getFile()
    {
        return ;
    }
        
    /* --------------------------------------------------------- */
    
Returns an input stream to the resource
    @Override
    public InputStream getInputStream() throws IOException
    {
        return new FileInputStream();
    }
        
    /* --------------------------------------------------------- */
    
Returns an output stream to the resource
    @Override
    public OutputStream getOutputStream()
        throws java.io.IOExceptionSecurityException
    {
        return new FileOutputStream();
    }
        
    /* --------------------------------------------------------- */
    
Deletes the given resource
    @Override
    public boolean delete()
        throws SecurityException
    {
        return .delete();
    }
    /* --------------------------------------------------------- */
    
Rename the given resource
    @Override
    public boolean renameToResource dest)
        throws SecurityException
    {
        ifdest instanceof FileResource)
            return .renameTo( ((FileResource)dest).);
        else
            return false;
    }
    /* --------------------------------------------------------- */
    
Returns a list of resources contained in the given resource
    @Override
    public String[] list()
    {
        String[] list =.list();
        if (list==null)
            return null;
        for (int i=list.length;i-->0;)
        {
            if (new File(,list[i]).isDirectory() &&
                !list[i].endsWith("/"))
                list[i]+="/";
        }
        return list;
    }
         
    /* ------------------------------------------------------------ */
    
Encode according to this resource type. File URIs are encoded.

Parameters:
uri URI to encode.
Returns:
The uri unchanged.
    @Override
    public String encode(String uri)
    {
        return uri;
    }
    
    /* ------------------------------------------------------------ */
    

Parameters:
o
Returns:
true of the object o is a FileResource pointing to the same file as this resource.
    @Override
    public boolean equalsObject o)
    {
        if (this == o)
            return true;
        if (null == o || ! (o instanceof FileResource))
            return false;
        FileResource f=(FileResource)o;
        return f._file ==  || (null !=  && .equals(f._file));
    }
    /* ------------------------------------------------------------ */
    

Returns:
the hashcode.
    @Override
    public int hashCode()
    {
       return null ==  ? super.hashCode() : .hashCode();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void copyTo(File destination)
        throws IOException
    {
        if (isDirectory())
        {
            IO.copyDir(getFile(),destination);
        }
        else
        {
            if (destination.exists())
                throw new IllegalArgumentException(destination+" exists");
            IO.copy(getFile(),destination);
        }
    }
New to GrepCode? Check out our FAQ X