@@ -167,10 +167,11 @@ public void testBadChunkSize() throws Exception {
167167 fileSize = 10 * 1024 * 1024 ;
168168
169169 byte [] randomBytes = new byte [fileSize ];
170- for (int idx = 0 ; idx < 2 * GridFS . MAX_CHUNKSIZE ; ++idx )
170+ for (int idx = 0 ; idx < fileSize ; ++idx )
171171 randomBytes [idx ] = (byte )(256 * Math .random ());
172172
173173 GridFSInputFile inputFile = _fs .createFile (randomBytes );
174+ inputFile .setFilename ("bad_chunk_size.bin" );
174175 try {
175176 inputFile .save (0 );
176177 fail ("should have received an exception about a chunk size being zero" );
@@ -197,6 +198,66 @@ public void testBadChunkSize() throws Exception {
197198 assertArrayEquals (randomBytes , savedFileBytes );
198199 }
199200
201+ @ Test (groups = {"basic" })
202+ public void testInputStreamSkipping () throws Exception {
203+ //int chunkSize = 5;
204+ int chunkSize = GridFS .DEFAULT_CHUNKSIZE ;
205+ int fileSize = 7 * chunkSize ;
206+
207+
208+ byte [] fileBytes = new byte [fileSize ];
209+ for (int idx = 0 ; idx < fileSize ; ++idx )
210+ fileBytes [idx ] = (byte )(idx % 251 );
211+ //Don't want chunks to be aligned at byte position 0
212+
213+ GridFSInputFile inputFile = _fs .createFile (fileBytes );
214+ inputFile .setFilename ("input_stream_skipping.bin" );
215+ inputFile .save (chunkSize );
216+
217+ GridFSDBFile savedFile = _fs .findOne (new BasicDBObject ("_id" , inputFile .getId ()));
218+ GridFSDBFile .MyInputStream inputStream = (GridFSDBFile .MyInputStream )savedFile .getInputStream ();
219+
220+ //Quick run-through, make sure the file is as expected
221+ for (int idx = 0 ; idx < fileSize ; ++idx )
222+ assertEquals ((byte )(idx % 251 ), (byte )inputStream .read ());
223+
224+ inputStream = (GridFSDBFile .MyInputStream )savedFile .getInputStream ();
225+
226+ int position = 0 ;
227+ assertEquals ((byte )(position ++ % 251 ), (byte )inputStream .read ());
228+
229+ long skipped = inputStream .skip (1 );
230+ assertEquals (1 , skipped );
231+ position += 1 ;
232+ assertEquals ((byte )(position ++ % 251 ), (byte )inputStream .read ());
233+
234+ skipped = inputStream .skip (chunkSize );
235+ assertEquals (chunkSize , skipped );
236+ position += chunkSize ;
237+ assertEquals ((byte )(position ++ % 251 ), (byte )inputStream .read ());
238+
239+ skipped = inputStream .skip (-1 );
240+ assertEquals (0 , skipped );
241+ skipped = inputStream .skip (0 );
242+ assertEquals (0 , skipped );
243+
244+ skipped = inputStream .skip (3 * chunkSize );
245+ assertEquals (3 * chunkSize , skipped );
246+ position += 3 * chunkSize ;
247+ assertEquals ((byte )(position ++ % 251 ), (byte )inputStream .read ());
248+
249+ //Make sure skipping works when we skip to an exact chunk boundary
250+ long toSkip = inputStream .available ();
251+ skipped = inputStream .skip (toSkip );
252+ assertEquals (toSkip , skipped );
253+ position += toSkip ;
254+ assertEquals ((byte )(position ++ % 251 ), (byte )inputStream .read ());
255+
256+ skipped = inputStream .skip (2 * fileSize );
257+ assertEquals (fileSize - position , skipped );
258+ assertEquals (-1 , inputStream .read ());
259+ }
260+
200261 final DB _db ;
201262 final GridFS _fs ;
202263
0 commit comments