KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > MockInputStream


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.store.InputStream;
20
21 import java.io.IOException JavaDoc;
22
23 public class MockInputStream extends InputStream {
24     private byte[] buffer;
25     private int pointer = 0;
26
27     public MockInputStream(byte[] bytes) {
28         buffer = bytes;
29         length = bytes.length;
30     }
31
32     protected void readInternal(byte[] dest, int destOffset, int len)
33             throws IOException JavaDoc {
34         int remainder = len;
35         int start = pointer;
36         while (remainder != 0) {
37 // int bufferNumber = start / buffer.length;
38
int bufferOffset = start % buffer.length;
39           int bytesInBuffer = buffer.length - bufferOffset;
40           int bytesToCopy = bytesInBuffer >= remainder ? remainder : bytesInBuffer;
41           System.arraycopy(buffer, bufferOffset, dest, destOffset, bytesToCopy);
42           destOffset += bytesToCopy;
43           start += bytesToCopy;
44           remainder -= bytesToCopy;
45         }
46         pointer += len;
47     }
48
49     public void close() throws IOException JavaDoc {
50         // ignore
51
}
52
53     protected void seekInternal(long pos) throws IOException JavaDoc {
54         pointer = (int) pos;
55     }
56 }
57
Popular Tags