CharArrayReader Class in Java

digitization, transformation, binary

CharArrayReader Class in Java

CharArrayReader class is a character input stream that uses a character array as the source of the characters it returns. we create a CharArrayReader by specifying the character array, or portion of an array, that it is to read from.

The CharArrayReader class represents a stream whose data comes from a character array. This class is similar to ByteArrayInputStream, but it deals with a Java character stream rather than a byte stream.

Ads code goes here

Furthermore, this class supports marking a position in the stream, which ByteArrayInputStream does not. The position of the end of the stream depends on the constructor used.

If the CharArrayReader(char[] buf) constructor is used, the end of the stream is the end of the character array. If the CharArrayReader(char[] buf, int offset, int length) constructor is used, the end of the stream is reached at the index given by offset+length.

The character array we pass to the CharArrayReader is not copied by this class. This means that changes we make to the elements of the array after we create the input stream do affect the values read from the array.

CharArrayReader defines the normal Reader methods, and supports the mark () and reset () methods.

The class CharArrayReader is defined as

public class java.io.CharArrayReader extends java.io.Reader{
// Public Constructors
public CharArrayReader (char [ ] buf);
public CharArrayReader (char [ ] buf, int offset, int length);
// Protected Instance Variables
protected charl ] buf:
protected int count;
protected int markedPos;
protected int pos;
// Public Instance Methods
public void close ();// Defines Reader
public void mark (int readAheadLimit) throws IOException;// Overrides Reader
public boolean markSupported ();// Overrides Reader
public int read () throws IOException;// Overrides Reader
public int read (char [ ] b, int off, int len) throws IOException;//Defines Reader
public boolean ready () throws IOException;// Overrides Reader
public void reset () throws IOException;// Overrides Reader
public long skip (long n) throws IOException;// Overrides Reader
}

The details of the class structure are given as follows:

protected char[] buf;

protected char[] buf is the buffer represented by this reader.

protected int count;

protected int count is the size of the buffer, or in other words, the length of the array.

protected int markedPos;

protected int markedPos is the buffer position when mark() was called. If mark() has not been called, this variable is 0.

protected int pos;

protected int pos is the current position in the buffer.

public CharArrayReader(char[] buf);

public CharArrayReader(char[] buf) constructor creates a CharArrayReader object that uses the given array of characters as its data source. The data is not copied, so changes made to the array affect the data that the CharArrayReader returns.

READ  Class System in Java

Parameter
buf – The reader source.

public CharArrayReader(char[] buf, int offset, int length);

public CharArrayReader(char[] buf, int offset, int length) constructor creates a CharArrayReader that uses, as its data source, length characters in a given array of bytes, starting at offset characters from the beginning of the array. The data is not copied, so changes made to the array affect the data that the CharArrayReader returns.

Parameter
buf – The reader source.
offset – An offset into the array.
length – The number of bytes to read.

public void close();

public void close() method closes the reader by removing the link between this CharArrayReader and the array it was created with.

public void mark(int readAheadLimit);

public void mark(int readAheadLimit) method causes the CharArrayReader to remember its current position. A subsequent call to reset() causes the object to return to that saved position, and thus reread a portion of the buffer. Because the data for this stream comes from a char array, there is no limit on reading ahead, so readAheadLimit is ignored.

Parameter
readAheadLimit – The maximum number of characters that can be read before the saved position becomes invalid.

public boolean markSupported();

public boolean markSupported() method returns true to indicate that this class supports mark() and reset().

public int read();

public int read() method returns the next character in the array.
This method returns the next character or -1 if the end of the stream is encountered.

public int read(char[] b, int off, int len);

public int read(char[] b, int off, int len) method copies up to len characters from its internal array into the given array b, starting at index off.

READ  Class Modifier in Java

This method returns the actual number of characters read or -1 if the end of the stream is encountered immediately.

Parameter
b – An array of characters to be filled from the stream.
off – An offset into the character array.
len – The number of characters to read.

public boolean ready();

public boolean ready() method returns true, If there is any data left to be read from the character array.
This method returns a boolean value that indicates whether the stream is ready to be read.

public void reset();

public void reset() method resets the position of the CharArrayReader to the position that was saved by calling the mark() method. If mark() has not been called, the CharArrayReader is reset to read from the beginning of the array.

public long skip(long n);

public long skip(long n) method skips n characters of input. If you try to skip past the end of the array, the stream is positioned at the end of the array.

This method returns the actual number of characters skipped.
Parameter
n-The number of characters to skip.
Apart from these CharArrayReader class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *