Class ImageSizeLimitInterceptor

  • All Implemented Interfaces:
    okhttp3.Interceptor

    
    public final class ImageSizeLimitInterceptor
     implements Interceptor
                        

    An OkHttp interceptor that checks if the size of an image response exceeds a specified limit.

    This interceptor is designed to be used with OkHttp to prevent the loading of excessively large images, which can lead to out-of-memory errors or performance issues. It intercepts the response and checks the contentLength of the response body. If the content length exceeds the maxSizeBytes, it closes the response and throws a LargeImageSizeException.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      Response intercept(Interceptor.Chain chain) Intercepts the response and checks if the image size exceeds the limit.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ImageSizeLimitInterceptor

        ImageSizeLimitInterceptor(Long maxSizeBytes)
    • Method Detail

      • intercept

         Response intercept(Interceptor.Chain chain)

        Intercepts the response and checks if the image size exceeds the limit.

        This method is called by OkHttp for each response. It retrieves the contentLength from the response body and compares it to the maxSizeBytes. If the content length is greater than the maximum allowed size, it closes the response and throws a LargeImageSizeException. Otherwise, it returns the original response.