Buggy code:
public void testGetArrayBytes ( ) throws IOException , CorruptIndexException { } private DocValues getDocValues ( IndexReader reader , String field ) throws IOException { } @ SuppressWarnings ( "fallthrough" ) private Source getSource ( DocValues values ) throws IOException { } private static EnumSet < Type > BYTES = EnumSet . of ( BYTES_FIXED_DEREF , BYTES_FIXED_STRAIGHT , BYTES_VAR_DEREF , BYTES_VAR_STRAIGHT , BYTES_FIXED_SORTED , BYTES_VAR_SORTED ) ; private static EnumSet < Type > NUMERICS = EnumSet . of ( VAR_INTS , FIXED_INTS_16 , FIXED_INTS_32 , FIXED_INTS_64 , FIXED_INTS_8 , FLOAT_32 , FLOAT_64 ) ; private FixedBitSet indexValues ( IndexWriter w , int numValues , Type valueType , List < Type > valueVarList , boolean withDeletions , int bytesSize ) throws IOException , CorruptIndexException { } public void testMultiValuedDocValuesField ( ) throws Exception { } public void testDifferentTypedDocValuesField ( ) throws Exception { } public void testSortedBytes ( ) throws IOException { Type [ ] types = new Type [ ] { Type . BYTES_FIXED_SORTED , Type . BYTES_VAR_SORTED } ; for ( Type type : types ) { boolean fixed = type == ( Type . BYTES_FIXED_SORTED ) ; final Directory d = newDirectory ( ) ; IndexWriterConfig cfg = newIndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . MockAnalyzer ( random ( ) ) ) ; IndexWriter w = new IndexWriter ( d , cfg ) ; int numDocs = atLeast ( 100 ) ; BytesRefHash hash = new BytesRefHash ( ) ; Map < String , String > docToString = new HashMap < String , String > ( ) ; int len = 1 + ( random ( ) . nextInt ( 50 ) ) ; for ( int i = 0 ; i < numDocs ; i ++ ) { Document doc = new Document ( ) ; doc . add ( newField ( "id" , ( "" + i ) , TYPE_STORED ) ) ; String string = ( fixed ) ? _TestUtil . randomFixedByteLengthUnicodeString ( random ( ) , len ) : _TestUtil . randomRealisticUnicodeString ( random ( ) , 1 , len ) ; BytesRef br = new BytesRef ( string ) ; doc . add ( new org . apache . lucene . document . SortedBytesDocValuesField ( "field" , br , ( type == ( Type . BYTES_FIXED_SORTED ) ) ) ) ; hash . add ( br ) ; docToString . put ( ( "" + i ) , string ) ; w . addDocument ( doc ) ; } if ( rarely ( ) ) { w . commit ( ) ; } int numDocsNoValue = atLeast ( 10 ) ; for ( int i = 0 ; i < numDocsNoValue ; i ++ ) { Document doc = new Document ( ) ; doc . add ( newField ( "id" , "noValue" , TYPE_STORED ) ) ; w . addDocument ( doc ) ; } BytesRef bytesRef = new BytesRef ( ( fixed ? len : 0 ) ) ; bytesRef . offset = 0 ; bytesRef . length = ( fixed ) ? len : 0 ; hash . add ( bytesRef ) ; if ( rarely ( ) ) { w . commit ( ) ; } for ( int i = 0 ; i < numDocs ; i ++ ) { Document doc = new Document ( ) ; String id = ( "" + i ) + numDocs ; doc . add ( newField ( "id" , id , TYPE_STORED ) ) ; String string = ( fixed ) ? _TestUtil . randomFixedByteLengthUnicodeString ( random ( ) , len ) : _TestUtil . randomRealisticUnicodeString ( random ( ) , 1 , len ) ; BytesRef br = new BytesRef ( string ) ; hash . add ( br ) ; docToString . put ( id , string ) ; doc . add ( new org . apache . lucene . document . SortedBytesDocValuesField ( "field" , br , ( type == ( Type . BYTES_FIXED_SORTED ) ) ) ) ; w . addDocument ( doc ) ; } w . commit ( ) ; IndexReader reader = w . getReader ( ) ; DocValues docValues = MultiDocValues . getDocValues ( reader , "field" ) ; Source source = getSource ( docValues ) ; SortedSource asSortedSource = source . asSortedSource ( ) ; int [ ] sort = hash . sort ( BytesRef . getUTF8SortedAsUnicodeComparator ( ) ) ; BytesRef expected = new BytesRef ( ) ; BytesRef actual = new BytesRef ( ) ; assertEquals ( hash . size ( ) , asSortedSource . getValueCount ( ) ) ; for ( int i = 0 ; i < ( hash . size ( ) ) ; i ++ ) { hash . get ( sort [ i ] , expected ) ; asSortedSource . getByOrd ( i , actual ) ; assertEquals ( expected . utf8ToString ( ) , actual . utf8ToString ( ) ) ; int ord = asSortedSource . getOrdByValue ( expected , actual ) ; assertEquals ( i , ord ) ; } AtomicReader slowR = SlowCompositeReaderWrapper . wrap ( reader ) ; Set < Map . Entry < String , String > > entrySet = docToString . entrySet ( ) ; for ( Map . Entry < String , String > entry : entrySet ) { int docId = docId ( slowR , new Term ( "id" , entry . getKey ( ) ) ) ; <START_BUG> expected . copyChars ( entry . getValue ( ) ) ; <END_BUG> assertEquals ( expected , asSortedSource . getBytes ( docId , actual ) ) ; } reader . close ( ) ; w . close ( ) ; d . close ( ) ; } } public int docId ( AtomicReader reader , Term term ) throws IOException { } public void testWithThreads ( ) throws Exception { } public void testLengthPrefixAcrossTwoPages ( ) throws Exception { } }

Fix:
expected = new BytesRef ( entry . getValue ( ) ) ;