?
Solved

FileChannel Problem specifically with ByteBuffer and FileChannel.MapMode

Posted on 2006-07-10
6
Medium Priority
?
709 Views
Last Modified: 2012-06-21
Guys

Has anyone seen this?

I am trying some file manipulation...  concantenating file (using my concatenate method)

then after saving them I want to get rid of the files.  Almost of all of them are deleted EXCEPT the file I modifyied using

concatenate (not deleted using my cleanupFiles method)

I was trying to use

//ByteBuffer buffer = tmpChannel.map(FileChannel.MapMode.READ_ONLY, channel.position(), tmpChannel.size());

but had to change it to use a regular fileinputstream again... some reason it has the modified files under some sort of lock..  try all kinds of ways to make sure they were closed,,, to no avail




private void concatenate(List fileList, boolean lastItemFirst)
      throws Exception
      {
            Object[] fileArray = fileList.toArray();
            
            
            String fileName;
            FileChannel channel = null;
            FileOutputStream fs = null;
            File concatenatedFile = null;
            FileInputStream temps = null;
            int max = fileArray.length - 1;
            
            
            if (lastItemFirst)
            {
                  
                  for (int items = max; items >=0; items--)
                  {
                        
                        fileName = (String)fileArray[items];
                        
                        //last segment of a Data
                        if (items == max)
                        {
                              
                              concatenatedFile = new File(fileName);
                        try
                        {
                              fs = new FileOutputStream(fileName, true);
                        }
                        finally
                    {
                          try
                          {
                                if (fs != null) fs.close();
                          
                          }
                          catch(IOException e)
                          {
                                e.printStackTrace();
                          }
                    }
                        
                        
                              
                              
                              channel = fs.getChannel();
                              
                        }
                        else
                        {
                              File f = new File(fileName);
                              try
                              {
                                    temps = new FileInputStream(fileName);
                              }
                              finally
                          {
                                try
                                {
                                      if (temps != null) temps.close();
                                
                                }
                                catch(IOException e)
                                {
                                      e.printStackTrace();
                                }
                          }
                              
                              byte[] buffer = new byte[(int)f.length()];
                              temps.read(buffer);
                              FileChannel tmpChannel =temps.getChannel();
      /*** PART I had to comment out  */                        
                              //ByteBuffer buffer = tmpChannel.map(FileChannel.MapMode.READ_ONLY, channel.position(), tmpChannel.size());
                             //ByteBuffer buffer = tmpChannel.map(FileChannel.MapMode.READ_ONLY, channel.position(), tmpChannel.size()).asReadOnlyBuffer();
                              
                              //channel.write(buffer);
                             fs.write(buffer);
                              tmpChannel.close();
                              temps.close();
                        }
                  }
            
            }            
            
            fs.flush();
            channel.force(true);
            fs.close();
            concatenatedFile.delete();
            
      }
      
      
      public final void cleanupFiles(Collection msgList)
      throws Exception
      {
            
            Iterator it = msgList.iterator();
            
            boolean answer=false;
            boolean file_exists=false;
            while (it.hasNext())
            {
                  Data data = (Data)it.next();
                  
                  
                  Iterator fileIterator = data.getId().iterator();
                  
                  
                  while (fileIterator.hasNext())
                  {
                        String fileName = (String)fileIterator.next();
                        
                        
                        File file = new File(fileName);
                        file_exists=file.exists();
                        if (file_exists)
                        {
                              answer=file.delete();
                        }
                  }
            }
      }


Thanks
ryno71
0
Comment
Question by:ryno71
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17076642
You'd probably find it a lot easier to use this:

http://java.sun.com/j2se/1.5.0/docs/api/java/io/SequenceInputStream.html
0
 
LVL 92

Expert Comment

by:objects
ID: 17077891
you're closing the stream before you do the concatenation :)
should be something like:

             Iterator i = fileList.iterator();
             FileChannel out = new FileOutputStream(((File)i.next()).getChannel();
             long pos = 0;
             while (i.hasNext())
                 source = args[i];
                 FileChannel in = new FileInputStream(((File)i.next()).getChannel();
                 long size = in.size();
                 MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, pos, size);
                 out.write(buf);
                 pos += size;
                 in.close();
              }
0
 

Author Comment

by:ryno71
ID: 17081011
Actually I am seeing this issue below where I cant delete the file I've modified via the FileChannel.map

Cannot delete file if memory mapped with FileChannel.map (windows)

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4715154

dont think its ever been fixed
0
 
LVL 92

Expert Comment

by:objects
ID: 17086787
it'll get closed eventually, once the gc'er is done with it.
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17185203
Closed, 250 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question