Package stdlib
Class BinaryOut
java.lang.Object
stdlib.BinaryOut
Binary output. This class provides methods for converting
primtive type variables (
boolean, byte, char,
int, long, float, and double)
to sequences of bits and writing them to an output stream.
The output stream can be standard output, a file, an OutputStream or a Socket.
Uses big-endian (most-significant byte first).
The client must flush() the output stream when finished writing bits.
The client should not intermixing calls to BinaryOut with calls
to Out; otherwise unexpected behavior will result.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate a binary output stream from standard output.Create a binary output stream from an OutputStream.Create a binary output stream from a filename.Create a binary output stream from a Socket. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidvoidclose()Close and flush the binary output stream.voidflush()Flush the binary output stream, padding 0s if number of bits written so far is not a multiple of 8.static voidTest client.voidwrite(boolean x) Write the specified bit to the binary output stream.voidwrite(byte x) Write the 8-bit byte to the binary output stream.voidwrite(char x) Write the 8-bit char to the binary output stream.voidwrite(char x, int r) Write the r-bit char to the binary output stream.voidwrite(double x) Write the 64-bit double to the binary output stream.voidwrite(float x) Write the 32-bit float to the binary output stream.voidwrite(int x) Write the 32-bit int to the binary output stream.voidwrite(int x, int r) Write the r-bit int to the binary output stream.voidwrite(long x) Write the 64-bit long to the binary output stream.voidwrite(short x) Write the 16-bit int to the binary output stream.voidWrite the string of 8-bit characters to the binary output stream.voidWrite the String of r-bit characters to the binary output stream.private voidwriteBit(boolean bit) Write the specified bit to the binary output stream.private voidwriteByte(int x) Write the 8-bit byte to the binary output stream.
-
Field Details
-
out
-
buffer
-
N
-
-
Constructor Details
-
BinaryOut
Create a binary output stream from an OutputStream. -
BinaryOut
public BinaryOut()Create a binary output stream from standard output. -
BinaryOut
Create a binary output stream from a filename. -
BinaryOut
Create a binary output stream from a Socket.
-
-
Method Details
-
writeBit
Write the specified bit to the binary output stream. -
writeByte
Write the 8-bit byte to the binary output stream. -
clearBuffer
-
flush
Flush the binary output stream, padding 0s if number of bits written so far is not a multiple of 8. -
close
Close and flush the binary output stream. Once it is closed, you can no longer write bits. -
write
Write the specified bit to the binary output stream.- Parameters:
x- thebooleanto write.
-
write
Write the 8-bit byte to the binary output stream.- Parameters:
x- thebyteto write.
-
write
Write the 32-bit int to the binary output stream.- Parameters:
x- theintto write.
-
write
Write the r-bit int to the binary output stream.- Parameters:
x- theintto write.r- the number of relevant bits in the char.- Throws:
RuntimeException- ifris not between 1 and 32.RuntimeException- ifxis not between 0 and 2r - 1.
-
write
Write the 64-bit double to the binary output stream.- Parameters:
x- thedoubleto write.
-
write
Write the 64-bit long to the binary output stream.- Parameters:
x- thelongto write.
-
write
Write the 32-bit float to the binary output stream.- Parameters:
x- thefloatto write.
-
write
Write the 16-bit int to the binary output stream.- Parameters:
x- theshortto write.
-
write
Write the 8-bit char to the binary output stream.- Parameters:
x- thecharto write.- Throws:
RuntimeException- ifxis not betwen 0 and 255.
-
write
Write the r-bit char to the binary output stream.- Parameters:
x- thecharto write.r- the number of relevant bits in the char.- Throws:
RuntimeException- ifris not between 1 and 16.RuntimeException- ifxis not between 0 and 2r - 1.
-
write
Write the string of 8-bit characters to the binary output stream.- Parameters:
s- theStringto write.- Throws:
RuntimeException- if any character in the string is not between 0 and 255.
-
write
Write the String of r-bit characters to the binary output stream.- Parameters:
s- theStringto write.r- the number of relevants bits in each character.- Throws:
RuntimeException- if r is not between 1 and 16.RuntimeException- if any character in the string is not between 0 and 2r - 1.
-
main
Test client. Read bits from standard input and write to the file specified on command line.
-