
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - DataInputStream readFully(byte[] b, int off, int len) method
Description
The Java DataInputStream readFully(byte[] b, int off, int len) method reads len bytes from an input stream.
It blocks until the one of the below conditions occurs −
b.length bytes of input data are available.
End of file detected.
If any I/O error occurs.
Declaration
Following is the declaration for java.io.DataInputStream.readFully(byte[] b, int off, int len) method −
public final void readFully(byte[] b, int off, int len)
Parameters
b − The destination buffer.
off − The offset into the data.
len − The number of bytes to read.
Return Value
This method does not return any value.
Exception
IOException − If any I/O error occurs or the stream has been closed.
EOFException − If this input stream reaches the end before.
Assumption
Assuming we have a text file test.txt, which has the following content. This file will be used as an input for our example program −
ABCDEFGH
Example - Usage of DataInputStream readFully(byte[] b, int off, int len) method
The following example shows the usage of Java DataInputStream readFully(byte[] b, int off, int len) method. We've created InputStream and DataInputStream references and then initialized them with FileInputStream and DataInputStream objects. In order to initialize DataInputStream(), we requires FileInputStream object. Once objects are created, we're checking if inputStream has content using available() method. Then a bytearray of available bytes is created which is then used in DataInputStream readFully() method, which populates the bytearray completely. Then this bytearray is iterated and printed.
DataInputStreamDemo.java
package com.tutorialspoint; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { InputStream is = null; DataInputStream dis = null; try { // create input stream from file input stream is = new FileInputStream("test.txt"); // create data input stream dis = new DataInputStream(is); // count the available bytes form the input stream int count = is.available(); // create buffer byte[] bs = new byte[count]; // read data into buffer dis.readFully(bs, 4, 3); // for each byte in the buffer for (byte b:bs) { // print the byte System.out.print(b+" "); } } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases any associated system files with this stream if(is!=null) is.close(); if(dis!=null) dis.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
0 0 0 0 65 66 67 0
Example - Usage of DataInputStream readFully(byte[] b, int off, int len) method
The following example shows the usage of Java DataInputStream readFully(byte[] b, int off, int len) method. We've created InputStream and DataInputStream references and then initialized them with FileInputStream and DataInputStream objects. In order to initialize DataInputStream(), we requires FileInputStream object.
Once objects are created, we're checking if inputStream has content using available() method. Then a bytearray of available bytes is created which is then used in DataInputStream readFully() method, which populates the bytearray as per given input. Then this bytearray is iterated and printed.
DataInputStreamDemo.java
package com.tutorialspoint; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { InputStream is = null; DataInputStream dis = null; try { // create input stream from file input stream is = new FileInputStream("test.txt"); // create data input stream dis = new DataInputStream(is); // count the available bytes form the input stream int count = is.available(); // create buffer byte[] bs = new byte[count]; // read data into buffer with an invalid length dis.readFully(bs, 4, 10); // for each byte in the buffer for (byte b:bs) { // print the byte System.out.print(b+" "); } } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases any associated system files with this stream if(is!=null) is.close(); if(dis!=null) dis.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
java.lang.IndexOutOfBoundsException: Range [4, 4 + 10) out of bounds for length 8 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:118) at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:397) at java.base/java.util.Objects.checkFromIndexSize(Objects.java:437) at java.base/java.io.DataInputStream.readFully(DataInputStream.java:205) at com.tutorialspoint.DataInputStreamDemo.main(DataInputStreamDemo.java:27)
Example - Usage of DataInputStream readFully(byte[] b, int off, int len) method
The following example shows the usage of Java DataInputStream readFully(byte[] b, int off, int len) method. We've created InputStream and DataInputStream references and then initialized them with FileInputStream and DataInputStream objects. In order to initialize DataInputStream(), we requires FileInputStream object. Once objects are created, we're checking if inputStream has content using available() method. Then a bytearray of available bytes is created which is then used in DataInputStream readFully() method, which populates the bytearray as per given input to get all the bytes of the stream. Then this bytearray is iterated and printed.
DataInputStreamDemo.java
package com.tutorialspoint; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { InputStream is = null; DataInputStream dis = null; try { // create input stream from file input stream is = new FileInputStream("test.txt"); // create data input stream dis = new DataInputStream(is); // count the available bytes form the input stream int count = is.available(); // create buffer byte[] bs = new byte[count]; int off = 0; int lens = 0; // read data into buffer dis.readFully(bs); // for each byte in the buffer for (byte b:bs) { // convert byte into character char c = (char)b; // print the character System.out.print(c+" "); } } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases any associated system files with this stream if(is!=null) is.close(); if(dis!=null) dis.close(); } } }
Assuming we have a text file test.txt, which has no content.
Output
Let us compile and run the above program, this will produce the following result −
65 66 67 68 69 70 71 72