Java - CharArrayReader markSupported() method



Description

The Java CharArrayReader markSupported() method is used to check whether the stream supports the mark(int readAheadLimit) and reset() methods. In the case of CharArrayReader, the markSupported() method always returns true, as it inherently supports marking and resetting due to its in-memory operation.

Declaration

Following is the declaration for java.io.CharArrayReader.markSupported() method −

public boolean markSupported()

Parameters

NA

Return Value

The method returns true if the stream supports mark() invocation.

Exception

NA

Example - Using CharArrayReader markSupported() method

The following example shows the usage of Java CharArrayReader markSupported() method.

CharArrayReaderDemo.java

package com.tutorialspoint;

import java.io.CharArrayReader;
import java.io.IOException;

public class CharArrayReaderDemo {
   public static void main(String[] args) {      
      CharArrayReader car = null;
      char[] ch = {'A', 'B', 'C', 'D', 'E'};

      try {
         // create new character array reader
         car = new CharArrayReader(ch);
         
         // verifies if the stream support mark() method
         boolean bool = car.markSupported();
         System.out.println("Is mark supported : "+bool);
         System.out.println("Proof:");
         
         // read and print the characters from the stream
         System.out.println(car.read());
         System.out.println(car.read());
         
         // mark() is invoked at this position
         car.mark(0);
         System.out.println("Mark() is invoked");
         System.out.println(car.read());
         System.out.println(car.read());
         
         // reset() is invoked at this position
         car.reset();
         System.out.println("Reset() is invoked");
         System.out.println(car.read());
         System.out.println(car.read());
         System.out.println(car.read());
         
      } catch(IOException e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(car!=null)
            car.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Is mark supported : true
Proof:
65
66
Mark() is invoked
67
68
Reset() is invoked
67
68
69

Example - Simple Check for markSupported()

The following example shows the usage of Java CharArrayReader mark(readAheadLimit) method.

CharArrayReaderDemo.java

package com.tutorialspoint;

import java.io.CharArrayReader;

public class CharArrayReaderDemo {
   public static void main(String[] args) {
      char[] data = "Hello, World!".toCharArray();

      // Create a CharArrayReader
      CharArrayReader reader = new CharArrayReader(data);

      // Check if mark is supported
      if (reader.markSupported()) {
         System.out.println("Mark and reset are supported by CharArrayReader.");
      } else {
         System.out.println("Mark and reset are NOT supported by CharArrayReader.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Mark and reset are supported by CharArrayReader.

Explanation

  • A CharArrayReader is created using a character array containing "Hello, World!".

  • The markSupported() method is called to check if the mark() and reset() methods are supported.

  • Since CharArrayReader always supports marking, the output will confirm that "Mark and reset are supported by CharArrayReader."

Example - Using markSupported() with Mark and Reset

The following example shows the usage of Java CharArrayReader markSupported() method.

CharArrayReaderDemo.java

package com.tutorialspoint;

import java.io.CharArrayReader;
import java.io.IOException;

public class CharArrayReaderDemo {
   public static void main(String[] args) {
      char[] data = "Java Programming".toCharArray();

      // Create a CharArrayReader
      CharArrayReader reader = new CharArrayReader(data);

      // Check if mark and reset are supported
      if (reader.markSupported()) {
         System.out.println("Mark and reset are supported by CharArrayReader.");

         try {
            // Mark the current position
            reader.mark(0); // Mark the position before reading
            System.out.println("Stream marked at the current position.");

            // Read and print the first 5 characters
            System.out.print("First 5 characters: ");
            for (int i = 0; i < 5; i++) {
               System.out.print((char) reader.read());
            }
            System.out.println();

            // Reset the stream to the marked position
            reader.reset();
            System.out.println("Stream reset to the marked position.");

            // Read and print the first 5 characters again
            System.out.print("First 5 characters after reset: ");
            for (int i = 0; i < 5; i++) {
               System.out.print((char) reader.read());
            }
            System.out.println();

         } catch (IOException e) {
            System.err.println("IOException occurred: " + e.getMessage());
         }
      } else {
         System.out.println("Mark and reset are NOT supported by CharArrayReader.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Mark and reset are supported by CharArrayReader.
Stream marked at the current position.
First 5 characters: Java 
Stream reset to the marked position.
First 5 characters after reset: Java

Explanation

  • A CharArrayReader is initialized with "Java Programming".

  • The markSupported() method checks if marking and resetting are supported, which it always is for CharArrayReader.

  • The stream is marked at the beginning, and the first 5 characters (Java ) are read.

  • The reset() method is called to return the stream to the marked position.

  • The first 5 characters are read again after the reset, demonstrating the use of mark() and reset().

Key Points

  • Purpose of markSupported()

    • This method checks if the stream supports the mark() and reset() operations. For CharArrayReader, it always returns true.

  • Practical Use

    • Before calling mark() or reset(), it is a good practice to check markSupported() to avoid unexpected behavior, especially when working with other types of readers or input streams that may not support marking.

  • Behavior in CharArrayReader

    • Since CharArrayReader operates on an in-memory character array, it inherently supports marking and resetting, and markSupported() will always return true.

  • Reliability

    • The second example demonstrates a practical scenario where mark() and reset() are used only after confirming support via markSupported().

java_io_chararrayreader.htm