ryno71
asked on
FileChannel Problem specifically with ByteBuffer and FileChannel.MapMode
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.R EAD_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.R EAD_ONLY, channel.position(), tmpChannel.size());
//ByteBuffer buffer = tmpChannel.map(FileChannel .MapMode.R EAD_ONLY, channel.position(), tmpChannel.size()).asReadO nlyBuffer( );
//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
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
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,
}
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
//ByteBuffer buffer = tmpChannel.map(FileChannel
//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
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()).ge tChannel() ;
long pos = 0;
while (i.hasNext())
source = args[i];
FileChannel in = new FileInputStream(((File)i.n ext()).get Channel();
long size = in.size();
MappedByteBuffer buf = in.map(FileChannel.MapMode .READ_ONLY , pos, size);
out.write(buf);
pos += size;
in.close();
}
should be something like:
Iterator i = fileList.iterator();
FileChannel out = new FileOutputStream(((File)i.
long pos = 0;
while (i.hasNext())
source = args[i];
FileChannel in = new FileInputStream(((File)i.n
long size = in.size();
MappedByteBuffer buf = in.map(FileChannel.MapMode
out.write(buf);
pos += size;
in.close();
}
ASKER
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
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
it'll get closed eventually, once the gc'er is done with it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://java.sun.com/j2se/1.5.0/docs/api/java/io/SequenceInputStream.html