@@ -244,6 +244,50 @@ void testRewindAcrossBuffers() throws IOException
244244 }
245245 }
246246
247+ @ Test
248+ void testRewindAcrossBuffers2 () throws IOException
249+ {
250+ byte [] ba = new byte [4096 * 2 ];
251+ ba [4095 ] = 1 ;
252+ ba [4096 ] = 2 ;
253+ ba [4097 ] = 3 ;
254+ ba [4096 * 2 - 1 ] = 4 ;
255+ ByteArrayInputStream bais = new ByteArrayInputStream (ba );
256+ try (RandomAccessRead rar = new NonSeekableRandomAccessReadInputStream (bais ))
257+ {
258+ assertEquals (0 , rar .length ()); // not really what I'd expect...
259+ int len = rar .read (new byte [4096 + 1 ]);
260+ assertEquals (4096 * 2 , rar .length ());
261+ assertEquals (4096 + 1 , len );
262+ rar .rewind (2 );
263+ assertEquals (1 , rar .read ());
264+ assertEquals (2 , rar .read ());
265+ assertEquals (3 , rar .read ());
266+ assertEquals (4096 * 2 , rar .length ());
267+
268+ byte [] buf = new byte [4096 ];
269+ len = rar .read (buf );
270+ assertEquals (4096 - 2 , len );
271+ assertEquals (4 , buf [len -1 ]);
272+ assertEquals (-1 , rar .read ());
273+ assertEquals (-1 , rar .read (new byte [1 ]));
274+ }
275+ }
276+
277+ @ Test
278+ void testAccessClosed () throws IOException
279+ {
280+ byte [] ba = new byte [1 ];
281+ ba [0 ] = 1 ;
282+ ByteArrayInputStream bais = new ByteArrayInputStream (ba );
283+ RandomAccessRead rar = new NonSeekableRandomAccessReadInputStream (bais );
284+ assertEquals (1 , rar .read ());
285+ assertEquals (-1 , rar .read ());
286+ rar .close ();
287+ Assertions .assertThrows (IOException .class , () -> rar .read (),
288+ "read() should have thrown an IOException" );
289+ }
290+
247291 private byte [] createRandomData ()
248292 {
249293 final long seed = new Random ().nextLong ();
0 commit comments