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 8acb68d commit fbe2f12
Showing 1 changed file with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,54 +92,54 @@ InetSocketAddress start() throws IOException {
log.info("Created RandomAccessFile from " + NAMED_PIPE_FILE_NAME);

Thread threadB = new Thread(() -> {
log.info("Redirecting output");
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 = socketIn.read(buffer))) {
break;
}
synchronized (randomAccessFile) {
if (IOUtils.EOF == (n = fileIn.read(buffer))) {
break;
}
fileOut.write(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();
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);
}
});

threadB.start();

log.info("Redirecting input");
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());
) {
threadB.start();

final byte[] buffer = new byte[1024 * 4];
int n;
while (true) {
if (IOUtils.EOF == (n = socketIn.read(buffer))) {
break;
}
synchronized (randomAccessFile) {
fileOut.write(buffer, 0, n);
if (IOUtils.EOF == (n = fileIn.read(buffer))) {
break;
}
}
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);
}

threadB.join();
Expand Down

0 comments on commit fbe2f12

Please sign in to comment.