/*
 * 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 permafrost.tundra.lang.BytesHelper;
import java.io.File;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * A filter input stream which supports the mark and reset methods.
 */
public class MarkableInputStream extends FilterInputStream {
    /**
     * Creates a new MarkableInputStream, which wraps the given input stream object in a stream that supports the mark
     * and reset methods.
     *
     * To provide support for mark and reset methods to a given input stream, this class creates a temporary backing
     * file, then copies the entire contents of the given stream to the file, and then uses the file for reading the
     * MarkableInputStream.
     *
     * @param  inputStream The stream to be wrapped.
     * @throws IOException If an I/O error occurs while reading from the stream.
     */
    public MarkableInputStream(InputStream inputStream) throws IOException {
        super(inputStream);

        File backingFile = FileHelper.create();
        FileHelper.writeFromStream(backingFile, inputStream, false);

        in = new DeleteOnCloseFileInputStream(backingFile);

        // if the size of the data is small, read it fully into memory
        if (backingFile.length() <= InputOutputHelper.DEFAULT_BUFFER_SIZE) {
            in = InputStreamHelper.normalize(BytesHelper.normalize(in));
        }
    }

    /**
     * Returns true because this class supports marking and resetting the stream.
     *
     * @return True because this class supports marking and resetting the stream.
     */
    @Override
    public boolean markSupported() {
        return true;
    }

    /**
     * Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at
     * the last marked position so that subsequent reads re-read the same bytes.
     *
     * @param readLimit This parameter is ignored.
     */
    @Override
    public synchronized void mark(int readLimit) {
        in.mark(readLimit);
    }

    /**
     * Repositions this stream to the position at the time the mark method was last called on this input stream, or to
     * the start of the stream if the mark method has never been called.
     *
     * @throws IOException If an I/O error occurs.
     */
    @Override
    public synchronized void reset() throws IOException {
        in.reset();
    }
}

@@ -1702,16 +1702,79 @@
  stream,
+ if the given input stream does not support%0A     * marking then
  this cl
@@ -1808,21 +1808,13 @@
 king
-%0A     *
  file
-,
  the
@@ -1860,24 +1860,31 @@
 en stream to
+%0A     *
  the file, a
@@ -1880,17 +1880,16 @@
 the file
-,
  and the
@@ -1919,23 +1919,16 @@
 ding the
-%0A     *
  Markabl
@@ -2192,24 +2192,72 @@
 utStream);%0A%0A
+        if (!inputStream.markSupported()) %7B%0A    
         File
@@ -2296,24 +2296,28 @@
 ();%0A        
+   
+ 
 FileHelper.w
@@ -2370,24 +2370,28 @@
 );%0A%0A        
+    
 in = new Del
@@ -2439,16 +2439,20 @@
         
+   
+ 
 // if th
@@ -2506,16 +2506,20 @@
  memory%0A
+    
         
@@ -2595,24 +2595,28 @@
             
+   
+ 
 in = InputSt
@@ -2676,1016 +2676,23 @@
     
-%7D%0A
     %7D%0A
-%0A 
    
-/**%0A
      
-* Returns true because this class supports marking and resetting the stream.%0A     *%0A     * @return True because this class supports marking and resetting the stream.%0A     */%0A    @Override%0A    public boolean markSupported() %7B%0A        return true;%0A    %7D%0A%0A    /**%0A     * Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at%0A     * the last marked position so that subsequent reads re-read the same bytes.%0A     *%0A     * @param readLimit This parameter is ignored.%0A     */%0A    @Override%0A    public synchronized void mark(int readLimit) %7B%0A        in.mark(readLimit);%0A    %7D%0A%0A    /**%0A     * Repositions this stream to the position at the time the mark method was last called on this input stream, or to%0A     * the start of the stream if the mark method has never been called.%0A     *%0A     * @throws IOException If an I/O error occurs.%0A     */%0A    @Override%0A    public synchronized void reset() throws IOException %7B%0A        in.reset();
+%7D
 %0A   

Change MarkableInputStream to only use a backing file if the wrapped stream does not already support marking