Java FilterInputStream mark() Example

FilterInputStream class mark() method example. This example shows you how to use mark() method.


Syntax is : public void mark(int readlimit)
this method 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. The readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

Here is the code.
/**
 * @(#) MarkFilterInputStream.java
 * A class representing use of method mark() of FilterInputStream 
 class in java.io Package.
 * @Version  09-June-2008
 @author   Rose India Team
 */
import java.io.*;

public class MarkFilterInputStream extends FilterInputStream {

    MarkFilterInputStream(FileInputStream objFin){
    super(objFin);
    }

  public static void main(String[] argsthrows IOException{

        byte[] byteArrayOut = new byte[]{'M','A','H','E','N','D','R','A'};
     
      /* Create object of ByteArrayOutputStream class to write specified 
      byte array to the output byte stream. */
      ByteArrayOutputStream objByteStream = new ByteArrayOutputStream();
    objByteStream.write(byteArrayOut,0,8);
    
      // Create object of ByteArrayInputStream class.
      InputStream objInStream = new ByteArrayInputStream(byteArrayOut);
  
    //Create object of MarkSupportedFilterInputStream class.
    MarkSupportedFilterInputStream obj =new MarkSupportedFilterInputStream
                                          (objInStream);

         // Read from the input stream buffer.
         System.out.println("\nFirst element is  : "+(char)obj.read());
     System.out.println("Next element is : "+(char)obj.read());
    
         // Set the mark position for second element.
         obj.mark(1);
       System.out.println("Element at second position has marked.")
         System.out.println("Next element is  : "+(char)obj.read());
     System.out.println("Next element is  : "+(char)obj.read());

         // Reset the BufferedInputStream.
         obj.reset();
         System.out.println("reset() method call so next element read will be "
     "next element to the marked element.");

         // read character after the reset the buffer.
         char nextLetter = (char)obj.read();
         System.out.println("The next element read after reset is: "+nextLetter);
    }
}

Output of the program.
 First element is  : M
Next element is : A
Element at second position has marked.
Next element is  : H
Next element is  : E
reset() method call so next element read will be next element to the marked element.
The next element read after reset is: H

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US