Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Feb 22, 2017
1 parent 7d4f56b commit 96e3538
Showing 1 changed file with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,53 +91,54 @@ InetSocketAddress start() throws IOException {

log.info("Created RandomAccessFile from " + NAMED_PIPE_FILE_NAME);

Thread threadA = new Thread(() -> {
log.info("Redirecting input");
Thread threadB = new Thread(() -> {
log.info("Redirecting output");
try (
InputStream socketIn = incomingSocket.getInputStream();
FileOutputStream fileOut = new FileOutputStream(randomAccessFile.getFD());
OutputStream socketOut = incomingSocket.getOutputStream();
FileInputStream fileIn = new FileInputStream(randomAccessFile.getFD());
) {
final byte[] buffer = new byte[1024 * 4];
int n;
while (true) {
if (IOUtils.EOF == (n = socketIn.read(buffer))) {
if (IOUtils.EOF == (n = fileIn.read(buffer))) {
break;
}
fileOut.write(buffer, 0, n);
fileOut.flush();
System.out.println("Sent " + n + " bytes to a file. Value: " + new String(buffer, 0, n));
System.out.println("Read " + n + " bytes from a file. Value: " + new String(buffer, 0, n));
socketOut.write(buffer, 0, n);
socketOut.flush();
}
log.info("Done redirecting input");
log.info("Done redirecting output");
IOUtils.closeQuietly(incomingSocket);
IOUtils.closeQuietly(randomAccessFile);
} catch (Exception e) {
log.warn("input", e);
log.warn("output", e);
}
});

log.info("Redirecting output");
threadB.start();

log.info("Redirecting input");
try (
OutputStream socketOut = incomingSocket.getOutputStream();
FileInputStream fileIn = new FileInputStream(randomAccessFile.getFD());
InputStream socketIn = incomingSocket.getInputStream();
FileOutputStream fileOut = new FileOutputStream(randomAccessFile.getFD());
) {
final byte[] buffer = new byte[1024 * 4];
int n;
while (true) {
if (IOUtils.EOF == (n = fileIn.read(buffer))) {
if (IOUtils.EOF == (n = socketIn.read(buffer))) {
break;
}
System.out.println("Read " + n + " bytes from a file. Value: " + new String(buffer, 0, n));
socketOut.write(buffer, 0, n);
fileOut.write(buffer, 0, n);
System.out.println("Sent " + n + " bytes to a file. Value: " + new String(buffer, 0, n));
}
log.info("Done redirecting output");
log.info("Done redirecting input");
IOUtils.closeQuietly(incomingSocket);
IOUtils.closeQuietly(randomAccessFile);
} catch (Exception e) {
log.warn("output", e);
log.warn("input", e);
}

threadA.start();
threadA.join();
threadB.join();
log.info("all futures completed");
} catch (Exception e) {
log.warn("", e);
Expand Down

0 comments on commit 96e3538

Please sign in to comment.