private static boolean canRead ( final DataInput pInput , final boolean pReset ) { long pos = FREE_SID ; if ( pReset ) { try { if ( pInput instanceof InputStream && ( ( InputStream ) pInput ) . markSupported ( ) ) { ( ( InputStream ) pInput ) . mark ( 8 ) ; } else if ( pInput instanceof ImageInputStream ) { ( ( ImageInputStream ) pInput ) . mark ( ) ; } else if ( pInput instanceof RandomAccessFile ) { pos = ( ( RandomAccessFile ) pInput ) . getFilePointer ( ) ; } else if ( pInput instanceof LittleEndianRandomAccessFile ) { pos = ( ( LittleEndianRandomAccessFile ) pInput ) . getFilePointer ( ) ; } else { return false ; } } catch ( IOException ignore ) { return false ; } } try { byte [ ] magic = new byte [ 8 ] ; pInput . readFully ( magic ) ; return Arrays . equals ( magic , MAGIC ) ; } catch ( IOException ignore ) { // Ignore } finally { if ( pReset ) { try { if ( pInput instanceof InputStream && ( ( InputStream ) pInput ) . markSupported ( ) ) { ( ( InputStream ) pInput ) . reset ( ) ; } else if ( pInput instanceof ImageInputStream ) { ( ( ImageInputStream ) pInput ) . reset ( ) ; } else if ( pInput instanceof RandomAccessFile ) { ( ( RandomAccessFile ) pInput ) . seek ( pos ) ; } else if ( pInput instanceof LittleEndianRandomAccessFile ) { ( ( LittleEndianRandomAccessFile ) pInput ) . seek ( pos ) ; } } catch ( IOException e ) { // TODO: This isn't actually good enough... // Means something fucked up, and will fail... e . printStackTrace ( ) ; } } } return false ; }
It s probably safer to create one version for InputStream and one for File