read(char[], int, int): This method is available in the java.io.PipedReader class of Java.
Syntax:
int java.io.PipedReader.read(char[] cbuf, int off, int len) throws IOException
This method takes three arguments. This method reads up to len characters of data from this piped stream into an array of characters.
Less than len characters will be read if the end of the data stream is reached or if len exceeds the pipe's buffer size.
This method blocks until at least one character of the input is available.
Parameters: Three parameters are required for this method.
cbuf: the buffer into which the data is read.
off: the start offset of the data.
len: the maximum number of characters read.
Returns: the total number of characters read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
1. IOException - if the pipe is broken, unconnected, closed, or an I/O error occurs.
2. IndexOutOfBoundsException - If off is negative, or len is negative, or len is greater than cbuf.length - off.
3. NullPointerException- if cbuf is null.
Approach 1: When no exception
Java
import java.io.IOException;import java.io.PipedReader;import java.io.PipedWriter;public class PipedReaderread2 {public static void main(String[] args) throws IOException {PipedReader pipedReader = new PipedReader();PipedWriter src = new PipedWriter();pipedReader.connect(src);char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int off = 0, len = 3;System.out.println("Reading");pipedReader.read(cbuf, off, len);pipedReader.close();}}
Output:
Reading
Approach 2: IOException
Java
package com.PipedReader;import java.io.IOException;import java.io.PipedReader;public class PipedReaderread2 {public static void main(String[] args) throws IOException {PipedReader pipedReader = new PipedReader();char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int off = 0, len = 3;pipedReader.read(cbuf, off, len);pipedReader.close();}}
Output:
Exception in thread "main" java.io.IOException: Pipe not connected at java.base/java.io.PipedReader.read(PipedReader.java:293) at com.PipedReader.PipedReaderread2.main(PipedReaderread2.java:13)
Approach 3: IndexOutOfBoundsException
Java
package com.PipedReader;import java.io.IOException;import java.io.PipedReader;import java.io.PipedWriter;public class PipedReaderread2 {public static void main(String[] args) throws IOException {PipedReader pipedReader = new PipedReader();PipedWriter src = new PipedWriter();pipedReader.connect(src);char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int off = 0, len = 13;pipedReader.read(cbuf, off, len);pipedReader.close();}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.PipedReader.read(PipedReader.java:303) at com.PipedReader.PipedReaderread2.main(PipedReaderread2.java:18)
Approach 4: NullPointerException
Java
package com.PipedReader;import java.io.IOException;import java.io.PipedReader;import java.io.PipedWriter;public class PipedReaderread2 {public static void main(String[] args) throws IOException {PipedReader pipedReader = new PipedReader();PipedWriter src = new PipedWriter();pipedReader.connect(src);char cbuf[] = null;int off = 0, len = 3;pipedReader.read(cbuf, off, len);pipedReader.close();}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "cbuf" is null at java.base/java.io.PipedReader.read(PipedReader.java:301) at com.PipedReader.PipedReaderread2.main(PipedReaderread2.java:18)
Some other methods of PipedReader
close(): This method Closes this piped stream and releases any system resources associated with the stream.
connect(PipedWriter): This method causes this piped reader to be connected to the piped writer src.
PipedReader(): This method creates a PipedReader so that it is not yet connected. It must be connected to a PipedWriter before being used.
PipedReader(int): This method creates a PipedReader so that it is not yet connected and uses a specified pipe size for the pipe's buffer.
PipedReader(PipedWriter): This method creates a PipedReader so that it is connected to the piped writer src.
PipedReader(PipedWriter, int): This method creates a PipedReader so that it is connected to the piped writer src and uses the specified pipe size for the pipe's buffer.
read(): This method reads the next character of data from this piped stream.
read(char[], int, int): This method reads up to len characters of data from this piped stream into an array of characters.
ready(): This method tells whether this stream is ready to be read.
No comments:
Post a Comment