[ Back | Previous | Next ]

How to create Inputstreams with PipedStreams?

Package:
java.io.*
Product:
JDK
Release:
1.0
Related Links:
General
File
FilenameFilter
FileWriter
JPEGCodec
ObjectInputStream
OutputStream
PipedInputStream
PrintWriter
StreamTokenizer
Comment:
private String _makeUpMail(AbstractMelding aMelding, final EDIFactPrint edi_print) {
	// CREATE
	final StringBuffer message = new StringBuffer();
	try {
		edi_print.setMelding(aMelding);
		PipedWriter pout = new PipedWriter();
		PipedReader pipeIn = new PipedReader(pout);
		final PrintWriter out = new PrintWriter(pout, true);
		final BufferedReader in = new BufferedReader(pipeIn, 1024 * 30);
		System.out.println(".");
		Runnable readRun = new Runnable() {
			public void run() {
				if (in != null) {
					try {
						String input;
						int i = 0;
						while ((input = in.readLine()) != null) {
							try {
								dbg("reading#" + i + ":" + input);
								if (i != 0 && input != null) {
									message.append("\n");
								}
								message.append(input);
								i++;
							} catch (Exception be) {
								System.out.println("badLocation: : " + be.getMessage());
								throw new IllegalArgumentException("_makeUpMail(): Error:" + be.getMessage());
							}
						}
					} catch (IOException e) {
						System.err.println("_makeUpMail(): IOException: " + e);
						throw new IllegalArgumentException("_makeUpMail(): Error:" + e.getMessage());
					}
				}
			}
		};
		Runnable writeRun = new Runnable() {
			public void run() {
				try {
					edi_print._jspService(out);
				} catch (Throwable t) {
					System.err.println("_makeUpMail(): IOException: " + t);
					throw new IllegalArgumentException("_makeUpMail(): Error:" + t.getMessage());
				}
			}
		};
		Thread readThread = new Thread(readRun, "ReadingPrint");
		readThread.start();
		new Thread(writeRun, "WritingPrint").start();
		int i = 0;
		while (readThread.isAlive()) {
			try {
				Thread.currentThread().sleep(10000);
			} catch (InterruptedException ie) {
			}
			System.out.print(".");
			if (i % 80 == 0) {
				System.out.println();
				i = 0;
			}
			i++;
		}
	} catch (IOException ioe) {
		throw new IllegalArgumentException("_makeUpMail(): IOError:" + ioe.getMessage());
	}
	return message.toString();
}
1