2014-02-24 3 views
1

나는 어떤 일이 벌어 질지 상상조차 할 수없는 문제가있다.Return BufferedReader

두 번째 클래스를 호출하여 명령을 통과하고 결과를 반환하는 클래스가 있습니다.

결과는 BufferedReader이며,이 결과는 첫 번째 클래스에서 처리됩니다.

일등석 :

. 
    . 
    .  
    String command = "ping " + ip + " -c 1"; 
    BufferedReader result = instance.sendCommand(command); 
     // close only after all commands are sent 
     System.out.println("out of sshManager result : "+result); 
     System.out.println("out of sshManager line : "+result.readLine()); 
     String line = null; 
     while ((line = result.readLine()) != null) { 
      System.out.println("out of sshManager line: "+line); 
     } 

2 등석 :

public BufferedReader sendCommand(String command) throws JSchException, IOException { 
    //StringBuilder outputBuffer = new StringBuilder();  

     Channel channel = sesConnection.openChannel("exec"); 
     ((ChannelExec) channel).setCommand(command); 
     channel.connect(); 

     InputStream commandOutput = channel.getInputStream(); 
     System.out.println("This is in sshmanager SSHManager: " + commandOutput); 
     result = new BufferedReader(new InputStreamReader(commandOutput));    
     String line = null; 
     System.out.println(" sshmanager result : " + result()); 
     System.out.println(" sshmanager result line : " + result.readLine()); 

     while ((line = result.readLine()) != null) { 
      System.out.println("in sshmanager: " + line); 
     } 

     System.out.println("in sshmanager result : " + result); 
    channel.disconnect(); 
    return result;   
} 

결과 :

This is in sshmanager SSHManager: [email protected] 
sshmanager result : [email protected] 
sshmanager result line : PING 192.168.11.11 (192.168.11.11) 56(84) bytes of data. 
in sshmanager: From 192.168.11.77 icmp_seq=1 Destination Host Unreachable 
in sshmanager: 
in sshmanager: --- 192.168.11.11 ping statistics --- 
in sshmanager: 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 3006ms 
in sshmanager: 
in sshmanager result: [email protected] 
out of sshManager result: [email protected] 
out of sshManager line: null 

객체를 잘 내 두 번째 클래스에서 만든하지만 난 몰라 왜, 때 내 첫 번째 클래스에서 객체를 관리하려고하면 contect가 null입니다.

당신은 어떤 상상을하고 있습니까?

답변

2

BufferedReader은 첫 번째 방법으로 반환 될 때 파일을 이미 읽었습니다. BufferedReader 대신 파일 내용이 포함 된 List<String>을 반환 할 수 있습니다.

public List<String> sendCommand(String command) throws JSchException, IOException { 
     List<String> lines = new LinkedList<String>(); 
     Channel channel = sesConnection.openChannel("exec"); 
     ((ChannelExec) channel).setCommand(command); 
     channel.connect(); 

     InputStream commandOutput = channel.getInputStream(); 
     result = new BufferedReader(new InputStreamReader(commandOutput));    
     String line = null; 

     while ((line = result.readLine()) != null) { 
      lines.add(line); 
     } 

     channel.disconnect(); 
     return lines;   
} 
+0

감사합니다. 작동하고 다른 작업을 위해 이러한 데이터를 보관해야하므로 좋은 생각입니다. Btw 거기에 내가 BufferedReader 이해가 안되는 이유는, 내가 두 번째 클래스의'readlince'에 주석을 달았고 while을 제거하면 그 결과는 여전히 null이기 때문입니다. 그러나,'System.out.println ("sshmanager result line :"+ result.readLine());을 유지하면 첫 번째 줄없이 첫 번째 클래스에서 결과를 읽을 수 있습니다. 조금 이상합니다. 그렇지 않습니다. ? – Sallyerik

1

모두 스트리밍 개념에서 비롯됩니다.

"2 등급"으로 스트림을 진행하면 실제로 스트림을 소비합니다. 그래서, 당신이 그것을 "1 등석"으로 되 돌리는 순간, 그 물줄기는 완전히 소모됩니다. 따라서 더 이상 아무것도 스트리밍 할 수 없습니다.

스트림을 리더로 래핑하는 경우에도 읽기 작업 (예 : readLine)이 스트림으로 전달됩니다.

1

버퍼 판독기는 에서 번까지 읽을 수있는 바이트 문자열과 같습니다.

기본 문제는 독자가 이미 텍스트를 추출 했으므로 다시 시도 할 때 null이됩니다 (판독기가 NOW이므로 비어 있음).