private File saveAsFile ( ResponseBody responseBody ) { if ( responseBody == null ) { return null ; } try { File file = new File ( destDirectory + File . separator + fileName + retrieveUniqueId ( ) + "." + extension ) ; InputStream inputStream = null ; OutputStream outputStream = null ; try { inputStream = responseBody . byteStream ( ) ; outputStream = new FileOutputStream ( file ) ; byte [ ] buffer = new byte [ 4096 ] ; int numOfBufferedBytes ; while ( ( numOfBufferedBytes = inputStream . read ( buffer ) ) != END_OF_FILE_DENOTER ) { outputStream . write ( buffer , 0 , numOfBufferedBytes ) ; } outputStream . flush ( ) ; return file ; } catch ( IOException exception ) { return null ; } catch ( Exception exception ) { return null ; } finally { if ( inputStream != null ) { inputStream . close ( ) ; } if ( outputStream != null ) { outputStream . close ( ) ; } } } catch ( IOException exception ) { return null ; } }
Saves the file returned in the response body as a file in the cache directory