@@ -64,7 +64,7 @@ public long writeTo( String filename ) throws IOException {
6464 * @throws IOException
6565 */
6666 public long writeTo ( File f ) throws IOException {
67-
67+
6868 FileOutputStream out = null ;
6969 try {
7070 out = new FileOutputStream ( f );
@@ -138,6 +138,7 @@ public int read(){
138138 public int read (byte [] b ){
139139 return read ( b , 0 , b .length );
140140 }
141+
141142 public int read (byte [] b , int off , int len ){
142143
143144 if ( _data == null || _offset >= _data .length ){
@@ -166,35 +167,30 @@ public long skip(long numBytesToSkip) throws IOException {
166167 //Don't count those extra bytes to skip in with the return value
167168 return 0 ;
168169
169- if ( _offset + numBytesToSkip <= _chunkSize ) {
170- //We're skipping over bytes in the current chunk, adjust the offset accordingly
171- _offset += numBytesToSkip ;
172- if ( _data == null && _currentChunkIdx < _numChunks )
173- _data = getChunk ( _currentChunkIdx );
174-
175- return numBytesToSkip ;
170+ // offset in the whole file
171+ long offsetInFile = 0 ;
172+ if ( _currentChunkIdx >= 0 )
173+ offsetInFile = _currentChunkIdx * _chunkSize + _offset ;
174+ if ( numBytesToSkip + offsetInFile >= _length ) {
175+ _currentChunkIdx = _numChunks ;
176+ return _length - offsetInFile ;
176177 }
177178
178- //We skipping over the remainder of this chunk, could do this less recursively...
179- ++_currentChunkIdx ;
180- long skippedBytes = 0 ;
181- if (_currentChunkIdx < _numChunks )
182- skippedBytes = _chunkSize - _offset ;
183- else
184- skippedBytes = _lastChunkSize ;
185-
186- _offset = 0 ;
187- _data = null ;
179+ int temp = _currentChunkIdx ;
180+ _currentChunkIdx = (int )((numBytesToSkip + offsetInFile ) / _chunkSize );
181+ if (temp != _currentChunkIdx )
182+ _data = getChunk (_currentChunkIdx );
183+ _offset = (int )((numBytesToSkip + offsetInFile ) % _chunkSize );
188184
189- return skippedBytes + skip ( numBytesToSkip - skippedBytes ) ;
185+ return numBytesToSkip ;
190186 }
191187
192188 final int _numChunks ;
193189 //Math trick to ensure the _lastChunkSize is between 1 and _chunkSize
194- final long _lastChunkSize = ((_length - 1 ) % _chunkSize ) + 1 ;
190+ // final long _lastChunkSize = ((_length - 1) % _chunkSize) + 1;
195191
196192 int _currentChunkIdx = -1 ;
197- int _offset ;
193+ int _offset = 0 ;
198194 byte [] _data = null ;
199195 }
200196
0 commit comments