本文整理汇总了Java中org.apache.lucene.store.IndexOutput.writeBytes方法的典型用法代码示例。如果您正苦于以下问题:Java IndexOutput.writeBytes方法的具体用法?Java IndexOutput.writeBytes怎么用?Java IndexOutput.writeBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.store.IndexOutput
的用法示例。
在下文中一共展示了IndexOutput.writeBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: appendRandomData
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
private void appendRandomData(IndexOutput output) throws IOException {
int numBytes = randomIntBetween(1, 1024);
final BytesRef ref = new BytesRef(scaledRandomIntBetween(1, numBytes));
ref.length = ref.bytes.length;
while (numBytes > 0) {
if (random().nextInt(10) == 0) {
output.writeByte(randomByte());
numBytes--;
} else {
for (int i = 0; i<ref.length; i++) {
ref.bytes[i] = randomByte();
}
final int min = Math.min(numBytes, ref.bytes.length);
output.writeBytes(ref.bytes, ref.offset, min);
numBytes -= min;
}
}
}
示例2: corruptFile
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
private void corruptFile(Directory dir, String fileIn, String fileOut) throws IOException {
IndexInput input = dir.openInput(fileIn, IOContext.READONCE);
IndexOutput output = dir.createOutput(fileOut, IOContext.DEFAULT);
long len = input.length();
byte[] b = new byte[1024];
long broken = randomInt((int) len-1);
long pos = 0;
while (pos < len) {
int min = (int) Math.min(input.length() - pos, b.length);
input.readBytes(b, 0, min);
if (broken >= pos && broken < pos + min) {
// Flip one byte
int flipPos = (int) (broken - pos);
b[flipPos] = (byte) (b[flipPos] ^ 42);
}
output.writeBytes(b, min);
pos += min;
}
IOUtils.close(input, output);
}
示例3: addPositions
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
private void addPositions(final DocsAndPositionsEnum in, final IndexOutput out) throws IOException {
int freq = in.freq();
out.writeVInt(freq);
int previousPosition = 0;
int previousEndOffset = 0;
for (int i = 0; i < freq; i++) {
final int pos = in.nextPosition();
final BytesRef payload = in.getPayload();
// The low-order bit of token is set only if there is a payload, the
// previous bits are the delta-encoded position.
final int token = (pos - previousPosition) << 1 | (payload == null ? 0 : 1);
out.writeVInt(token);
previousPosition = pos;
if (storeOffsets) { // don't encode offsets if they are not stored
final int startOffset = in.startOffset();
final int endOffset = in.endOffset();
out.writeVInt(startOffset - previousEndOffset);
out.writeVInt(endOffset - startOffset);
previousEndOffset = endOffset;
}
if (payload != null) {
out.writeVInt(payload.length);
out.writeBytes(payload.bytes, payload.offset, payload.length);
}
}
}
示例4: flushToIndexOutput
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
public void flushToIndexOutput(final IndexOutput indexOutput) throws IOException {
super.flush();
if (file.buffers.size() == 0) {
return;
}
if (file.buffers.size() == 1) {
indexOutput.writeBytes(file.buffers.get(0), (int) file.length);
return;
}
final int tempSize = file.buffers.size() - 1;
int i;
for (i = 0; i < tempSize; i++) {
indexOutput.writeBytes(file.buffers.get(i), bufferSize);
}
final int leftOver = (int) (file.length % bufferSize);
if (leftOver == 0) {
indexOutput.writeBytes(file.buffers.get(i), bufferSize);
} else {
indexOutput.writeBytes(file.buffers.get(i), leftOver);
}
}
示例5: testLargeWrites
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
/** This test that writes larger than the size of the buffer output
* will correctly increment the file pointer.
*/
public void testLargeWrites() throws IOException {
IndexOutput os = dir.createOutput("testBufferStart.txt", newIOContext(random()));
byte[] largeBuf = new byte[2048];
for (int i=0; i<largeBuf.length; i++) {
largeBuf[i] = (byte) (Math.random() * 256);
}
long currentPos = os.getFilePointer();
os.writeBytes(largeBuf, largeBuf.length);
try {
assertEquals(currentPos + largeBuf.length, os.getFilePointer());
} finally {
os.close();
}
}
示例6: testRawIndexInputRead
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
public void testRawIndexInputRead() throws IOException {
for (int i = 0; i < 10; i++) {
Random random = random();
final Directory dir = newDirectory();
IndexOutput os = dir.createOutput("foo", newIOContext(random));
os.writeBytes(READ_TEST_BYTES, READ_TEST_BYTES.length);
os.close();
IndexInput is = dir.openInput("foo", newIOContext(random));
checkReads(is, IOException.class);
is.close();
os = dir.createOutput("bar", newIOContext(random));
os.writeBytes(RANDOM_TEST_BYTES, RANDOM_TEST_BYTES.length);
os.close();
is = dir.openInput("bar", newIOContext(random));
checkRandomReads(is);
is.close();
dir.close();
}
}
示例7: testVerifyingIndexInput
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
public void testVerifyingIndexInput() throws IOException {
Directory dir = newDirectory();
IndexOutput output = dir.createOutput("foo.bar", IOContext.DEFAULT);
int iters = scaledRandomIntBetween(10, 100);
for (int i = 0; i < iters; i++) {
BytesRef bytesRef = new BytesRef(TestUtil.randomRealisticUnicodeString(random(), 10, 1024));
output.writeBytes(bytesRef.bytes, bytesRef.offset, bytesRef.length);
}
CodecUtil.writeFooter(output);
output.close();
// Check file
IndexInput indexInput = dir.openInput("foo.bar", IOContext.DEFAULT);
long checksum = CodecUtil.retrieveChecksum(indexInput);
indexInput.seek(0);
IndexInput verifyingIndexInput = new Store.VerifyingIndexInput(dir.openInput("foo.bar", IOContext.DEFAULT));
readIndexInputFullyWithRandomSeeks(verifyingIndexInput);
Store.verify(verifyingIndexInput);
assertThat(checksum, equalTo(((ChecksumIndexInput) verifyingIndexInput).getChecksum()));
IOUtils.close(indexInput, verifyingIndexInput);
// Corrupt file and check again
corruptFile(dir, "foo.bar", "foo1.bar");
verifyingIndexInput = new Store.VerifyingIndexInput(dir.openInput("foo1.bar", IOContext.DEFAULT));
readIndexInputFullyWithRandomSeeks(verifyingIndexInput);
try {
Store.verify(verifyingIndexInput);
fail("should be a corrupted index");
} catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
// ok
}
IOUtils.close(verifyingIndexInput);
IOUtils.close(dir);
}
示例8: readRAMFiles
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
/**
* Read a number of files from a data input to a ram directory.
* @param in the data input
* @param dir the ram directory
* @throws IOException
*/
public static void readRAMFiles(DataInput in, RAMDirectory dir) throws IOException {
int numFiles = in.readInt();
for (int i = 0; i < numFiles; i++) {
String name = Text.readString(in);
long length = in.readLong();
if (length > 0) {
// can we avoid the extra copy?
IndexOutput output = null;
try {
IOContext context = new IOContext();
output = dir.createOutput(name, context);
int position = 0;
byte[] buffer = new byte[BUFFER_SIZE];
while (position < length) {
int len = position + BUFFER_SIZE <= length ? BUFFER_SIZE : (int) (length - position);
in.readFully(buffer, 0, len);
output.writeBytes(buffer, 0, len);
position += len;
}
} finally {
if (output != null) {
output.close();
}
}
}
}
}
示例9: readRAMFiles
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
/**
* Read a number of files from a data input to a ram directory.
* @param in the data input
* @param dir the ram directory
* @throws IOException
*/
public static void readRAMFiles(DataInput in, RAMDirectory dir)
throws IOException {
int numFiles = in.readInt();
for (int i = 0; i < numFiles; i++) {
String name = Text.readString(in);
long length = in.readLong();
if (length > 0) {
// can we avoid the extra copy?
IndexOutput output = null;
try {
output = dir.createOutput(name);
int position = 0;
byte[] buffer = new byte[BUFFER_SIZE];
while (position < length) {
int len =
position + BUFFER_SIZE <= length ? BUFFER_SIZE
: (int) (length - position);
in.readFully(buffer, 0, len);
output.writeBytes(buffer, 0, len);
position += len;
}
} finally {
if (output != null) {
output.close();
}
}
}
}
}
示例10: addFixedStraightBytesField
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
private void addFixedStraightBytesField(FieldInfo field, IndexOutput output, Iterable<BytesRef> values, int length) throws IOException {
field.putAttribute(legacyKey, LegacyDocValuesType.BYTES_FIXED_STRAIGHT.name());
CodecUtil.writeHeader(output,
Lucene40DocValuesFormat.BYTES_FIXED_STRAIGHT_CODEC_NAME,
Lucene40DocValuesFormat.BYTES_FIXED_STRAIGHT_VERSION_CURRENT);
output.writeInt(length);
for (BytesRef v : values) {
if (v != null) {
output.writeBytes(v.bytes, v.offset, v.length);
}
}
}
示例11: PreFlexRWNormsConsumer
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
public PreFlexRWNormsConsumer(Directory directory, String segment, IOContext context) throws IOException {
final String normsFileName = IndexFileNames.segmentFileName(segment, "", NORMS_EXTENSION);
boolean success = false;
IndexOutput output = null;
try {
output = directory.createOutput(normsFileName, context);
output.writeBytes(NORMS_HEADER, 0, NORMS_HEADER.length);
out = output;
success = true;
} finally {
if (!success) {
IOUtils.closeWhileHandlingException(output);
}
}
}
示例12: copyFile
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
public void copyFile(Directory dir, String src, String dest) throws IOException {
IndexInput in = dir.openInput(src, newIOContext(random()));
IndexOutput out = dir.createOutput(dest, newIOContext(random()));
byte[] b = new byte[1024];
long remainder = in.length();
while(remainder > 0) {
int len = (int) Math.min(b.length, remainder);
in.readBytes(b, 0, len);
out.writeBytes(b, len);
remainder -= len;
}
in.close();
out.close();
}
示例13: testOverflow
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
@Ignore // memory hole
public void testOverflow() throws IOException {
BaseDirectoryWrapper dir = newFSDirectory(createTempDir("testOverflow"));
if (dir instanceof MockDirectoryWrapper) {
((MockDirectoryWrapper)dir).setThrottling(MockDirectoryWrapper.Throttling.NEVER);
}
final int blockBits = TestUtil.nextInt(random(), 14, 28);
final int blockSize = 1 << blockBits;
byte[] arr = new byte[TestUtil.nextInt(random(), blockSize / 2, blockSize * 2)];
for (int i = 0; i < arr.length; ++i) {
arr[i] = (byte) i;
}
final long numBytes = (1L << 31) + TestUtil.nextInt(random(), 1, blockSize * 3);
final PagedBytes p = new PagedBytes(blockBits);
final IndexOutput out = dir.createOutput("foo", IOContext.DEFAULT);
for (long i = 0; i < numBytes; ) {
assertEquals(i, out.getFilePointer());
final int len = (int) Math.min(arr.length, numBytes - i);
out.writeBytes(arr, len);
i += len;
}
assertEquals(numBytes, out.getFilePointer());
out.close();
final IndexInput in = dir.openInput("foo", IOContext.DEFAULT);
p.copy(in, numBytes);
final PagedBytes.Reader reader = p.freeze(random().nextBoolean());
for (long offset : new long[] {0L, Integer.MAX_VALUE, numBytes - 1,
TestUtil.nextLong(random(), 1, numBytes - 2)}) {
BytesRef b = new BytesRef();
reader.fillSlice(b, offset, 1);
assertEquals(arr[(int) (offset % arr.length)], b.bytes[b.offset]);
}
in.close();
dir.close();
}
示例14: flushBuffer
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
/**
* Flushes the in-memory bufer to the given output, copying at most
* <code>numBytes</code>.
* <p>
* <b>NOTE:</b> this method does not refill the buffer, however it does
* advance the buffer position.
*
* @return the number of bytes actually flushed from the in-memory buffer.
*/
protected int flushBuffer(IndexOutput out, long numBytes) throws IOException {
int toCopy = bufferLength - bufferPosition;
if (toCopy > numBytes) {
toCopy = (int) numBytes;
}
if (toCopy > 0) {
out.writeBytes(buffer, bufferPosition, toCopy);
bufferPosition += toCopy;
}
return toCopy;
}
示例15: writeBits
import org.apache.lucene.store.IndexOutput; //导入方法依赖的package包/类
/** Write as a bit set */
private void writeBits(IndexOutput output) throws IOException {
output.writeInt(size()); // write size
output.writeInt(count()); // write count
output.writeBytes(bits, bits.length);
}