Skip to content

fix(filehandling): EgovFileUtil.readFile 빈 파일 NoSuchElementException 방지#257

Open
z3rotig4r wants to merge 1 commit into
eGovFramework:mainfrom
z3rotig4r:fix/filehandling-readfile-empty
Open

fix(filehandling): EgovFileUtil.readFile 빈 파일 NoSuchElementException 방지#257
z3rotig4r wants to merge 1 commit into
eGovFramework:mainfrom
z3rotig4r:fix/filehandling-readfile-empty

Conversation

@z3rotig4r

Copy link
Copy Markdown
Contributor

문제

EgovFileUtil.readFile(File, encoding)의 라인 결합 루프가 hasNext() 검사 없이 it.next()를 먼저 호출한다.

for (Iterator<String> it = lines.iterator(); ; ) {
    sb.append(it.next());          // 빈 리스트면 즉시 예외
    if (it.hasNext()) {
        sb.append("");             // 항상 no-op (죽은 코드)
    } else {
        break;
    }
}

내용이 없는 파일은 라인 리스트가 비어 있어 첫 it.next()에서 NoSuchElementException이 미포착 전파된다. 기대값은 빈 문자열이다.

수정

표준 hasNext() 가드 루프로 교체했다.

for (Iterator<String> it = lines.iterator(); it.hasNext(); ) {
    sb.append(it.next());
}
  • 빈 파일은 빈 문자열을 반환한다.
  • sb.append("")는 어떤 입력에서도 no-op이므로, 비어 있지 않은 단일/여러 줄 입력의 출력은 종전과 바이트 단위로 동일하다. 라인 구분자 처리 의미는 변경하지 않았다(readLines 계열이 구분자를 제거하는 기존 동작 유지).

테스트

FilehandlingServiceTest에 2건 추가, 모듈 25건 통과.

  • testReadEmptyFile: 빈 파일 → "" 반환
  • testReadMultiLineFile: 여러 줄 입력의 결합 결과가 종전과 동일함을 고정(회귀 가드)

readFile(File, encoding)의 조인 루프가 hasNext() 검사 없이 it.next()를 먼저
호출해, 빈 파일(빈 라인 리스트)에서 NoSuchElementException이 미포착 전파됐다.
표준 hasNext() 가드 루프로 교체해 빈 파일은 빈 문자열을 반환한다. 죽은 코드
sb.append("")(항상 no-op) 제거로 비어있지 않은 입력의 출력은 불변이다.

테스트: 빈 파일/여러 줄 파일 읽기 2건 추가.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant