Wrapping InputOutput Streams

suggest change

OutputStream and InputStream have many different classes, each of them with a unique functionality. By wrapping a stream around another, you gain the functionality of both streams.

You can wrap a stream any number of times, just take note of the ordering.

Useful combinations

Writing characters to a file while using a buffer

File myFile = new File("targetFile.txt");
PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(myFile)));

Compressing and encrypting data before writing to a file while using a buffer

Cipher cipher = ... // Initialize cipher
File myFile = new File("targetFile.enc");
BufferedOutputStream outputStream = new BufferedOutputStream(new DeflaterOutputStream(new CipherOutputStream(new FileOutputStream(myFile), cipher)));

List of Input/Output Stream wrappers

Wrapper | Description | —————– | —————– | BufferedOutputStream/ BufferedInputStream | While OutputStream writes data one byte at a time, BufferedOutputStream writes data in chunks. This reduces the number of system calls, thus improving performance. | DeflaterOutputStream/ DeflaterInputStream | Performs data compression. | InflaterOutputStream/ InflaterInputStream | Performs data decompression. | CipherOutputStream/ CipherInputStream | Encrypts/Decrypts data. | DigestOutputStream/ DigestInputStream | Generates Message Digest to verify data integrity. | CheckedOutputStream/ CheckedInputStream | Generates a CheckSum. CheckSum is a more trivial version of Message Digest. | DataOutputStream/ DataInputStream | Allows writing of primitive data types and Strings. Meant for writing bytes. Platform independent. | PrintStream | Allows writing of primitive data types and Strings. Meant for writing bytes. Platform dependent. | OutputStreamWriter | Converts a OutputStream into a Writer. An OutputStream deals with bytes while Writers deals with characters | PrintWriter | Automatically calls OutputStreamWriter. Allows writing of primitive data types and Strings. Strictly for writing characters and best for writing characters |

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


InputStreams and OutputStreams:
* Wrapping InputOutput Streams

Table Of Contents
8 Arrays
10 Maps
11 Strings
12 InputStreams and OutputStreams
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap