mirror of
https://github.com/apache/lucene.git
synced 2025-02-27 13:06:37 +00:00
Low-level InputStream test w/mock
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150003 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
624995b302
commit
7eb95be705
40
src/test/org/apache/lucene/index/MockInputStream.java
Normal file
40
src/test/org/apache/lucene/index/MockInputStream.java
Normal file
@ -0,0 +1,40 @@
|
||||
package org.apache.lucene.index;
|
||||
|
||||
import org.apache.lucene.store.InputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MockInputStream extends InputStream {
|
||||
private byte[] buffer;
|
||||
private int pointer = 0;
|
||||
|
||||
public MockInputStream(byte[] bytes) {
|
||||
buffer = bytes;
|
||||
length = bytes.length;
|
||||
}
|
||||
|
||||
protected void readInternal(byte[] dest, int destOffset, int len)
|
||||
throws IOException {
|
||||
int remainder = len;
|
||||
int start = pointer;
|
||||
while (remainder != 0) {
|
||||
// int bufferNumber = start / buffer.length;
|
||||
int bufferOffset = start % buffer.length;
|
||||
int bytesInBuffer = buffer.length - bufferOffset;
|
||||
int bytesToCopy = bytesInBuffer >= remainder ? remainder : bytesInBuffer;
|
||||
System.arraycopy(buffer, bufferOffset, dest, destOffset, bytesToCopy);
|
||||
destOffset += bytesToCopy;
|
||||
start += bytesToCopy;
|
||||
remainder -= bytesToCopy;
|
||||
}
|
||||
pointer += len;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
// ignore
|
||||
}
|
||||
|
||||
protected void seekInternal(long pos) throws IOException {
|
||||
pointer = (int) pos;
|
||||
}
|
||||
}
|
21
src/test/org/apache/lucene/index/TestInputStream.java
Normal file
21
src/test/org/apache/lucene/index/TestInputStream.java
Normal file
@ -0,0 +1,21 @@
|
||||
package org.apache.lucene.index;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.lucene.store.InputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TestInputStream extends TestCase {
|
||||
public void testRead() throws IOException {
|
||||
InputStream is = new MockInputStream(new byte[] { (byte) 0x80, 0x01,
|
||||
(byte) 0xFF, 0x7F,
|
||||
(byte) 0x80, (byte) 0x80, 0x01,
|
||||
(byte) 0x81, (byte) 0x80, 0x01,
|
||||
0x06, 'L', 'u', 'c', 'e', 'n', 'e'});
|
||||
assertEquals(128,is.readVInt());
|
||||
assertEquals(16383,is.readVInt());
|
||||
assertEquals(16384,is.readVInt());
|
||||
assertEquals(16385,is.readVInt());
|
||||
assertEquals("Lucene",is.readString());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user