Class WindowsShortcut


  • public class WindowsShortcut
    extends java.lang.Object
    Represents a Windows shortcut (typically visible to Java only as a '.lnk' file). Retrieved 2011-09-23 from http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java/672775#672775 Written by: (the stack overflow users, obviously!) Apache Commons VFS dependency removed by crysxd (why were we using that!?) https://github.com/crysxd Headerified, refactored and commented by Code Bling http://stackoverflow.com/users/675721/code-bling Network file support added by Stefan Cordes http://stackoverflow.com/users/81330/stefan-cordes Adapted by Sam Brightman http://stackoverflow.com/users/2492/sam-brightman Based on information in 'The Windows Shortcut File Format' by Jesse Hager <jessehager@iname.com> And somewhat based on code from the book 'Swing Hacks: Tips and Tools for Killer GUIs' by Joshua Marinacci and Chris Adamson ISBN: 0-596-00907-0 http://www.oreilly.com/catalog/swinghks/
    Since:
    13692
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean isDirectory  
      private boolean isLocal  
      private java.lang.String realFile  
    • Constructor Summary

      Constructors 
      Constructor Description
      WindowsShortcut​(java.io.File file)
      Constructs a new WindowsShortcut
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static int bytesToDword​(byte[] bytes, int off)  
      private static int bytesToWord​(byte[] bytes, int off)  
      private static byte[] getBytes​(java.io.InputStream in)
      Gets all the bytes from an InputStream
      private static byte[] getBytes​(java.io.InputStream in, java.lang.Integer max)
      Gets up to max bytes from an InputStream
      private static java.lang.String getNullDelimitedString​(byte[] bytes, int off)  
      java.lang.String getRealFilename()
      Returns the name of the filesystem object pointed to by this shortcut.
      boolean isDirectory()
      Tests if the shortcut points to a directory.
      boolean isLocal()
      Tests if the shortcut points to a local resource.
      private static boolean isMagicPresent​(byte[] link)  
      static boolean isPotentialValidLink​(java.io.File file)
      Provides a quick test to see if this could be a valid link ! If you try to instantiate a new WindowShortcut and the link is not valid, Exceptions may be thrown and Exceptions are extremely slow to generate, therefore any code needing to loop through several files should first check this.
      private void parseLink​(byte[] link)
      Gobbles up link data by parsing it and storing info in member fields
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • WindowsShortcut

        public WindowsShortcut​(java.io.File file)
                        throws java.io.IOException,
                               java.text.ParseException
        Constructs a new WindowsShortcut
        Parameters:
        file - file
        Throws:
        java.io.IOException - if an I/O error occurs
        java.text.ParseException - if a parsing error occurs
    • Method Detail

      • isPotentialValidLink

        public static boolean isPotentialValidLink​(java.io.File file)
                                            throws java.io.IOException
        Provides a quick test to see if this could be a valid link ! If you try to instantiate a new WindowShortcut and the link is not valid, Exceptions may be thrown and Exceptions are extremely slow to generate, therefore any code needing to loop through several files should first check this.
        Parameters:
        file - the potential link
        Returns:
        true if may be a link, false otherwise
        Throws:
        java.io.IOException - if an IOException is thrown while reading from the file
      • getRealFilename

        public java.lang.String getRealFilename()
        Returns the name of the filesystem object pointed to by this shortcut.
        Returns:
        the name of the filesystem object pointed to by this shortcut
      • isLocal

        public boolean isLocal()
        Tests if the shortcut points to a local resource.
        Returns:
        true if the 'local' bit is set in this shortcut, false otherwise
      • isDirectory

        public boolean isDirectory()
        Tests if the shortcut points to a directory.
        Returns:
        true if the 'directory' bit is set in this shortcut, false otherwise
      • getBytes

        private static byte[] getBytes​(java.io.InputStream in)
                                throws java.io.IOException
        Gets all the bytes from an InputStream
        Parameters:
        in - the InputStream from which to read bytes
        Returns:
        array of all the bytes contained in 'in'
        Throws:
        java.io.IOException - if an IOException is encountered while reading the data from the InputStream
      • getBytes

        private static byte[] getBytes​(java.io.InputStream in,
                                       java.lang.Integer max)
                                throws java.io.IOException
        Gets up to max bytes from an InputStream
        Parameters:
        in - the InputStream from which to read bytes
        max - maximum number of bytes to read
        Returns:
        array of all the bytes contained in 'in'
        Throws:
        java.io.IOException - if an IOException is encountered while reading the data from the InputStream
      • isMagicPresent

        private static boolean isMagicPresent​(byte[] link)
      • parseLink

        private void parseLink​(byte[] link)
                        throws java.text.ParseException
        Gobbles up link data by parsing it and storing info in member fields
        Parameters:
        link - all the bytes from the .lnk file
        Throws:
        java.text.ParseException - if a parsing error occurs
      • bytesToWord

        private static int bytesToWord​(byte[] bytes,
                                       int off)
      • bytesToDword

        private static int bytesToDword​(byte[] bytes,
                                        int off)