간단한 코드의 변경을 논하다
4794 단어 코드
public static String decodeBuffer(String str ,String charset){
if (str == null) {
return str = StringUtils.EMPTY;
}
try {
byte[] byteStr = new BASE64Decoder().decodeBuffer(str.trim());
return new String(byteStr,charset);
} catch (UnsupportedEncodingException e) {
return new String();
} catch (IOException e) {
return new String();
}
}
이렇게 바꾸면 좀 낫지 않을까요?(공백에 대한 처리를 변경하고 공백이면 공백으로 되돌려줍니다)
public static String decodeBuffer(String str, String charset) {
String decodeStr = StringUtils.EMPTY;
if (!StringUtils.isBlank(str)) {
try {
byte[] byteStr = new BASE64Decoder().decodeBuffer(str);
return new String(byteStr, charset);
} catch (UnsupportedEncodingException e) {
return new String(); //
} catch (IOException e) {
return new String(); //
}
}
return decodeStr;
}
프로그램을 어디까지 써야 할지 생각할 때가 있어요.더 잘해야 할지, 적당히 해야 할지, 프로그래머로서 때로는 집착정신이 필요할 때가 있다. 최선은 바라지 않지만 더 잘해야 한다.
다시 고치기 (이상 로그 기록을 추가하면 return만 보장됩니다):
public static String decodeBuffer(String str, String charset) {
String decodeStr = StringUtils.EMPTY;
if (!StringUtils.isBlank(str)) {
try {
byte[] byteStr = new BASE64Decoder().decodeBuffer(str);
decodeStr = new String(byteStr, charset);
} catch (UnsupportedEncodingException e) {
log.debug("UnsupportedEncodingException happened:" + e.getMessage()); //
} catch (IOException e) {
log.debug("IOException happened:" + e.getMessage()); //
}
}
return decodeStr;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
vue 단일 페이지에 여러 개의 echarts 도표가 있을 때의 공용 코드 쓰기html에서: 데이터 처리는 말할 필요가 없다.응, 직접 그림을 그려: 공통 섹션: 이 페이지를 떠날 때 파괴: 추가 정보: Vue + Echarts 차트 표시 및 동적 렌더링 준비 작업 echarts 의존 설치 n...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.