package com.o_char.pong;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

/* 送信用クラス */
public class PongSender {
  private PongController pongController;
  private Socket socket;
  private BufferedWriter bfw;
  private int i;
  private PrintWriter out;

  private PongSender(PongController npc, Socket ns, BufferedWriter nbfw, int ni) {
    this.pongController = npc;
    this.socket = ns;
    this.bfw = nbfw;
    this.i = ni;
    this.out = new PrintWriter(this.bfw, true);
  }

  public static PongSender createSender(PongController pc, Socket socket, int i)
          throws IOException {
    OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
    BufferedWriter bfw = new BufferedWriter(osw); // データ送信用バッファの設定

    PongSender pongSender = new PongSender(pc, socket, bfw, i);
    if (i != -1) {
      System.out.println("Complete setting : Sender[" + i + "] = " + pongSender);
      System.out.println("Complete setting : Sending Buffered Writer[" + i + "] = " + bfw);
    } else {
      System.out.println("Complete setting : Sender = " + pongSender);
      System.out.println("Complete setting : Sending Buffered Writer = " + bfw);
    }
    return pongSender;
  }

  public static PongSender createSender(PongController pc, Socket socket)
          throws IOException {
    return PongSender.createSender(pc, socket, -1);
  }

  // 送信する
  public boolean send(String string) {
    if (this.bfw == null) {
      return false;
    }

    boolean isNormalWork = true;

    System.out.println("Send: \"" + string + "\" to " + this.socket.getRemoteSocketAddress());
    out.println(string); // データの送信

    return isNormalWork;
  }

  // 送信用バッファを閉じる
  public void terminate() {
    if (this.bfw == null) {
      return;
    }
    try {
      if (this.i == -1) {
        System.out.println("Closing : Sending Buffered Writer = " + this.bfw);
      } else {
        System.out.println("Closing : Sending Buffered Writer[" + i + "] = " + this.bfw);
      }
      this.bfw.close();
    } catch (IOException ioe) {
      // Do Nothing.
    } finally {
      this.bfw = null;
    }
  }
}

@@ -316,18 +316,22 @@
 te int i
+ndex
 ;%0A
-
   privat
@@ -428,17 +428,23 @@
 w, int n
-i
+ewIndex
 ) %7B%0A    
@@ -527,13 +527,23 @@
 is.i
- = ni
+ndex = newIndex
 ;%0A  
@@ -672,22 +672,22 @@
 t, int i
-)%0A    
+ndex)%0A
       th
@@ -913,16 +913,20 @@
 , bfw, i
+ndex
 );%0A    i
@@ -928,17 +928,32 @@
     if (
-i
+pongSender.index
  != -1) 
@@ -1006,25 +1006,40 @@
  Sender%5B%22 + 
-i
+pongSender.index
  + %22%5D = %22 + 
@@ -1116,33 +1116,48 @@
 ered Writer%5B%22 + 
-i
+pongSender.index
  + %22%5D = %22 + bfw)
@@ -1431,20 +1431,16 @@
 socket)%0A
-    
       th
@@ -1454,24 +1454,24 @@
 Exception %7B%0A
+
     return P
@@ -1522,14 +1522,90 @@
 %0A  /
-/ %E9%80%81%E4%BF%A1%E3%81%99%E3%82%8B
+**%0A   * %E6%96%87%E5%AD%97%E5%88%97%E3%82%92%E9%80%81%E4%BF%A1%E3%81%99%E3%82%8B.%0A   *%0A   * @param string %E9%80%81%E4%BF%A1%E3%81%99%E3%82%8B%E6%96%87%E5%AD%97%E5%88%97.%0A   * @return %E9%80%81%E4%BF%A1%E3%81%8C%E6%88%90%E5%8A%9F%E3%81%97%E3%81%9F%E3%81%8B%E3%81%A9%E3%81%86%E3%81%8B.%0A   */
 %0A  p
@@ -1699,42 +1699,8 @@
  %7D%0A%0A
-    boolean isNormalWork = true;%0A%0A
     
@@ -1786,24 +1786,41 @@
 Address());%0A
+    // %E3%83%87%E3%83%BC%E3%82%BF%E3%82%92%E9%80%81%E4%BF%A1%E3%81%99%E3%82%8B.%0A
     out.prin
@@ -1835,19 +1835,8 @@
 ng);
- // %E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E9%80%81%E4%BF%A1%0A
 %0A   
@@ -1847,20 +1847,12 @@
 urn 
-isNormalWork
+true
 ;%0A  
@@ -1973,16 +1973,20 @@
  (this.i
+ndex
  == -1) 
@@ -2077,24 +2077,24 @@
    %7D else %7B%0A
-
         Syst
@@ -2148,16 +2148,20 @@
 er%5B%22 + i
+ndex
  + %22%5D = 

refactor pongSender.