why a Socket is not instanceof Closeable at runtime?

后端 未结 3 1069
名媛妹妹
名媛妹妹 2020-12-21 09:35

In an Android app, I added this code to onCreate()

    Closeable sss = new Socket();
    if (!(sss instanceof Closeable)) {
        throw new Runtim         


        
相关标签:
3条回答
  • 2020-12-21 09:53

    For future readers, I want to clarify something that was assumed to be evident by everyone in this discussion:

    How could I assign Closeable sss = new Socket(); if Socket is not Closeable?

    The API requirements specified in AndroidManifest.xml are:

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    

    and the device reports

    android.os.Build.VERSION.SDK_INT=16
    

    The code was compiled for API level 19, this is why it was successfully compiled. But it was run on API level 16, and this is why it failed.

    In other words, the compile-time check was performed for API19 on one computer (the desktop PC) while the run-time check was performed for API16 on another computer (the handheld device).

    0 讨论(0)
  • 2020-12-21 10:03

    Socket is not itself closeable, but both of its streams are. And closing either stream will close the Socket

    0 讨论(0)
  • 2020-12-21 10:06

    This issue occurs on Android API levels prior to 19. On affected versions, Socket does not implement Closeable.

    Sources:

    • Android: Exception using Closeable interface with Socket
    • https://code.google.com/p/android/issues/detail?id=62909
    0 讨论(0)
提交回复
热议问题