/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package permafrost.tundra.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Automatically deletes the underlying file when the close method is called on the input stream.
 */
public class DeleteOnCloseFileInputStream extends MarkableFileInputStream {
    /**
     * Whether the stream has been closed.
     */
    protected boolean isClosed = false;

    /**
     * Reference to the file to be read and deleted on close.
     */
    private File file = null;

    /**
     * Constructs a new AutoDeleteFileInputStream by opening a connection to an actual file, the file named by the File
     * object file in the file system.
     *
     * @param file The file to be opened for reading.
     * @throws FileNotFoundException If the file does not exist, is a directory rather than a regular file, or for some
     *                               other reason cannot be opened for reading.
     */
    public DeleteOnCloseFileInputStream(File file) throws FileNotFoundException {
        super(file);
        this.file = file;
    }

    /**
     * Constructs a new AutoDeleteFileInputStream by opening a connection to an actual file, the file named by the path
     * name in the file system.
     *
     * @param name The file to be opened for reading.
     * @throws FileNotFoundException If the file does not exist, is a directory rather than a regular file, or for some
     *                               other reason cannot be opened for reading.
     */
    public DeleteOnCloseFileInputStream(String name) throws FileNotFoundException {
        super(name);
        if (name != null) file = new File(name);
    }

    /**
     * Closes this file input stream and releases any system resources associated with the stream, then deletes the
     * file.
     *
     * @throws IOException If the file cannot be deleted, or if an I/O error occurs.
     */
    @Override
    public void close() throws IOException {
        super.close();

        synchronized (this) {
            if (!isClosed) {
                isClosed = true;
                // automatically delete the file after closing the stream
                if (file != null && !file.delete()) {
                    throw new IOException("File could not be deleted: " + FileHelper.normalize(file));
                }
            }
        }
    }

    /**
     * Returns the file this input stream reads from.
     *
     * @return The file this input stream reads from.
     */
    public File getFile() {
        return file;
    }
}
@@ -1512,15 +1512,22 @@
   pr
-otected
+ivate volatile
  boo
@@ -1549,17 +1549,16 @@
  false;%0A
-%0A
     /**%0A
@@ -1593,19 +1593,36 @@
  to 
-be 
 read 
-and
+from/write to, and then
  del
@@ -1656,16 +1656,22 @@
 private 
+final 
 File fil
@@ -1675,15 +1675,8 @@
 file
- = null
 ;%0A%0A 
@@ -1801,20 +1801,20 @@
 the 
-File
+path
 %0A     * 
 obje
@@ -1813,18 +1813,11 @@
   * 
-object fil
+nam
 e in
@@ -1855,20 +1855,38 @@
  @param 
-file
+name                  
  The fil
@@ -2161,24 +2161,26 @@
 tStream(
-File fil
+String nam
 e) throw
@@ -2217,45 +2217,50 @@
     
-super(file);%0A        this.file = file
+this(name == null ? null : new File(name))
 ;%0A  
@@ -2383,36 +2383,36 @@
 amed by the 
-path
+File
 %0A     * 
 name in the 
@@ -2391,35 +2391,42 @@
 the File%0A     * 
-nam
+object fil
 e in the file sy
@@ -2452,20 +2452,38 @@
  @param 
-name
+file                  
  The fil
@@ -2754,34 +2754,32 @@
 InputStream(
-String nam
+File fil
 e) throws Fi
@@ -2814,19 +2814,19 @@
   super(
-nam
+fil
 e);%0A    
@@ -2833,47 +2833,24 @@
     
-if (name != null) file = new File(name)
+this.file = file
 ;%0A  
@@ -3173,25 +3173,66 @@
 lose();%0A
-%0A
+        if (!isClosed && file != null) %7B%0A 
         
 synchron
@@ -3223,16 +3223,19 @@
         
+   
 synchron
@@ -3242,14 +3242,13 @@
 ized
- (this
+(file
 ) %7B%0A
@@ -3251,32 +3251,36 @@
 ) %7B%0A            
+   
+ 
 if (!isClosed) %7B
@@ -3276,24 +3276,28 @@
 isClosed) %7B%0A
+    
             
@@ -3317,16 +3317,20 @@
 = true;%0A
+    
         
@@ -3415,28 +3415,16 @@
     
+    
 if (
-file != null && 
 !fil
@@ -3457,16 +3457,20 @@
         
+    
 throw ne
@@ -3540,24 +3540,46 @@
 ize(file));%0A
+                    %7D%0A
             
@@ -3789,20 +3789,40 @@
  return 
-file
+new File(file.getPath())
 ;%0A    %7D%0A

Fix DeleteOnCloseFileInputStream to be thread-safe