What’s A Byte Stream, Anyway?

Byte streams are a sequence of bytes used by programs to input and output information. The terms byte stream and octet-stream are both used somewhat synonymously, though octet streams are common among networking terminology.

Image by Overcoded

Bits vs. Bytes

Essentially, a bitstream becomes a byte stream when it’s interpreted as a series of 8-bit sequences rather than a sequence of 1’s and 0’s. If one is in a more formal mood, it can be called an octet-stream. It’s still a bunch of 1’s and 0’s but with the implication that one should interpret the data as an array of 8-bit values.

Image by Overcoded

Byte Stream

In the above example, the data can be interpreted as simple 1’s and 0’s amounting to the binary value of 102. However, if this were taken as a smaller segment within an indeterminately-long bitstream — even that interpretation wouldn’t be possible.

Image by Overcoded

Byte Stream Example in Java

Java is one of the most utilized programming languages in the world and, as one might expect, has great affordance for working with byte streams. Java provides a high-level API for streaming bytes between data sources. Below, see an example of using Java’s FileInputStream and FileOutputStream classes to copy the contents of input.txt to output.txt:

// Define I/O streamsFileInputStream inStream;FileOutputStream outStream;// Get input streaminStream = new FileInputStream("input.txt");// Get output StreamoutStream = new FileOutputStream("output.txt");// Stream input, write to outputint next;  // the next character in the byte streamwhile((next = inStream.read()) != -1){    // Write next character    outStream.write(next);}

Discussion

There is much to consider when it comes to bits, bytes, and the streaming of either. Modern network communications rely heavily on protocols largely concerned with the transmission of bits.

Entrepreneur, Computer Science Student, Designer, and self-confessed health nut that enjoys productivity hacks, developing better habits, and Mother Nature.