DataInputStream readUnsignedByte() method in Java with Examples

The readUnsignedByte() method of DataInputStream class in Java is used to read byte and returns as an integer. The integer is an unsigned value in the range from 0 to 255. The bytes in this method are read from the accommodated input stream.

Syntax:

public final int readUnsignedByte()
                 throws IOException

Specified By: This method is specified by readUnsignedByte() method of DataInput interface.

Parameters: This method does not accept any parameter.

Return value: This method returns the byte value read as an integer. It is an unsigned 8-bit byte.



Exceptions:

  • EOFException – It throws EOFException if the input stream is ended.
  • IOException – This method throws IOException if the stream is closed or some other I/O error occurs.

Below programs illustrate readUnsignedByte() method in DataInputStream class in IO package:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// DataInputStream readUnsignedByte() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create byte array
        byte[] b = { 10, 20, 30, 40, 50 };
  
        // Create byte array input stream
        ByteArrayInputStream byteArrayInputStr
            = new ByteArrayInputStream(b);
  
        // Convert byte array input stream to
        // DataInputStream
        DataInputStream dataInputStr
            = new DataInputStream(
                byteArrayInputStr);
  
        while (dataInputStr.available() > 0) {
            // Print bytes
            System.out.println(
                dataInputStr.readUnsignedByte());
        }
    }
}

chevron_right


Output:

10
20
30
40
50

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// DataInputStream readUnsignedByte() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create byte array
        byte[] b = { -20, -10, 0, 10, 20 };
  
        // Create byte array input stream
        ByteArrayInputStream byteArrayInputStr
            = new ByteArrayInputStream(b);
  
        // Convert byte array input stream to
        // DataInputStream
        DataInputStream dataInputStr
            = new DataInputStream(
                byteArrayInputStr);
  
        while (dataInputStr.available() > 0) {
            // Print bytes
            System.out.println(
                dataInputStr.readUnsignedByte());
        }
    }
}

chevron_right


Output:

236
246
0
10
20

References:
https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readUnsignedByte()

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.