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 456d420 commit 7d4f56b
Showing 1 changed file with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ InetSocketAddress start() throws IOException {
break;
}
fileOut.write(buffer, 0, n);
fileOut.flush();
System.out.println("Sent " + n + " bytes to a file. Value: " + new String(buffer, 0, n));
}
log.info("Done redirecting input");
Expand All @@ -114,35 +115,29 @@ InetSocketAddress start() throws IOException {
}
});

Thread threadB = new Thread(() -> {
log.info("Redirecting output");
try (
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 = fileIn.read(buffer))) {
break;
}
System.out.println("Read " + n + " bytes from a file. Value: " + new String(buffer, 0, n));
socketOut.write(buffer, 0, n);
socketOut.flush();
log.info("Redirecting output");
try (
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 = fileIn.read(buffer))) {
break;
}
log.info("Done redirecting output");
IOUtils.closeQuietly(incomingSocket);
IOUtils.closeQuietly(randomAccessFile);
} catch (Exception e) {
log.warn("output", e);
System.out.println("Read " + n + " bytes from a file. Value: " + new String(buffer, 0, n));
socketOut.write(buffer, 0, n);
}
});
log.info("Done redirecting output");
IOUtils.closeQuietly(incomingSocket);
IOUtils.closeQuietly(randomAccessFile);
} catch (Exception e) {
log.warn("output", e);
}

threadA.start();
threadB.start();

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

0 comments on commit 7d4f56b

Please sign in to comment.