Java Streams  «Prev 

Java Streaming Data

Although streams read and write bytes, Java does not support the byte data type very well.
Bytes are always promoted to ints when used in a calculation or passed to or returned from a method. The signed-ness of Java's byte data type also presents a problem. While we generally think of a byte read or written as a number between 0 and 255,
Java bytes are signed numbers between -128 and 127.
It's important to notice the distinction between the logical, unsigned byte used in I/O and the byte primitive data type. Much of the time the logical, unsigned unit of I/O byte will actually be represented by an int in the Java code.
public final class Byte
extends Number
implements Comparable<Byte>
The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte. In addition, this class provides several methods for converting a byte to a String and a String to a byte, as well as other constants and methods useful when dealing with a byte.
This data type exists since Java 1.1/

Data streams read and write strings, integers, floating-point numbers, and other data that is commonly presented at a higher level than mere bytes. The java.io.DataInputStream and java.io.DataOutputStream classes read and write the primitive Java data types (boolean, int, double, etc.) and strings in a particular, well-defined, platform-independent format. Since DataInputStream and DataOutputStream use the same formats, they are complementary. What a data output stream writes, a data input stream can read. These classes are especially useful when you need to move data between platforms that may use different native formats for integers or floating-point numbers.

The Data Stream Classes

The java.io.DataInputStream and java.io.DataOutputStream classes are subclasses of FilterInputStream and FilterOutputStream, respectively.
public class DataInputStream extends FilterInputStream implements DataInput
public class DataOutputStream extends FilterOutputStream implements DataOutput

They have all the usual methods you have come to associate with input and output stream classes, such as read(), write(), flush(), available(), skip(), close(), markSupported(), and reset(). (Data input streams support marking if, and only if, their underlying input stream supports marking.) However, the real purpose of DataInputStream and DataOutputStream is not to read and write raw bytes using the standard input and output stream methods.
The real purpose of DataInputStream and DataOutputStream is to read and interpret multibyte data such as
  1. ints,
  2. floats,
  3. doubles, and
  4. chars.