- Forward


Data Input and Output
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Methods in the DataInputStream Class
Back SMYC Forward
  • boolean readBoolean()
  • byte readByte()
  • char readChar()
  • double readDouble()
  • float readFloat()
  • int readInt()
  • long readLong()
  • short readShort()
Methods in the DataOutputStream Class
Back SMYC Forward
  • writeBoolean(boolean v)
  • writeByte(int v)
  • writeChar(int v)
  • writeDouble(double v)
  • writeFloat(float v)
  • writeInt(int v)
  • writeLong(long v)
  • writeShort(int v)
Things are Not as Simple as they Appear
Back SMYC Forward
  • Complications when using the console
  • Complications caused by byte ordering
Complications When Using the Console
Back SMYC Forward

The Following Seems Fine

javaexamples/streams/DataWriter.java
 
Complications When Using the Console (cont.)
Back SMYC Forward
  • Why does the int 97 become ' a'?
  • Why does the double 97 become '@X@ '?
Complications When Using the Console (cont.)
Back SMYC Forward

The Following Also Seems Fine

javaexamples/streams/DataReader.java
 
Complications When Using the Console (cont.)
Back SMYC Forward
  • Try typing a 1-digit int (terminated by a CR+LF)
  • Try typing a 2-digit int (terminated by a CR+LF)
  • Try typing a 1-digit double (terminated by a CR+LF)
  • Try typing a 6-digit double (terminated by a CR+LF)
Byte Order
Back SMYC Forward

Using one byte to represent unsigned integers is straightforward. For example, using the usual conventions, the decimal number 26 can be represented as follows:

  00011010
  

Byte Order (cont.)
Back SMYC Forward

Using two bytes to represent unsigned integers is a little trickier. For example, which of the following is the correct representation of the number 256?

  00000001 00000000
  
Most significant byte first (Big-endian or network byte order)
Expand

or

  00000000 00000001
  
Least significant byte first (Little-endian)
Expand

Byte Order (cont.)
Back SMYC Forward
  • A "Real-World" Examples: Dates
    • 7/4/1776
      Expand
    • July 4 1776
      Expand
    • 4 July 1776
      Expand
    • 1776 July 4
      Expand
  • Doesn't the Same Issue Arise with Strings?
    • ASCII uses one byte
    • EBCDIC is a different one-byte system
    • Unicode uses two/four bytes (depending on the encoding) in a fixed order
Re-Ordering Bytes
Back SMYC Forward
  • Use a mask to "extract" a particular byte
  • Use bit-shifting to "move" a byte
  • "Combine" the bytes
Re-Odering Bytes (cont.)
Back SMYC Forward

To "extract" the LSB:

        01100001 00011010
  &     00000000 11111111
        _________________
        00000000 00011010
  

That is, perform a bitwise AND with the decimal number 255 (or 0x00FF).

Re-Odering Bytes (cont.)
Back SMYC Forward

To "extract" the MSB:

        01100001 00011010
  &     11111111 00000000 
        _________________
        01100001 00000000
  

That is, perform a bitwise AND with the decimal number 65280 (or 0xFF00).

Re-Odering Bytes (cont.)
Back SMYC Forward

Shifting Bits:

        00000011 00000000 
   >>   8
        _________________
        00000000 00000011
  

That is, 768 >> 8 evaluates to 3.

Re-Odering Bytes (cont.)
Back SMYC Forward

Combining Bytes:

        00000011 00000000 
      | 00000000 00000001
        _________________
        00000011 00000001
  

That is, 768 | 1 evaluates to 769.

Byte Order (cont.)
Back SMYC Forward
  • Java:
    • Uses network byte order (i.e., big-endian) on all platforms
  • C/C++:
    • Do not specify a byte order and typically use whatever the platform uses (e.g., big-endian on Unix, little-endian on MS-DOS)
Returning to an Earlier Example
Back SMYC Forward
javaexamples/streams/DataWriter.java
 

What will be printed if we assign 97 + (65 << 8) to i?

There's Always More to Learn
Back -