2017-11-21 1 views
0

에 나는 코드FileInputStream에 오류가 SonarQube

문제는 다음 날은 API LEVEL 19+ 위입니다 try-with-resources를 사용하도록 제안 sonarqube을 SonarQube 중요한 문제가 무엇입니까 그리고 난 minimumSDKLEVEL 16을 목표로하고있다.

어떤 식 으로든 아래 코드에서 이미 완료 한 finally 블록에있는 FileInputStream 블록을 보여주는 것입니다.

protected void copyFile(File sourceFile, File destFile) throws IOException { 
    if (!sourceFile.exists()) { 
     return; 
    } 

    FileChannel source = null; 
    FileChannel destination = null; 
    try { 
     source = new FileInputStream(sourceFile).getChannel(); 
     destination = new FileOutputStream(destFile).getChannel(); 
     if (source != null) { 
      destination.transferFrom(source, 0, source.size()); 
     } 

    } finally { 
     if (source != null && destination != null) { 

      source.close(); 
      destination.close(); 
     } 
    } 

} 

심지어 안드로이드 스튜디오 빨간색 줄 오류를 제공하고 동일 finally 블록에 try-with-resources 또는 가까운 스트림을 사용하도록 제안합니다.

UPDATE 모두가 null 인 경우에만 두 채널을 폐쇄하고, 당신이 스트림을 폐쇄하지 않는 때문입니다

Pre check exception by SonarLint

답변

0

. 같은 블록을 분리/닫기 수표를 추출하는 시도 : source 또는 destination 스트림 중 하나가 null하지 않을 때 시나리오가 있기 때문에

protected void copyFile(File sourceFile, File destFile) throws IOException { 
    if (!sourceFile.exists()) { 
     return; 
    } 

    FileInputStream fis = null; 
    FileChannel source = null; 
    FileOutputStream fout = null; 
    FileChannel destination = null; 
    try { 
     fis = new FileInputStream(sourceFile); 
     source = fis.getChannel(); 
     fout = new FileOutputStream(destFile); 
     destination = fout.getChannel(); 
     if (source != null) { 
      destination.transferFrom(source, 0, source.size()); 
     } 

    } finally { 
     if (fis != null) { 
      try { 
       fis.close(); 
      } catch (IOException e) { 
       System.out.println("Failed to close source input stream."); 
      } 
     } 
     if (source != null) { 
      try { 
       source.close(); 
      } catch (IOException e) { 
       System.out.println("Failed to close source channel."); 
      } 
     } 
     if (fout != null) { 
      try { 
       fout.close(); 
      } catch (IOException e) { 
       System.out.println("Failed to close destination output stream."); 
      } 
     } 
     if (destination != null) { 
      try { 
       destination.close(); 
      } catch (IOException e) { 
       System.out.println("Failed to close destination channel."); 
      } 
     } 
    } 
} 
+0

시도해 보았지만 finally 블록에서 닫는 스트림을 나타내는 오류가 여전히 발생했습니다. – Aks4125

+1

죄송합니다. close()가 Exception을 throw 할 수 있기 때문입니다. 나는 대답을 업데이트 할 것이다. – fejd

+0

업데이트 된 질문을 확인하십시오. 업데이트 된 솔루션을 사용했습니다. – Aks4125

1

SonarQube이 중요한 문제를주고있다. 또한 FileInputStream 개체를 닫지 않고 채널을 닫는 중입니다. 이 경우에는 FileChannel을 닫지 않을 것입니다. 이 라인을 참조하십시오이를 방지하기 위해

if (source != null && destination != null) {...} 

, 당신은 다음과 같은 작업을 수행 할 수

1 : @fejd이 제안되는 FileChannel의 분할 가깝습니다.

2 : 각각 블록이 자체 스트림을 닫는 중첩 된 try-catch 블록을 포함합니다.

일반적인 참고 사항 중 하나는 복잡한 시나리오에서 2을 사용할 수 있지만 주어진 시나리오에서는 1이면 충분합니다.

+0

@ fejd의 대답과 2 번 점수를 시도했지만 여전히 오류가 발생했습니다. :( – Aks4125

관련 문제