FULL PRODUCT VERSION :
jdk 6.0 21
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
use some big (few mb) file. (example csv text file)
lock the file
read from it using BufferedReader.readLine()
while java is reading overwrite the file using windows explorer
windows will bring an error that the file is locked
java will get an OutOfMemoryException in BufferedReader.readLine()
file in the target folder is corrupt. file in the source folder is ok.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
as stated above, start the program (see source below)
while the program is running overwrite the locked file with a file from another folder.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
OS error message that file is in use
ACTUAL -
OutOfMemoryError (heap)
file corrupt in target folder
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2882)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:515)
at java.lang.StringBuffer.append(StringBuffer.java:306)
at java.io.BufferedReader.readLine(BufferedReader.java:345)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at test.FileTest.main(FileTest.java:41)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.util.List;
import org.supercsv.io.CsvListReader;
import org.supercsv.io.ICsvListReader;
import org.supercsv.prefs.CsvPreference;
public class FileTest
{
public static void main(String[] args) throws Exception
{
BufferedReader inFile = null;
String fileName = "test.csv";
FileChannel channel = new RandomAccessFile(new File(fileName), "rw").getChannel();
FileLock lock = channel.lock();
try
{
inFile = new BufferedReader(Channels.newReader((ReadableByteChannel)channel, Charset.defaultCharset().name()));
}
catch (Exception e)
{
e.printStackTrace();
}
String list;
int k = 0;
try
{
while ((list = inFile.readLine()) != null)
{
Thread.sleep(10);
if (k++ % 1000 == 0)
System.out.println(k);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
lock.release();
channel.close();
}
}
---------- END SOURCE ----------
jdk 6.0 21
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
use some big (few mb) file. (example csv text file)
lock the file
read from it using BufferedReader.readLine()
while java is reading overwrite the file using windows explorer
windows will bring an error that the file is locked
java will get an OutOfMemoryException in BufferedReader.readLine()
file in the target folder is corrupt. file in the source folder is ok.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
as stated above, start the program (see source below)
while the program is running overwrite the locked file with a file from another folder.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
OS error message that file is in use
ACTUAL -
OutOfMemoryError (heap)
file corrupt in target folder
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2882)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:515)
at java.lang.StringBuffer.append(StringBuffer.java:306)
at java.io.BufferedReader.readLine(BufferedReader.java:345)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at test.FileTest.main(FileTest.java:41)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.util.List;
import org.supercsv.io.CsvListReader;
import org.supercsv.io.ICsvListReader;
import org.supercsv.prefs.CsvPreference;
public class FileTest
{
public static void main(String[] args) throws Exception
{
BufferedReader inFile = null;
String fileName = "test.csv";
FileChannel channel = new RandomAccessFile(new File(fileName), "rw").getChannel();
FileLock lock = channel.lock();
try
{
inFile = new BufferedReader(Channels.newReader((ReadableByteChannel)channel, Charset.defaultCharset().name()));
}
catch (Exception e)
{
e.printStackTrace();
}
String list;
int k = 0;
try
{
while ((list = inFile.readLine()) != null)
{
Thread.sleep(10);
if (k++ % 1000 == 0)
System.out.println(k);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
lock.release();
channel.close();
}
}
---------- END SOURCE ----------