Results 1 to 7 of 7
  1. #1
    CapsLoc is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Reading mapped file MacOSX vs. Windows

    Hello,

    I have a program (which isn't written by me) which maps a textfile into the adress space of a proces and then reads from it and puts its content on the screen.

    Thing is, when I run this on Windows (using eclipse) it'll give me the content of the textfile. But when I run it on MacOSX (leopard) (using either terminal or eclipse) it'll even show other information as well (eg. the font used), probably the metadata or so.

    Could anyone explain to me why running the same code generates different outputs? I know java is dependent on the operating system, but why does it give me the metadata in MacOSX?

    here's the code:
    Java Code:
    import java.io.*;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.channels.FileLock;
    import java.nio.BufferUnderflowException;
    
    
    public class ReadMappedFile 
    {
        public static void main(String[] args) throws Exception
        {
            File file = new File("/Users/Loc/Desktop/ae.txt");
    
            FileInputStream in = null;
            FileChannel fc = null;
            MappedByteBuffer buffer;
            FileLock lock = null;
            int position = 0;
    
            for(int i = 0; i < 4; i++)
            {
                try 
                {
                    in = new FileInputStream(file);
                    fc = in.getChannel();
                    buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size());
    
                    while(true)
                    {
                        Thread.sleep(100);
                        position = buffer.position();
                        lock = fc.tryLock(position, 1, true);  // try to get shared lock on single byte 
                        if(lock == null)                       // if lock was not free
                        {
                            System.out.println("\n\nlocking conflict for byte " + position + "\n"); 
                            lock = fc.lock(position, 1, true); // block until byte can be locked
                        }
                        System.out.print((char) buffer.get());
                        lock.release();
                    }
                } 
                catch (FileNotFoundException fnfEx) 
                {
                    System.err.println(fnfEx);
                } 
                catch (BufferUnderflowException buEx) 
                {
                    System.out.println("\nEnd of file.");
                } 
                catch (IOException ioEx) 
                {
                    System.err.println(ioEx);
                } 
                finally
                {
                    if (fc != null) fc.close();
                    if (in != null) in.close();
                    System.out.println("\nAnd starting all over again ...");
                }   
            }
        }
    }
    Thanks in advance

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Perhaps its not really a .txt file and is actually an .rtf or the like? If it really is flat text, then that is all you will see. What happens if you open the file in a hex editor - does it contain more than simply text information?

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    I suspect you're somehow getting part of the resource fork, but I don't have time to investigate in detail.

    -Gary-

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    OS X does not use resource forks - That would be unlikely unless its a very old file from OS 9 copied into X.

    Edit:
    Well, it technically supports them, but uses a package structure rather than embedding them in the file system, and this entire concept is almost never used anymore.
    Last edited by quad64bit; 12-12-2010 at 01:37 AM.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by quad64bit View Post
    OS X does not use resource forks - That would be unlikely unless its a very old file from OS 9 copied into X.

    Edit:
    Well, it technically supports them, but uses a package structure rather than embedding them in the file system, and this entire concept is almost never used anymore.
    You're right, of course, and the OSX TextEdit utility saves in RTF format by default, and really doesn't seem to save in plain text at all when creating a new file (although it will happily edit and save an existing .txt file).

    -Gary-

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Yeah I suspect it's something of this nature. FYI, TextEdit can use .txt by default if you set it in the prefs and toggling back and forth can be done with a key command on the fly as well!

  7. #7
    CapsLoc is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Problem solved

    Hello,

    Thanks for the replies.
    Problem was indeed that it was actually a .rtf file, silly of me to think that simply changing the extension would be sufficient..

    Anyway I made a .txt file on windows and used it with that, and it did what I hoped it would do, reading just the text itself.

    Thanks again!

Similar Threads

  1. Increasing heap/stack size (MacOSX)
    By CapsLoc in forum New To Java
    Replies: 3
    Last Post: 12-13-2010, 11:52 AM
  2. How to access driver in Windows like a file?
    By pthoonen in forum New To Java
    Replies: 6
    Last Post: 01-15-2009, 04:28 AM
  3. Memory-Mapped File-(MappedByteBuffer )
    By lenah in forum Advanced Java
    Replies: 3
    Last Post: 12-22-2008, 08:00 PM
  4. Reading Client Windows Registry through browser
    By nareshnettem in forum Advanced Java
    Replies: 2
    Last Post: 05-14-2008, 08:29 AM

Posting Permissions

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