DataInputStream readFully() method in Java with Examples - GeeksforGeeks

05 Jun, 2020
The readFully() method of DataInputStream class in Java is of two types:
readFully(byte[] b) method of DataInputStream class in Java is used to read bytes equal to the length of byte array b from an input stream and store them into the byte array b.General Contract:The readFully(byte[] b) method is blocked in case of one of the following conditions:Input data is available and it returns normal.File is ended and an EOFException is thrown.An I/O error occurs and an IOException is thrown.Syntax:public final void readFully(byte[] b)
                  throws IOException
Specified By: This method is specified by the readFully() method of DataInput interface.Parameters: This method accepts one parameter b which represents the byte array into which the data is to be read.Return value: This method does not return any value.Exceptions:NullPointerException – It throws NullPointerException if byte array is null.EOFException – It throws EOFException if the file is ended.IOException – This method throws IOException if the stream is closed or some other I/O error occurs.Below program illustrates readFully(byte[]) method in DataInputStream class in IO package:Program: Assume the existence of file “c:/demo.txt”.// Java program to illustrate// DataInputStream readFully(byte[]) methodimport java.io.*;public class GFG {    public static void main(String[] args)        throws IOException    {          // Create input stream 'demo.txt'        // for reading containing        // text "GEEKSFORGEEKS"        FileInputStream inputStream            = new FileInputStream(                "c:/demo.txt");          // Convert inputStream to        // DataInputStream        DataInputStream dataInputStr            = new DataInputStream(                inputStream);          // Count the total bytes        int count = dataInputStr.available();          // Create byte array        byte[] b = new byte[count];          // Read full data into byte array        dataInputStr.readFully(b);          for (byte by : b) {            // Print the character            System.out.print((char)by);        }    }}Input:Output:readFully(byte[] b, int offset, int length) method of DataInputStream class in Java is used to read bytes equal to the parameter passed ‘length’ from an input stream and store them into the byte array b.General Contract:The readFully(byte[], int, int) method is blocked in case of one of the following conditions:Input data is available and it returns normal.File is ended and an EOFException is thrown.An I/O error occurs and an IOException is thrown.Syntax:public final void readFully(byte[] b,
                            int offset,
                            int length)
                     throws IOException
Specified By: This method is specified by the readFully() method of DataInput interface.Parameters: This method accepts three parameters:b – It represents the byte array into which the data is to read.offset – It represents the starting index in the byte array.length – It represents the total number of bytes to be read.Return value: This method does not return any value.Exceptions:NullPointerException – It throws NullPointerException if byte array is null.IndexOutOfBoundsException – It throws IndexOutOfBoundsException if offset is negative or length is negative or length is greater than the difference of length of byte array and offset.EOFException – It throws EOFException if the file is ended.IOException – This method throws IOException if the stream is closed or some other I/O error occurs.Below program illustrates readFully(byte[], int, int) method in DataInputStream class in IO package:Program: Assume the existence of file “c:/demo.txt”.// Java program to illustrate// DataInputStream readFully(byte[], int, int) methodimport java.io.*;public class GFG {    public static void main(String[] args)        throws IOException    {          // Create input stream 'demo.txt'        // for reading containing        // text "GEEKSFORGEEKS"        FileInputStream inputStream            = new FileInputStream(                "c:/demo.txt");          // Convert inputStream to        // DataInputStream        DataInputStream dataInputStr            = new DataInputStream(                inputStream);          // Count the total bytes        int count = dataInputStr.available();          // Create byte array        byte[] b = new byte[count];          // Read full data into byte array        dataInputStr.readFully(b, 4, 5);          for (byte by : b) {            // Print the character            System.out.print((char)by);        }    }}Input:Output:
readFully(byte[] b) method of DataInputStream class in Java is used to read bytes equal to the length of byte array b from an input stream and store them into the byte array b.General Contract:The readFully(byte[] b) method is blocked in case of one of the following conditions:Input data is available and it returns normal.File is ended and an EOFException is thrown.An I/O error occurs and an IOException is thrown.Syntax:public final void readFully(byte[] b)
                  throws IOException
Specified By: This method is specified by the readFully() method of DataInput interface.Parameters: This method accepts one parameter b which represents the byte array into which the data is to be read.Return value: This method does not return any value.Exceptions:NullPointerException – It throws NullPointerException if byte array is null.EOFException – It throws EOFException if the file is ended.IOException – This method throws IOException if the stream is closed or some other I/O error occurs.Below program illustrates readFully(byte[]) method in DataInputStream class in IO package:Program: Assume the existence of file “c:/demo.txt”.// Java program to illustrate// DataInputStream readFully(byte[]) methodimport java.io.*;public class GFG {    public static void main(String[] args)        throws IOException    {          // Create input stream 'demo.txt'        // for reading containing        // text "GEEKSFORGEEKS"        FileInputStream inputStream            = new FileInputStream(                "c:/demo.txt");          // Convert inputStream to        // DataInputStream        DataInputStream dataInputStr            = new DataInputStream(                inputStream);          // Count the total bytes        int count = dataInputStr.available();          // Create byte array        byte[] b = new byte[count];          // Read full data into byte array        dataInputStr.readFully(b);          for (byte by : b) {            // Print the character            System.out.print((char)by);        }    }}Input:Output:
General Contract:The readFully(byte[] b) method is blocked in case of one of the following conditions:
Input data is available and it returns normal.
File is ended and an EOFException is thrown.
An I/O error occurs and an IOException is thrown.
Syntax:
public final void readFully(byte[] b)
                  throws IOException

