@@ -329,19 +329,18 @@ public void close() throws IOException {
329329 public int read (long position , byte [] buffer , int offset , int length )
330330 throws IOException {
331331 checkStream ();
332- if (in instanceof PositionedReadable ) {
333- final int n = ((PositionedReadable ) in ).read (position , buffer , offset ,
334- length );
335- if (n > 0 ) {
336- // This operation does not change the current offset of the file
337- decrypt (position , buffer , offset , n );
338- }
339-
340- return n ;
341- } else {
332+ if (!(in instanceof PositionedReadable )) {
342333 throw new UnsupportedOperationException ("This stream does not support " +
343334 "positioned read." );
344335 }
336+ final int n = ((PositionedReadable ) in ).read (position , buffer , offset ,
337+ length );
338+ if (n > 0 ) {
339+ // This operation does not change the current offset of the file
340+ decrypt (position , buffer , offset , n );
341+ }
342+
343+ return n ;
345344 }
346345
347346 /**
@@ -351,19 +350,18 @@ public int read(long position, byte[] buffer, int offset, int length)
351350 public int read (long position , final ByteBuffer buf )
352351 throws IOException {
353352 checkStream ();
354- if (in instanceof ByteBufferPositionedReadable ) {
355- int bufPos = buf .position ();
356- final int n = ((ByteBufferPositionedReadable ) in ).read (position , buf );
357- if (n > 0 ) {
358- // This operation does not change the current offset of the file
359- decrypt (position , buf , n , bufPos );
360- }
361-
362- return n ;
363- } else {
353+ if (!(in instanceof ByteBufferPositionedReadable )) {
364354 throw new UnsupportedOperationException ("This stream does not support " +
365355 "positioned reads with byte buffers." );
366356 }
357+ int bufPos = buf .position ();
358+ final int n = ((ByteBufferPositionedReadable ) in ).read (position , buf );
359+ if (n > 0 ) {
360+ // This operation does not change the current offset of the file
361+ decrypt (position , buf , n , bufPos );
362+ }
363+
364+ return n ;
367365 }
368366
369367 /**
@@ -481,16 +479,15 @@ private void decrypt(long filePosition, ByteBuffer buf, int length, int start)
481479 public void readFully (long position , byte [] buffer , int offset , int length )
482480 throws IOException {
483481 checkStream ();
484- if (in instanceof PositionedReadable ) {
485- ((PositionedReadable ) in ).readFully (position , buffer , offset , length );
486- if (length > 0 ) {
487- // This operation does not change the current offset of the file
488- decrypt (position , buffer , offset , length );
489- }
490- } else {
482+ if (!(in instanceof PositionedReadable )) {
491483 throw new UnsupportedOperationException ("This stream does not support " +
492484 "positioned readFully." );
493485 }
486+ ((PositionedReadable ) in ).readFully (position , buffer , offset , length );
487+ if (length > 0 ) {
488+ // This operation does not change the current offset of the file
489+ decrypt (position , buffer , offset , length );
490+ }
494491 }
495492
496493 @ Override
@@ -515,13 +512,12 @@ public void seek(long pos) throws IOException {
515512 outBuffer .position (outBuffer .position () + forward );
516513 }
517514 } else {
518- if (in instanceof Seekable ) {
519- ((Seekable ) in ).seek (pos );
520- resetStreamOffset (pos );
521- } else {
515+ if (!(in instanceof Seekable )) {
522516 throw new UnsupportedOperationException ("This stream does not " +
523517 "support seek." );
524518 }
519+ ((Seekable ) in ).seek (pos );
520+ resetStreamOffset (pos );
525521 }
526522 }
527523
@@ -675,14 +671,13 @@ public boolean seekToNewSource(long targetPos) throws IOException {
675671 Preconditions .checkArgument (targetPos >= 0 ,
676672 "Cannot seek to negative offset." );
677673 checkStream ();
678- if (in instanceof Seekable ) {
679- boolean result = ((Seekable ) in ).seekToNewSource (targetPos );
680- resetStreamOffset (targetPos );
681- return result ;
682- } else {
674+ if (!(in instanceof Seekable )) {
683675 throw new UnsupportedOperationException ("This stream does not support " +
684676 "seekToNewSource." );
685677 }
678+ boolean result = ((Seekable ) in ).seekToNewSource (targetPos );
679+ resetStreamOffset (targetPos );
680+ return result ;
686681 }
687682
688683 @ Override
@@ -691,63 +686,58 @@ public ByteBuffer read(ByteBufferPool bufferPool, int maxLength,
691686 UnsupportedOperationException {
692687 checkStream ();
693688 if (outBuffer .remaining () > 0 ) {
694- if (in instanceof Seekable ) {
695- // Have some decrypted data unread, need to reset.
696- ((Seekable ) in ).seek (getPos ());
697- resetStreamOffset (getPos ());
698- } else {
689+ if (!(in instanceof Seekable )) {
699690 throw new UnsupportedOperationException ("This stream does not " +
700691 "support seek." );
701692 }
693+ // Have some decrypted data unread, need to reset.
694+ ((Seekable ) in ).seek (getPos ());
695+ resetStreamOffset (getPos ());
702696 }
703- if (in instanceof HasEnhancedByteBufferAccess ) {
704- final ByteBuffer buffer = ((HasEnhancedByteBufferAccess ) in ).
705- read (bufferPool , maxLength , opts );
706- if (buffer != null ) {
707- final int n = buffer .remaining ();
708- if (n > 0 ) {
709- streamOffset += buffer .remaining (); // Read n bytes
710- final int pos = buffer .position ();
711- decrypt (buffer , n , pos );
712- }
713- }
714- return buffer ;
715- } else {
697+ if (!(in instanceof HasEnhancedByteBufferAccess )) {
716698 throw new UnsupportedOperationException ("This stream does not support " +
717699 "enhanced byte buffer access." );
718700 }
701+ final ByteBuffer buffer = ((HasEnhancedByteBufferAccess ) in ).
702+ read (bufferPool , maxLength , opts );
703+ if (buffer != null ) {
704+ final int n = buffer .remaining ();
705+ if (n > 0 ) {
706+ streamOffset += buffer .remaining (); // Read n bytes
707+ final int pos = buffer .position ();
708+ decrypt (buffer , n , pos );
709+ }
710+ }
711+ return buffer ;
719712 }
720713
721714 @ Override
722715 public void releaseBuffer (ByteBuffer buffer ) {
723- if (in instanceof HasEnhancedByteBufferAccess ) {
724- ((HasEnhancedByteBufferAccess ) in ).releaseBuffer (buffer );
725- } else {
716+ if (!(in instanceof HasEnhancedByteBufferAccess )) {
726717 throw new UnsupportedOperationException ("This stream does not support " +
727718 "release buffer." );
728719 }
720+ ((HasEnhancedByteBufferAccess ) in ).releaseBuffer (buffer );
729721 }
730722
731723 @ Override
732724 public void setReadahead (Long readahead ) throws IOException ,
733725 UnsupportedOperationException {
734- if (in instanceof CanSetReadahead ) {
735- ((CanSetReadahead ) in ).setReadahead (readahead );
736- } else {
726+ if (!(in instanceof CanSetReadahead )) {
737727 throw new UnsupportedOperationException ("This stream does not support " +
738728 "setting the readahead caching strategy." );
739729 }
730+ ((CanSetReadahead ) in ).setReadahead (readahead );
740731 }
741732
742733 @ Override
743734 public void setDropBehind (Boolean dropCache ) throws IOException ,
744735 UnsupportedOperationException {
745- if (in instanceof CanSetReadahead ) {
746- ((CanSetDropBehind ) in ).setDropBehind (dropCache );
747- } else {
736+ if (!(in instanceof CanSetReadahead )) {
748737 throw new UnsupportedOperationException ("This stream does not " +
749738 "support setting the drop-behind caching setting." );
750739 }
740+ ((CanSetDropBehind ) in ).setDropBehind (dropCache );
751741 }
752742
753743 @ Override
@@ -851,12 +841,11 @@ public boolean hasCapability(String capability) {
851841 case StreamCapabilities .DROPBEHIND :
852842 case StreamCapabilities .READBYTEBUFFER :
853843 case StreamCapabilities .PREADBYTEBUFFER :
854- if (in instanceof StreamCapabilities ) {
855- return ((StreamCapabilities ) in ).hasCapability (capability );
856- } else {
844+ if (!(in instanceof StreamCapabilities )) {
857845 throw new UnsupportedOperationException ("This stream does not expose " +
858846 "its stream capabilities." );
859847 }
848+ return ((StreamCapabilities ) in ).hasCapability (capability );
860849 default :
861850 return false ;
862851 }
0 commit comments