@@ -14,6 +14,8 @@ class InputStreamThread {
1414 private final Client client ;
1515 private final Socket socket ;
1616 private final Timer timer = new Timer ();
17+ private InputStream finalInputStream ;
18+ final AtomicReference <byte []> bytes = new AtomicReference <>(null );
1719
1820 public InputStreamThread (final Client client ) {
1921 this .client = client ;
@@ -22,9 +24,7 @@ public InputStreamThread(final Client client) {
2224
2325 public void run () throws IOException {
2426 //initialise inputStream
25- final InputStream finalInputStream = this .socket .getInputStream ();
26-
27- final AtomicReference <byte []> bytes = new AtomicReference <>(null );
27+ this .finalInputStream = this .socket .getInputStream ();
2828 //start reading byte arrays
2929 this .timer .scheduleAtFixedRate (new TimerTask () {
3030 @ Override
@@ -36,14 +36,14 @@ public void run() {
3636 return ;
3737 }
3838 //check if finalInputStream is null
39- assert finalInputStream != null ;
40- if (finalInputStream .available () > 0 ) {
41- final int b = finalInputStream .read ();
39+ assert InputStreamThread . this . finalInputStream != null ;
40+ if (InputStreamThread . this . finalInputStream .available () > 0 ) {
41+ final int b = InputStreamThread . this . finalInputStream .read ();
4242 if (b != -1 ) {
43- bytes .set (new byte [b ]);
43+ InputStreamThread . this . bytes .set (new byte [b ]);
4444 //receive bytes
45- finalInputStream .read (bytes .get (), 0 , b );
46- final ReadingByteBuffer readingByteBuffer = new ReadingByteBuffer (bytes .get ());
45+ InputStreamThread . this . finalInputStream .read (InputStreamThread . this . bytes .get (), 0 , b );
46+ final ReadingByteBuffer readingByteBuffer = new ReadingByteBuffer (InputStreamThread . this . bytes .get ());
4747 //read packetId
4848 final int packetId = readingByteBuffer .readInt ();
4949
@@ -76,6 +76,11 @@ public void run() {
7676 }
7777
7878 public void interrupt () {
79- this .timer .cancel ();
79+ try {
80+ this .finalInputStream .close ();
81+ this .timer .cancel ();
82+ } catch (final IOException exception ) {
83+ exception .printStackTrace ();
84+ }
8085 }
8186}
0 commit comments