Java - ObjectInputStream readUnsignedShort() method



Description

The Java ObjectInputStream readUnsignedShort() method reads two bytes from the input stream and returns an integer (0 to 65535). This is useful when working with unsigned short values since Java's short type is signed (-32,768 to 32,767).

Declaration

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

 public int readUnsignedShort() 

Parameters

NA

Return Value

This method returns the 16 bit short read.

Exception

  • EOFException − If end of file is reached.

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

Example - Usage of ObjectInputStream readUnsignedShort() method

The following example shows the usage of Java ObjectInputStream readUnsignedShort() 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) { short b = 32767; 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.writeShort(b); oout.flush(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print the short System.out.println("" + ois.readUnsignedShort()); } catch (Exception ex) { ex.printStackTrace(); } } } 

Output

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

 32767 

Example - Reading a Single Unsigned Short

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

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 = "shortData.bin"; // Writing an unsigned short value (e.g., 50000, which exceeds Java's signed short range) try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename))) { dos.writeShort(50000); // Writing 50000 as short (stored in 2 bytes) System.out.println("Unsigned short value 50000 written to file."); } catch (IOException e) { e.printStackTrace(); } // Reading the unsigned short using ObjectInputStream try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { int unsignedShort = ois.readUnsignedShort(); // Reads as int (0-65535) System.out.println("Read unsigned short value: " + unsignedShort); } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 Unsigned short value 50000 written to file. Read unsigned short value: 50000 

Explanation

  • writeShort(50000) writes a 2-byte value.

  • Since 50000 is outside Java's signed short range (-32,768 to 32,767), it would be stored incorrectly if read as a short.

  • readUnsignedShort() correctly reads it as an integer (50000).

  • If we had used readShort(), we would have gotten a negative number due to sign extension.

Example - Reading Multiple Unsigned Shorts from a File

The following example shows the usage of Java ObjectInputStream readUnsignedShort() method. This example writes multiple unsigned short values 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 = "multiShortData.bin"; // Writing multiple unsigned short values try (ObjectOutputStream dos = new ObjectOutputStream(new FileOutputStream(filename))) { dos.writeShort(65535); // Maximum unsigned short value dos.writeShort(32768); // Mid-range value dos.writeShort(0); // Minimum unsigned short value System.out.println("Unsigned short values written to file."); } catch (IOException e) { e.printStackTrace(); } // Reading the unsigned shorts using ObjectInputStream try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { System.out.println("Reading unsigned short values:"); for (int i = 0; i < 3; i++) { int value = ois.readUnsignedShort(); System.out.println("Short " + (i + 1) + ": " + value); } } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 Unsigned short values written to file. Reading unsigned short values: Short 1: 65535 Short 2: 32768 Short 3: 0 

Explanation

  • Writes three unsigned short values: 65535 (max), 32768 (mid-range), and 0 (min).

  • readUnsignedShort() ensures all values are interpreted correctly (instead of negative values).

  • This method is useful when dealing with binary protocols, file formats, or network data where unsigned values are needed.

java_io_objectinputstream.htm
Advertisements