PipedInputStream Examples

Introduction:

Java provides a rich set of I/O classes that enable developers to efficiently handle input and output operations. One such class is `PipedInputStream`, which is part of the java.io package. In this blog post, we'll explore the intricacies of `PipedInputStream` and provide 10 code examples to illustrate its various use cases.

PipedInputStream Examples
PipedInputStream Examples



1. Basic Usage:


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class BasicUsageExample {
public static void main(String[] args) throws IOException {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

// Connect the input and output streams
pis.connect(pos);

// Write data to the output stream
pos.write("Hello, PipedInputStream!".getBytes());

// Read data from the input stream
int data;
while ((data = pis.read()) != -1) {
System.out.print((char) data);
}

// Close streams
pos.close();
pis.close();
}
}

Explanation: This example demonstrates the basic setup of `PipedInputStream` and `PipedOutputStream` and how to transfer data between them.

2. Reading Data into a Buffer:


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class BufferedReaderExample {
public static void main(String[] args) throws IOException {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

pis.connect(pos);

pos.write("Buffered Read Example".getBytes());

byte[] buffer = new byte[1024];
int bytesRead;

// Read data into a buffer
while ((bytesRead = pis.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, bytesRead));
}

pos.close();
pis.close();
}
}

Explanation: This example showcases how to read data from `PipedInputStream` into a buffer for more efficient processing.

3. Multi-Threaded Communication:


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class MultiThreadedExample {
public static void main(String[] args) {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

try {
pis.connect(pos);

// Start a writer thread
Thread writerThread = new Thread(() -> {
try {
pos.write("Multi-threaded Example".getBytes());
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start a reader thread
Thread readerThread = new Thread(() -> {
try {
int data;
while ((data = pis.read()) != -1) {
System.out.print((char) data);
}
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start threads
writerThread.start();
readerThread.start();

// Wait for threads to finish
writerThread.join();
readerThread.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

Explanation: Demonstrates how to use `PipedInputStream` and `PipedOutputStream` for communication between multiple threads.

4. Using ByteArrayInputStream:


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class ByteArrayInputStreamExample {
public static void main(String[] args) throws IOException {
byte[] data = "ByteArrayInputStream Example".getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(data);
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

pis.connect(pos);

// Use ByteArrayInputStream to feed data to PipedInputStream
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = bais.read(buffer)) != -1) {
pos.write(buffer, 0, bytesRead);
}

// Read data from PipedInputStream
while ((bytesRead = pis.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, bytesRead));
}

pos.close();
pis.close();
}
}

Explanation: Illustrates how to combine `ByteArrayInputStream` with `PipedInputStream` for flexibility in data sources.

5. Handling Interrupts:


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class InterruptHandlingExample {
public static void main(String[] args) {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

try {
pis.connect(pos);

// Start a reader thread
Thread readerThread = new Thread(() -> {
try {
int data;
while ((data = pis.read()) != -1) {
System.out.print((char) data);
// Simulate interrupt
if (Thread.interrupted()) {
System.out.println("Reader Thread Interrupted");
break;
}
}
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start threads
readerThread.start();

// Simulate interrupt after a delay
try {
Thread.sleep(1000);
readerThread.interrupt();
readerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Demonstrates how to handle interrupts gracefully in a `PipedInputStream` reader thread.

6. Timeout for Blocking Read:


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class TimeoutExample {
public static void main(String[] args) {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

try {
pis.connect(pos);

// Start a reader thread
Thread readerThread = new Thread(() -> {
try {
int data;
long startTime = System.currentTimeMillis();

// Set a timeout for blocking read
while ((data = pis.read()) != -1) {
System.out.print((char) data);

// Break if timeout exceeds 2 seconds
if (System.currentTimeMillis() - startTime > 2000) {
System.out.println("\nTimeout Reached");
break;
}
}
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start threads
readerThread.start();

// Write data after a delay
try {
Thread.sleep(1000);
pos.write("Timeout Example".getBytes());
pos.close();
readerThread.join();
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Shows how to set a timeout for blocking reads in a `PipedInputStream`.

7. Buffered Reading with BufferedReader:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class BufferedReaderExample {
public static void main(String[] args) {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

try {
pis.connect(pos);

// Start a writer thread
Thread writerThread = new Thread(() -> {
try {
pos.write("Buffered Reader Example".getBytes());
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start a reader thread with BufferedReader
Thread readerThread = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(pis))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
});

// Start threads
writerThread.start();
readerThread.start();

// Wait for threads to finish
writerThread.join();
readerThread.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

Explanation: Illustrates the usage of `BufferedReader` with `PipedInputStream` for more convenient reading.

8. Resetting the Stream:


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class ResetStreamExample {
public static void main(String[] args) {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

try {
pis.connect(pos);

// Start a writer thread
Thread writerThread = new Thread(() -> {
try {
pos.write("Reset Stream Example".getBytes());
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start a reader thread
Thread readerThread = new Thread(() -> {
try {
int data;
while ((data = pis.read()) != -1) {
System.out.print((char) data);
}

// Reset the stream to read again
pis.close();
pis.connect(new PipedOutputStream());
writerThread.join();
System.out.println("\nStream Reset\n");

// Start a new reader thread
Thread newReaderThread = new Thread(() -> {
try {
int newData;
while ((newData = pis.read()) != -1) {
System.out.print((char) newData);
}
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
});

newReaderThread.start();
newReaderThread.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
});

// Start threads
writerThread.start();
readerThread.start();

// Wait for threads to finish
writerThread.join();
readerThread.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

Explanation: Demonstrates how to reset a `PipedInputStream` for reusing it to read the data again.

9. Handling EOF:


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class EOFHandlingExample {
public static void main(String[] args) {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

try {
pis.connect(pos);

// Start a writer thread
Thread writerThread = new Thread(() -> {
try {
pos.write("EOF Handling Example".getBytes());
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start a reader thread
Thread readerThread = new Thread(() -> {
try {
int data;
while (true) {
data = pis.read();
if (data == -1) {
System.out.println("\nEnd of File (EOF) Reached");
break;
}
System.out.print((char) data);
}
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start threads
writerThread.start();
readerThread.start();

// Wait for threads to finish
writerThread.join();
readerThread.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

Explanation: Shows how to handle the End of File (EOF) condition when reading from a `PipedInputStream`.

10. Large Data Transfer:


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Arrays;

public class LargeDataTransferExample {
public static void main(String[] args) {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();

try {
pis.connect(pos);

// Start a writer thread
Thread writerThread = new Thread(() -> {
try {
byte[] largeData = new byte[1024 * 1024]; // 1 MB
Arrays.fill(largeData, (byte) 'A');
pos.write(largeData);
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start a reader thread
Thread readerThread = new Thread(() -> {
try {
byte[] buffer = new byte[1024];
int bytesRead;

while ((bytesRead = pis.read(buffer)) != -1) {
// Process the data
System.out.print(new String(buffer, 0, bytesRead));
}
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
});

// Start threads
writerThread.start();
readerThread.start();

// Wait for threads to finish
writerThread.join();
readerThread.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

Explanation: Demonstrates how to transfer large amounts of data through `PipedInputStream` and `PipedOutputStream`.

Conclusion:

In this blog post, we delved into the various aspects of `PipedInputStream` in Java, exploring its basic usage, multithreading capabilities, handling interrupts, and more. By providing these 10 code examples, we hope to have equipped you with a comprehensive understanding of how to leverage `PipedInputStream` for efficient I/O operations in your Java applications.

Post a Comment

Previous Post Next Post