Java-如果同一个方法返回`FileInputStream`,如何在finally块中关闭`FileInputStream`

时间:2016-09-28 10:13:07

标签: java sonarqube fileinputstream autocloseable

Sonar显示以下代码的阻止类型的错误。它抱怨我没有关闭FileInputStream但我该怎么做? FileInputStream是方法的返回类型,如果我在这里关闭,那么它在调用的地方就没用了。请告诉我 - 如果同一方法返回FileInputStream,我如何在finally块中关闭FileInputStream

以下是代码:

@Override
public InputStream getInputStream() throws MissingObjectException {

    try {
        InputStream is;
        if (smbFile != null) {
            is = new BufferedInputStream(new SmbFileInputStream(smbFile), 60000);
        } 
        else {
            is = new BufferedInputStream(new FileInputStream(getFilePath()));
        }
        return is;
    }
    catch (Exception e) {
            throw new MissingObjectException();
    }
}

2 个答案:

答案 0 :(得分:2)

无需在同一功能中关闭输入。问题可能是您不应在InputStream is块中声明try{}以及放置return语句。

答案 1 :(得分:0)

在try block

之前放置声明
public class MyBroadcastReceiver extends BroadcastReceiver {
    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber; 

    @Override
    public void onReceive(Context context, Intent intent) {

        //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
            savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
        }
        else{
            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            int state = 0;
            if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                state = TelephonyManager.CALL_STATE_IDLE;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                state = TelephonyManager.CALL_STATE_OFFHOOK;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                state = TelephonyManager.CALL_STATE_RINGING;
            }
            onCallStateChanged(context, state, number);
        }
    }

    public void onCallStateChanged(Context context, int state, String number) {
        if(lastState == state){
            //No change, debounce extras
            return;
        }
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                savedNumber = number;
                onIncomingCallStarted(context, number, callStartTime);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //Transition of ringing->offhook are pickups of incoming calls.  Nothing done on them
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    callStartTime = new Date();
                    onOutgoingCallStarted(context, savedNumber, callStartTime);
                    Toast.makeText(context, "Veza uspostavljena", Toast.LENGTH_SHORT).show();
                }
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    //Ring but no pickup-  a miss
                    onMissedCall(context, savedNumber, callStartTime);
                }
                else if(isIncoming){
                    onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
                }
                else{
                    onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
                    Toast.makeText(context, "Odbijen poziv", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        lastState = state;
    }
}