import GenericGF from './GenericGF' ; import GenericGFPoly from './GenericGFPoly' ; import Exception from './../../Exception' ; import System from './../../util/System' ; author Sean Owen
 *  author William Rucklidge
  export default class ReedSolomonEncoder { private field : GenericGF ; private cachedGenerators : GenericGFPoly [ ] ; public constructor ( field ) { this . field = field ; this . cachedGenerators = [ ] ; this . cachedGenerators . push ( new GenericGFPoly ( field , Int32Array . from ( [ 1 ] ) ) ) ; } private buildGenerator ( degree ) { const cachedGenerators = this . cachedGenerators ; if ( degree >= cachedGenerators . length ) { let lastGenerator = cachedGenerators [ cachedGenerators . length - 1 ] ; const field = this . field ; for ( let d = cachedGenerators . length ; d <= degree ; d ++ ) { const nextGenerator = lastGenerator . multiply ( new GenericGFPoly ( field , Int32Array . from ( [ 1 , field . exp ( d - 1 + field . getGeneratorBase ( ) ) ] ) ) ) ; cachedGenerators . push ( nextGenerator ) ; lastGenerator = nextGenerator ; } } return cachedGenerators [ degree ] ; } public encode ( toEncode , ecBytes ) { if ( ecBytes === 0 ) { throw new Exception ( Exception . IllegalArgumentException , "No error correction bytes" ) ; } const dataBytes = toEncode . length - ecBytes ; if ( dataBytes <= 0 ) { throw new Exception ( Exception . IllegalArgumentException , "No data bytes provided" ) ; } const generator = this . buildGenerator ( ecBytes ) ; const infoCoefficients = new Int32Array ( dataBytes ) ; System . arraycopy ( toEncode , 0 , infoCoefficients , 0 , dataBytes ) ; let info = new GenericGFPoly ( this . field , infoCoefficients ) ; info = info . multiplyByMonomial ( ecBytes , 1 ) ; const remainder = info . divide ( generator ) [ 1 ] ; const coefficients = remainder . getCoefficients ( ) ; const numZeroCoefficients = ecBytes - coefficients . length ; for ( let i = 0 ; i < numZeroCoefficients ; i ++ ) { toEncode [ dataBytes + i ] = 0 ; } System . arraycopy ( coefficients , 0 , toEncode , dataBytes + numZeroCoefficients , coefficients . length ) ; } }