Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - ObjectInputStream readUnsignedByte() method



Description

The Java ObjectInputStream readUnsignedByte() method reads one byte from the input stream and returns it as an integer (0 to 255). This is useful when reading unsigned byte data, as Java's byte type is signed (-128 to 127).

Declaration

Following is the declaration for java.io.ObjectInputStream.readUnsignedByte() method.

public int readUnsignedByte()

Parameters

NA

Return Value

This method returns the 8 bit byte read.

Exception

  • EOFException − If end of file is reached.

  • IOException − If other I/O error has occurred.

Example - Usage of ObjectInputStream readUnsignedByte() method

The following example shows the usage of Java ObjectInputStream readUnsignedByte() method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      byte b = 127;
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeByte(b);
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print the unshared object
         System.out.println("" + ois.readUnsignedByte());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

127

Example - Reading a Single Unsigned Byte

The following example shows the usage of Java ObjectInputStream readUnsignedByte() method. This example writes an unsigned byte to a file and reads it back using readUnsignedByte().

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      String filename = "byteData.bin";

      // Writing a byte value (e.g., 200, which is outside Java's signed byte range)
      try (ObjectOutputStream dos = new ObjectOutputStream(new FileOutputStream(filename))) {
         dos.writeByte(200); // Writing an unsigned byte (200)
         System.out.println("Byte value 200 written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Reading the unsigned byte using ObjectInputStream
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         int unsignedByte = ois.readUnsignedByte(); // Reads as int (0-255)
         System.out.println("Read unsigned byte value: " + unsignedByte);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Byte value 200 written to file.
Read unsigned byte value: 200

Explanation

  • writeByte(200) writes an unsigned byte (value 200).

  • readUnsignedByte() reads it as an int, so the value 200 is correctly read (instead of -56 if read as a signed byte).

  • The output confirms that 200 is read without issues.

Example - Reading Multiple Unsigned Bytes from a File

The following example shows the usage of Java ObjectInputStream readUnsignedByte() method. This example writes multiple unsigned bytes to a file and reads them back.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      String filename = "multiByteData.bin";

      // Writing multiple unsigned byte values
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
    	 oos.writeByte(255); // Max unsigned byte value
    	 oos.writeByte(128); // Mid-range value
    	 oos.writeByte(0);   // Min unsigned byte value
         System.out.println("Unsigned bytes written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Reading the unsigned bytes using ObjectInputStream
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         System.out.println("Reading unsigned bytes:");
         for (int i = 0; i < 3; i++) {
            int value = ois.readUnsignedByte();
            System.out.println("Byte " + (i + 1) + ": " + value);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Unsigned bytes written to file.
Reading unsigned bytes:
Byte 1: 255
Byte 2: 128
Byte 3: 0

Explanation

  • We write three unsigned bytes: 255, 128, and 0.

  • readUnsignedByte() correctly reads each value as an integer (0-255).

  • This ensures that unsigned values remain accurate after deserialization.

java_io_objectinputstream.htm
Advertisements