Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package examples;
- import java.io.BufferedOutputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.apache.commons.io.output.TeeOutputStream;
- import org.apache.commons.net.io.Util;
- public final class IOUtil {
- private static final Logger log = Logger.getLogger(IOUtil.class.getName());
- private static void readFromConsole(final OutputStream remoteOutput) {
- Thread read = new Thread() {
- @Override
- public void run() {
- int ch;
- try {
- while ((ch = System.in.read()) != -1) {
- remoteOutput.write(ch);
- remoteOutput.flush();
- }
- } catch (IOException ioe) {
- log.warning(ioe.toString());
- }
- }
- };
- read.start();
- }
- public static void writeToConsole(final InputStream remoteInput) {
- Thread write = new Thread() {
- @Override
- public void run() {
- try {
- Util.copyStream(remoteInput, System.out);
- } catch (IOException ioe) {
- log.warning(ioe.toString());
- }
- }
- };
- write.start();
- }
- public static void writeToFile(final OutputStream localOutput) throws FileNotFoundException {
- Thread fileWriter = new Thread() {
- @Override
- public void run() {
- BufferedOutputStream bufferedOutput = null;
- try {
- bufferedOutput = new BufferedOutputStream(new FileOutputStream("weather.log"));
- } catch (FileNotFoundException ex) {
- Logger.getLogger(IOUtil.class.getName()).log(Level.SEVERE, null, ex);
- }
- TeeOutputStream tee = new TeeOutputStream(localOutput, bufferedOutput);
- try {
- bufferedOutput.write(1);
- } catch (IOException ex) {
- Logger.getLogger(IOUtil.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- bufferedOutput.flush();
- } catch (IOException ex) {
- Logger.getLogger(IOUtil.class.getName()).log(Level.SEVERE, null, ex);
- }
- log.info("trying...");
- }
- };
- fileWriter.start();
- }
- public static void readWrite(final InputStream remoteInput, final OutputStream remoteOutput, final InputStream localInput, final OutputStream localOutput) throws FileNotFoundException {
- readFromConsole(remoteOutput);
- writeToConsole(remoteInput);
- writeToFile(remoteOutput);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment