Class CharArrayWriter in Java

digitization, transformation, binary

Class CharArrayWriter in Java

The CharArrayWriter class represents a stream whose data is written to an internal character array. This class is similar to ByteArrayOutputStream, but it operates on an array of Java characters instead of a byte array.

The data from a CharArrayWriter can be sent to another Writer using the writeTo() method. A copy of the array can be obtained using the toCharArray() method.

Ads code goes here

The structure of the class CharArrayWriter is given by

public class java.io.CharArrayWriter extends java.io.Writer {
// Variables
protected char[] buf;
protected int count;
// Constructors
public CharArrayWriter();
public CharArrayWriter(int initialSize);
// Instance Methods
public void close();
public void flush();
public void reset();
public int size();
public char[] toCharArray();
public String toString();
public void write(int c);
public void write(char[] c, int off, int len);
public void write(String str, int off, int len);
public void writeTo(Writer out);
}

The details of the class structure are given as follows:

protected char[] buf;

protected char[] buf represents the buffer that holds data for this stream.

protected int count;

protected int count represents a placeholder that marks the end of the data in the buffer.

public CharArrayWriter();

public CharArrayWriter() constructor creates a CharArrayWriter with an internal buffer that has a default size of 32 characters. The buffer grows automatically as data is written to the stream.

public CharArrayWriter(int initialSize);

public CharArrayWriter(int initialSize) constructor creates a CharArrayWriter with an internal buffer that has a size of initialSize characters. The buffer grows automatically as data is written to the stream.

READ  Class Float in Java

Parameter
initialSize – The initial buffer size.

public void close();

public void close() method does nothing. For most subclasses of Writer, this method releases any system resources that are associated with the Writer object.

However, the CharArrayWriter’s internal array may be needed for subsequent calls to toCharArray() or writeTo().

For this reason, close() does nothing, and the internal array is not released until the CharArrayWriter is garbage collected.

public void flush();

public void flush() method does nothing. The CharArrayWriter writes data directly into its internal array; thus it is never necessary to flush the stream.

public void reset();

public void reset() method discards the current contents of the buffer and resets the position of the stream to zero. Subsequent data is written starting at the beginning of the array.

public int size();

public int size() method returns the number of characters currently stored in this object’s internal buffer. It is a count of the number of characters that have been written to the stream.
This method returns the number of characters currently stored in this object’s internal buffer. It is a count of the number of characters that have been written to the stream.

public char[] toCharArray();

public char[] toCharArray() method copies the data in the internal array and returns a reference to the copy. The returned array is as long as the data that has been written to the stream, i.e., the same as size().

This method returns a copy of the data that has been written to this CharArrayWriter in the form of a char array.

public String toString();

public String toString() method returns a reference to a String object created from the characters stored in this object’s internal buffer.

READ  Class Locale in Java

This method returns a copy of the data that has been written to this CharArrayWriter in the form of a String.

public void write(int c);

public void write(int c) method writes the low-order 16 bits of the given value into the internal array. If the array is full, a larger array is allocated.

Parameter
c – The value to write.

public void write(char[] c, int off, int len);

public void write(char[] c, int off, int len) method copies len characters to this object’s internal array from c, starting off elements from the beginning of the array. If the internal array is full, a larger array is allocated.

Parameter
c – An array of characters to write to the stream.
off – An offset into the character array.
len – The number of characters to write.

public void write(String str, int off, int len);

public void write(String str, int off, int len) method copies len characters to this object’s internal array from str, starting off characters from the beginning of the given string. If the internal array is full, a larger array is allocated.

Parameter
str – A String to write to the stream.
off – An offset into the string.
len – The number of characters to write.

public void writeTo(Writer out);

public void writeTo(Writer out) method writes the contents of this object’s internal buffer to the given Writer. All the data that has been written to this CharArrayWriter is written to out.

Parameter
out – The destination stream.

Apart from these CharArrayWriter 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)
READ  Popular Abstract Interfaces in Java

CharArrayWriter class also has inherited methods from class- Writer. They are as follows:

  • write(char[])
  • write(String)

Share and Enjoy !

Leave a Comment

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