import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class ThreadCommitTest {
    
public static void main(String[] args) throws Exception {
        (
new ReadWriteThread(true)).start(); 
        (
new ReadWriteThread(false)).start(); 
    }

    
    
static class ReadWriteThread extends Thread {
        
private static String inFileName_ = "f:\\Fetion2006Setup.zip";
        
private static String outFileName_ = "d:\\Fetion2006Setup.zip";
        
private static Boolean lock_ = new Boolean(false);
        
public static int LEN_PERCENT = 1000000;
        
private boolean isRead_;
        
private FileOutputStream writer_;
        
private FileInputStream reader;
        
private int nowlen;
        
private long len;
        
private static byte[] value_;
        
private static boolean isOver_ = false;
        
public ReadWriteThread(boolean isRead) throws Exception {
            isRead_ 
= isRead;
            
if (isRead) {
                reader 
= new FileInputStream(inFileName_);
                nowlen 
= 0;
                len 
= (new File(inFileName_)).length();
            }
 else {
                writer_ 
= new FileOutputStream(outFileName_);
            }

        }

        
public void run() {
            
while(true{
                
if (isOver_)
                    
break;
                
try {
                    
synchronized (lock_) {
                        
if (isRead_ && value_ == null{
                            
int charLen = LEN_PERCENT;
                            
if (nowlen + LEN_PERCENT > len) {
                                charLen 
= (int)(len - nowlen);
                            }

                            nowlen 
+= charLen;
                            
if (charLen <= 0{
                                isOver_ 
= true;
                                reader.close();
                            }

                            
if (!isOver_) {
                                value_ 
= new byte[charLen];
                                reader.read(value_);
                            }

                        }
 else if (!isRead_ && value_ != null{
                            
if (isOver_) {
                                writer_.flush();
                                writer_.close();
                                
break;
                            }
 else {
                                writer_.write(value_);
                                writer_.flush();
                                value_ 
= null;
                            }

                        }

                        lock_.notifyAll();
                    }

                }
 catch (Exception e) {
                    System.out.println(
"-------" + e + "-------");
                }

            }

            System.out.println((isRead_ 
? "Reader" : "Writer"+ " Over");
        }

    }

}