Alternate Dat Streams (ADS) enumeration in JAVA?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Alternate Dat Streams (ADS) enumeration in JAVA?

  1. #1
    Join Date
    Apr 2009
    Posts
    5

    Alternate Dat Streams (ADS) enumeration in JAVA?

    Hi guys, perhaps most of you will know of Alternate Data Streams. These are a feature of the NTFS file system that were introduced beginning with Windows NT 3.1 and which were added in order to support Macintosh's HFS which employs resource forks so that the system can maintain metadata about the file, for example icons.
    The fact is that ADS provide a way to create legitimate files on a Windows system that can contain data, scripts or executable code but that there is no native tools on Window systems (prior to Vista, which now has a native command to view ADS) that can detect the presence of ADS.

    There are a few ways ADS can be created at the command line, one is as follows:

    c:\ads>notepad myfile.txt:ads.txt

    Now what i was concerned with is if it is possible to program a tool to enumerate ADS associated with a specific file, or better still all files in a directory and the directory itself (as ADS can also be attached to directories)in JAVA. I know that there are tools out there that do this (LADS.exe, ADSSpy.exe) however these are programmed in other languages, i believe a google search also retrieves source code to do this in c and c++.

    ADS can be treated as files in JAVA (i have written to and read from ADS just to check) by simply giving their path ( sorry this may seem obvious).

    so does anyone know if this is possible in JAVA? or know of any source codes to do this/something similar in JAVA?

    Thanks guys
    Last edited by ronrothy; April 1st, 2009 at 06:11 AM.

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    Can you please explain what you mean by "enumerate ADS associated with a specific file?" Are you looking for a way to pull up a directory full of ADS files and go through them one by one searching for specific text? Or something else.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    Java doesn't have any OS or file-system specific facilities built in, so unless you can do it with the basic generic file facilities, your other options are to execute an external utility, or create a JNI (Java Native Interface) application to interface with a native code DLL.

    The cheapest, fastest, and most reliable components of a computer system are those that aren't there...
    G. Bell
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Apr 2009
    Posts
    5

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    dlorde thankyou for the advice, you see i am programming a tool in JAVA that performs various functions related to ADS, such as get the file size and preview the data in an ADS but i have been unable to find a way to actually search a directory for ADS and show the results (i now know why).
    I will not despair, there are some utilities that i know of which enumerate ADS in specified directories and ADS associated with specific files, one example is LADS.exe.

    So, how would i go about executing an external program from within a JAVA program? can anyone give me a code example of this in action?

    Thankyou guys for any help you might give, it is appreciated.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    Use ProcessBuilder. There are plenty of tutorials available online such as this one.

    There are a number of potential pitfalls involved with using Process objects, so I suggest you also read this article as well.

  6. #6
    Join Date
    Apr 2009
    Posts
    5

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    Cheers, i've been reading and looking at examples on google, and have found this seemingly simple code which i am trying to use as a test and modify to get working. Here it is...

    Code:
    import java.io.*; 
    import java.util.*;                            
    
    class main
    {
    	public static void main (String args[])
    	{
    		try
            {
              Runtime rt = Runtime.getRuntime();
              Process p = rt.exec("C:\\lads.exe");
              
              InputStream in = p.getInputStream();
              OutputStream out = p.getOutputStream();
              InputStream err = p.getErrorStream();
     
              //do whatever you want
              //some more code
     
             p.destroy() ;
            }
            catch(Exception exc)
            {
    	    
    	      System.out.println("Error");
    	
            }
              
        }
    }
    The code must be working to some extent, as when i remove the external program i want to execute (Lads.exe) from the C:\ directory, the program prints out "error", and when the external program is actually in the directory c:\, then no error is displayed. And obviously the code compiles, any clue as to what i need to do to get this external file executing?

    i think maybe there is something quite simple i am missing here. Also when i look at the processes in task manager, i can't see "Lads.exe".

    I appreciate the help

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    Without knowing what lads.exe actually does it's hard to give concrete advice although I suspect part of your problem is calling destroy() immediately are creating the process. Have you tried waiting for the process to terminate (call waitFor()), or polling the inputstreams to get the output from lads.exe.

    If you need to pass parameters to the program you can do that through either the ProcessBuilder or possibly via the output stream.

  8. #8
    Join Date
    Apr 2009
    Posts
    5

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    Without knowing what lads.exe actually does it's hard to give concrete advice
    The program allows the user to list ADS, its a CLI tool.
    http://www.heysoft.de/nt/ep-lads.htm

    although I suspect part of your problem is calling destroy() immediately are creating the process.
    Ok, i commented out destroy(), but i still get the same reult and the process does not appear in task manager.

    Have you tried waiting for the process to terminate (call waitFor()), or polling the inputstreams to get the output from lads.exe.
    I'm not familiar with "call waitFor()", i'll read up on it and what do you mean by polling the input streams?

    Thank for the feedback, its hard to find a complete working example of this sort of thing, and i'm not in familiar JAVA territory here...

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    Ok, i commented out destroy(), but i still get the same reult and the process does not appear in task manager.
    Which isn't surprising because the Java program is just terminating. You need put some sort of block in place to stop main() from exiting - such as calling waitFor().

    and what do you mean by polling the input streams?
    You create a loop that repeatedly reads from the input stream (normally until the end of stream marker is found). The data that is read in is the data that is output by lads.exe. It is sometimes important to do this because on some OS's the streams aren't buffered (or the buffer maybe very small) and once the buffer is full the process may block until the stream is cleared.

  10. #10
    Join Date
    Apr 2009
    Posts
    5

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    Thanks Keang, when googling for info regarding waitFor(), i came across this code:

    Code:
    class MainClass 
    {
      public static void main(String args[]) 
      {
      Runtime r = Runtime.getRuntime();
      Process p = null;
      String cmd[] = { ""notepad", "/java/src/java/lang/Runtime.java""};
      try {
          p = r.exec(cmd);
          p.waitFor();
      } catch (Exception e) {
          System.out.println("error executing " + cmd[0]);
      }
      System.out.println(cmd[0] + " returned " + p.exitValue());
      }
    }
    Which actually executes notepad (the basics of what i wanted to do - execute an external program) and when i replace ""notepad", "/java/src/java/lang/Runtime.java"" with the path to another GUI based program the program again executes and appears on screen.
    However there does seem to be an issue with executing CLI tools, as if i put the path to such a program in the code like Lads.exe, at the console i get the output "c:\lads.exe returned 0".

    In order to search a directory for an ADS Lads.exe requires its path to be input at the command prompt followed by the path to the directory the user wishes to search, i would like to get this working with this code by adding these paths in but when i do this i still get the output "c:\lads.exe returned 0".

    Any of you guys know whats going on here? i am also researching myself.

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Alternate Dat Streams (ADS) enumeration in JAVA?

    Using Runtime.exec() is the old way of creating a process, try reading up on ProcessBuilder which is a little easier to use. I've already given you a link to a tutorial. You could also try reading the API docs for ProcessBuilder

    If your app needs arguments passing to it, as I've already said, you need to either add them to the ProcessBuilder before starting the process or input them via the output stream. The way you do it depends on the way lads.exe works ie does accept arguments on start up or does it prompt for input.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center