use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class TestCodecUtil method testWriteNonAsciiSuffix.
public void testWriteNonAsciiSuffix() throws Exception {
RAMFile file = new RAMFile();
IndexOutput output = new RAMOutputStream(file, true);
expectThrows(IllegalArgumentException.class, () -> {
CodecUtil.writeIndexHeader(output, "foobar", 5, StringHelper.randomId(), "ሴ");
});
}
use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class TestCodecUtil method testWriteVeryLongSuffix.
public void testWriteVeryLongSuffix() throws Exception {
StringBuilder justLongEnough = new StringBuilder();
for (int i = 0; i < 255; i++) {
justLongEnough.append('a');
}
RAMFile file = new RAMFile();
IndexOutput output = new RAMOutputStream(file, true);
byte[] id = StringHelper.randomId();
CodecUtil.writeIndexHeader(output, "foobar", 5, id, justLongEnough.toString());
output.close();
IndexInput input = new RAMInputStream("file", file);
CodecUtil.checkIndexHeader(input, "foobar", 5, 5, id, justLongEnough.toString());
assertEquals(input.getFilePointer(), input.length());
assertEquals(input.getFilePointer(), CodecUtil.indexHeaderLength("foobar", justLongEnough.toString()));
input.close();
}
use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class TestCodecUtil method testReadBogusCRC.
public void testReadBogusCRC() throws Exception {
RAMFile file = new RAMFile();
IndexOutput output = new RAMOutputStream(file, false);
// bad
output.writeLong(-1L);
// bad
output.writeLong(1L << 32);
// bad
output.writeLong(-(1L << 32));
// ok
output.writeLong((1L << 32) - 1);
output.close();
IndexInput input = new RAMInputStream("file", file);
// read 3 bogus values
for (int i = 0; i < 3; i++) {
expectThrows(CorruptIndexException.class, () -> {
CodecUtil.readCRC(input);
});
}
// good value
CodecUtil.readCRC(input);
}
use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class TestCodecUtil method testCheckFooterValidPastFooter.
public void testCheckFooterValidPastFooter() throws Exception {
RAMFile file = new RAMFile();
IndexOutput output = new RAMOutputStream(file, true);
CodecUtil.writeHeader(output, "FooBar", 5);
output.writeString("this is the data");
CodecUtil.writeFooter(output);
output.close();
ChecksumIndexInput input = new BufferedChecksumIndexInput(new RAMInputStream("file", file));
CodecUtil.checkHeader(input, "FooBar", 5, 5);
assertEquals("this is the data", input.readString());
// bogusly read a byte too far (can happen)
input.readByte();
Exception mine = new RuntimeException("fake exception");
RuntimeException expected = expectThrows(RuntimeException.class, () -> {
CodecUtil.checkFooter(input, mine);
});
assertEquals("fake exception", expected.getMessage());
Throwable[] suppressed = expected.getSuppressed();
assertEquals(1, suppressed.length);
assertTrue(suppressed[0].getMessage().contains("checksum status indeterminate"));
input.close();
}
use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class FST method save.
public void save(DataOutput out) throws IOException {
if (startNode == -1) {
throw new IllegalStateException("call finish first");
}
CodecUtil.writeHeader(out, FILE_FORMAT_NAME, VERSION_CURRENT);
// to the root node, instead of special casing here:
if (emptyOutput != null) {
// Accepts empty string
out.writeByte((byte) 1);
// Serialize empty-string output:
RAMOutputStream ros = new RAMOutputStream();
outputs.writeFinalOutput(emptyOutput, ros);
byte[] emptyOutputBytes = new byte[(int) ros.getFilePointer()];
ros.writeTo(emptyOutputBytes, 0);
// reverse
final int stopAt = emptyOutputBytes.length / 2;
int upto = 0;
while (upto < stopAt) {
final byte b = emptyOutputBytes[upto];
emptyOutputBytes[upto] = emptyOutputBytes[emptyOutputBytes.length - upto - 1];
emptyOutputBytes[emptyOutputBytes.length - upto - 1] = b;
upto++;
}
out.writeVInt(emptyOutputBytes.length);
out.writeBytes(emptyOutputBytes, 0, emptyOutputBytes.length);
} else {
out.writeByte((byte) 0);
}
final byte t;
if (inputType == INPUT_TYPE.BYTE1) {
t = 0;
} else if (inputType == INPUT_TYPE.BYTE2) {
t = 1;
} else {
t = 2;
}
out.writeByte(t);
out.writeVLong(startNode);
if (bytes != null) {
long numBytes = bytes.getPosition();
out.writeVLong(numBytes);
bytes.writeTo(out);
} else {
assert bytesArray != null;
out.writeVLong(bytesArray.length);
out.writeBytes(bytesArray, 0, bytesArray.length);
}
}
Aggregations