Buggy code:
public class MemoryPostingsFormat extends PostingsFormat { public MemoryPostingsFormat ( ) { } private static final boolean VERBOSE = false ; private static final class TermsWriter extends TermsConsumer { private final IndexOutput out ; private final FieldInfo field ; private final Builder < BytesRef > builder ; private final ByteSequenceOutputs outputs = ByteSequenceOutputs . getSingleton ( ) ; private int termCount ; public TermsWriter ( IndexOutput out , FieldInfo field ) { } private class PostingsWriter extends PostingsConsumer { private int lastDocID ; private int lastPos ; private int lastPayloadLen ; int docCount ; RAMOutputStream buffer = new RAMOutputStream ( ) ; @ Override public void startDoc ( int docID , int termDocFreq ) throws IOException { } @ Override public void addPosition ( int pos , BytesRef payload ) throws IOException { } @ Override public void finishDoc ( ) { } public MemoryPostingsFormat . TermsWriter . PostingsWriter reset ( ) { } } private final MemoryPostingsFormat . TermsWriter . PostingsWriter postingsWriter = new MemoryPostingsFormat . TermsWriter . PostingsWriter ( ) ; @ Override public PostingsConsumer startTerm ( BytesRef text ) { } private final RAMOutputStream buffer2 = new RAMOutputStream ( ) ; private final BytesRef spare = new BytesRef ( ) ; private byte [ ] finalBuffer = new byte [ 128 ] ; @ Override public void finishTerm ( BytesRef text , TermStats stats ) throws IOException { assert ( postingsWriter . docCount ) == ( stats . docFreq ) ; assert ( buffer2 . getFilePointer ( ) ) == 0 ; buffer2 . writeVInt ( stats . docFreq ) ; if ( ( field . indexOptions ) != ( IndexOptions . DOCS_ONLY ) ) { buffer2 . writeVLong ( ( ( stats . totalTermFreq ) - ( stats . docFreq ) ) ) ; } int pos = ( ( int ) ( buffer2 . getFilePointer ( ) ) ) ; buffer2 . writeTo ( finalBuffer , 0 ) ; buffer2 . reset ( ) ; final int totalBytes = pos + ( ( int ) ( postingsWriter . buffer . getFilePointer ( ) ) ) ; if ( totalBytes > ( finalBuffer . length ) ) { finalBuffer = ArrayUtil . grow ( finalBuffer , totalBytes ) ; } postingsWriter . buffer . writeTo ( finalBuffer , pos ) ; postingsWriter . buffer . reset ( ) ; spare . bytes = finalBuffer ; spare . length = totalBytes ; if ( MemoryPostingsFormat . VERBOSE ) { System . out . println ( ( ( ( ( ( "<seq2seq4repair_space>finishTerm<seq2seq4repair_space>term=" + ( text . utf8ToString ( ) ) ) + "<seq2seq4repair_space>" ) + totalBytes ) + "<seq2seq4repair_space>bytes<seq2seq4repair_space>totalTF=" ) + ( stats . totalTermFreq ) ) ) ; for ( int i = 0 ; i < totalBytes ; i ++ ) { System . out . println ( ( "<seq2seq4repair_space>" + ( Integer . toHexString ( ( ( finalBuffer [ i ] ) & 255 ) ) ) ) ) ; } } <START_BUG> builder . add ( text , new BytesRef ( spare ) ) ; <END_BUG> ( termCount ) ++ ; } @ Override public void finish ( long sumTotalTermFreq , long sumDocFreq , int docCount ) throws IOException { } @ Override public Comparator < BytesRef > getComparator ( ) { } } private static String EXTENSION = "ram" ; @ Override public FieldsConsumer fieldsConsumer ( SegmentWriteState state ) throws IOException { } private static final class FSTDocsEnum extends DocsEnum { private final IndexOptions indexOptions ; private final boolean storePayloads ; private byte [ ] buffer = new byte [ 16 ] ; private final ByteArrayDataInput in = new ByteArrayDataInput ( buffer ) ; private Bits liveDocs ; private int docUpto ; private int docID = - 1 ; private int accum ; private int freq ; private int payloadLen ; private int numDocs ; public FSTDocsEnum ( IndexOptions indexOptions , boolean storePayloads ) { } public boolean canReuse ( IndexOptions indexOptions , boolean storePayloads ) { } public MemoryPostingsFormat . FSTDocsEnum reset ( BytesRef bufferIn , Bits liveDocs , int numDocs ) { } @ Override public int nextDoc ( ) { } @ Override public int docID ( ) { } @ Override public int advance ( int target ) { } @ Override public int freq ( ) { } } private static final class FSTDocsAndPositionsEnum extends DocsAndPositionsEnum { private final boolean storePayloads ; private byte [ ] buffer = new byte [ 16 ] ; private final ByteArrayDataInput in = new ByteArrayDataInput ( buffer ) ; private Bits liveDocs ; private int docUpto ; private int docID = - 1 ; private int accum ; private int freq ; private int numDocs ; private int posPending ; private int payloadLength ; private boolean payloadRetrieved ; private int pos ; private final BytesRef payload = new BytesRef ( ) ; public FSTDocsAndPositionsEnum ( boolean storePayloads ) { } public boolean canReuse ( boolean storePayloads ) { } public MemoryPostingsFormat . FSTDocsAndPositionsEnum reset ( BytesRef bufferIn , Bits liveDocs , int numDocs ) { } @ Override public int nextDoc ( ) { } @ Override public int nextPosition ( ) { } @ Override public BytesRef getPayload ( ) { } @ Override public boolean hasPayload ( ) { } @ Override public int docID ( ) { } @ Override public int advance ( int target ) { } @ Override public int freq ( ) { } } private static final class FSTTermsEnum extends TermsEnum { private final FieldInfo field ; private final BytesRefFSTEnum < BytesRef > fstEnum ; private final ByteArrayDataInput buffer = new ByteArrayDataInput ( ) ; private boolean didDecode ; private int docFreq ; private long totalTermFreq ; private BytesRefFSTEnum . InputOutput < BytesRef > current ; public FSTTermsEnum ( FieldInfo field , FST < BytesRef > fst ) { } private void decodeMetaData ( ) throws IOException { } @ Override public boolean seekExact ( BytesRef text , boolean useCache ) throws IOException { }

Fix:
builder . add ( text , BytesRef . deepCopyOf ( spare ) ) ;