Specified By: This method is specified by the readFully() method of DataInput interface.
Parameters: This method accepts one parameter b which represents the byte array into which the data is to be read.
Return value: This method does not return any value.
Exceptions:
NullPointerException – It throws NullPointerException if byte array is null.
EOFException – It throws EOFException if the file is ended.
IOException – This method throws IOException if the stream is closed or some other I/O error occurs.
Below program illustrates readFully(byte[]) method in DataInputStream class in IO package:
Program: Assume the existence of file “c:/demo.txt”.
// Java program to illustrate// DataInputStream readFully(byte[]) methodimport java.io.*;public class GFG {    public static void main(String[] args)        throws IOException    {          // Create input stream 'demo.txt'        // for reading containing        // text "GEEKSFORGEEKS"        FileInputStream inputStream            = new FileInputStream(                "c:/demo.txt");          // Convert inputStream to        // DataInputStream        DataInputStream dataInputStr            = new DataInputStream(                inputStream);          // Count the total bytes        int count = dataInputStr.available();          // Create byte array        byte[] b = new byte[count];          // Read full data into byte array        dataInputStr.readFully(b);          for (byte by : b) {            // Print the character            System.out.print((char)by);        }    }}
readFully(byte[] b, int offset, int length) method of DataInputStream class in Java is used to read bytes equal to the parameter passed ‘length’ from an input stream and store them into the byte array b.General Contract:The readFully(byte[], int, int) method is blocked in case of one of the following conditions:Input data is available and it returns normal.File is ended and an EOFException is thrown.An I/O error occurs and an IOException is thrown.Syntax:public final void readFully(byte[] b,
                            int offset,
                            int length)
                     throws IOException
Specified By: This method is specified by the readFully() method of DataInput interface.Parameters: This method accepts three parameters:b – It represents the byte array into which the data is to read.offset – It represents the starting index in the byte array.length – It represents the total number of bytes to be read.Return value: This method does not return any value.Exceptions:NullPointerException – It throws NullPointerException if byte array is null.IndexOutOfBoundsException – It throws IndexOutOfBoundsException if offset is negative or length is negative or length is greater than the difference of length of byte array and offset.EOFException – It throws EOFException if the file is ended.IOException – This method throws IOException if the stream is closed or some other I/O error occurs.Below program illustrates readFully(byte[], int, int) method in DataInputStream class in IO package:Program: Assume the existence of file “c:/demo.txt”.// Java program to illustrate// DataInputStream readFully(byte[], int, int) methodimport java.io.*;public class GFG {    public static void main(String[] args)        throws IOException    {          // Create input stream 'demo.txt'        // for reading containing        // text "GEEKSFORGEEKS"        FileInputStream inputStream            = new FileInputStream(                "c:/demo.txt");          // Convert inputStream to        // DataInputStream        DataInputStream dataInputStr            = new DataInputStream(                inputStream);          // Count the total bytes        int count = dataInputStr.available();          // Create byte array        byte[] b = new byte[count];          // Read full data into byte array        dataInputStr.readFully(b, 4, 5);          for (byte by : b) {            // Print the character            System.out.print((char)by);        }    }}Input:Output:
General Contract:The readFully(byte[], int, int) method is blocked in case of one of the following conditions:
Input data is available and it returns normal.
File is ended and an EOFException is thrown.
An I/O error occurs and an IOException is thrown.
Syntax:
public final void readFully(byte[] b,
                            int offset,
                            int length)
                     throws IOException

Specified By: This method is specified by the readFully() method of DataInput interface.
Parameters: This method accepts three parameters:
b – It represents the byte array into which the data is to read.
offset – It represents the starting index in the byte array.
length – It represents the total number of bytes to be read.
Return value: This method does not return any value.
Exceptions:
NullPointerException – It throws NullPointerException if byte array is null.
IndexOutOfBoundsException – It throws IndexOutOfBoundsException if offset is negative or length is negative or length is greater than the difference of length of byte array and offset.
EOFException – It throws EOFException if the file is ended.
IOException – This method throws IOException if the stream is closed or some other I/O error occurs.
Below program illustrates readFully(byte[], int, int) method in DataInputStream class in IO package:
Program: Assume the existence of file “c:/demo.txt”.
// Java program to illustrate// DataInputStream readFully(byte[], int, int) methodimport java.io.*;public class GFG {    public static void main(String[] args)        throws IOException    {          // Create input stream 'demo.txt'        // for reading containing        // text "GEEKSFORGEEKS"        FileInputStream inputStream            = new FileInputStream(                "c:/demo.txt");          // Convert inputStream to        // DataInputStream        DataInputStream dataInputStr            = new DataInputStream(                inputStream);          // Count the total bytes        int count = dataInputStr.available();          // Create byte array        byte[] b = new byte[count];          // Read full data into byte array        dataInputStr.readFully(b, 4, 5);          for (byte by : b) {            // Print the character            System.out.print((char)by);        }    }}
References:1. https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readFully(byte%5B%5D)2. https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readFully(byte%5B%5D, int, int)
Java-Functions
Java-IO package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
ArrayList in Java
How to iterate any Map in Java
Multidimensional Arrays in Java
Stack Class in Java
Singleton Class in Java
Set in Java