import Version from './Version' ; import ECBlocks from './ECBlocks' ; import ECB from './ECB' ; import ErrorCorrectionLevel from './ErrorCorrectionLevel' ; import IllegalArgumentException from '../../IllegalArgumentException' ; author Sean Owen
  export default class DataBlock { private constructor ( private numDataCodewords , private codewords ) { } param rawCodewords param version param ecLevel return public static getDataBlocks ( rawCodewords , version , ecLevel ) { if ( rawCodewords . length !== version . getTotalCodewords ( ) ) { throw new IllegalArgumentException ( ) ; } const ecBlocks = version . getECBlocksForLevel ( ecLevel ) ; let totalBlocks = 0 ; const ecBlockArray = ecBlocks . getECBlocks ( ) ; for ( const ecBlock of ecBlockArray ) { totalBlocks += ecBlock . getCount ( ) ; } const result = new Array < DataBlock > ( totalBlocks ) ; let numResultBlocks = 0 ; for ( const ecBlock of ecBlockArray ) { for ( let i = 0 ; i < ecBlock . getCount ( ) ; i ++ ) { const numDataCodewords = ecBlock . getDataCodewords ( ) ; const numBlockCodewords = ecBlocks . getECCodewordsPerBlock ( ) + numDataCodewords ; result [ numResultBlocks ++ ] = new DataBlock ( numDataCodewords , new Uint8Array ( numBlockCodewords ) ) ; } } const shorterBlocksTotalCodewords = result [ 0 ] . codewords . length ; let longerBlocksStartAt = result . length - 1 ; while ( longerBlocksStartAt >= 0 ) { const numCodewords = result [ longerBlocksStartAt ] . codewords . length ; if ( numCodewords === shorterBlocksTotalCodewords ) { break ; } longerBlocksStartAt -- ; } longerBlocksStartAt ++ ; const shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks . getECCodewordsPerBlock ( ) ; let rawCodewordsOffset = 0 ; for ( let i = 0 ; i < shorterBlocksNumDataCodewords ; i ++ ) { for ( let j = 0 ; j < numResultBlocks ; j ++ ) { result [ j ] . codewords [ i ] = rawCodewords [ rawCodewordsOffset ++ ] ; } } for ( let j = longerBlocksStartAt ; j < numResultBlocks ; j ++ ) { result [ j ] . codewords [ shorterBlocksNumDataCodewords ] = rawCodewords [ rawCodewordsOffset ++ ] ; } const max = result [ 0 ] . codewords . length ; for ( let i = shorterBlocksNumDataCodewords ; i < max ; i ++ ) { for ( let j = 0 ; j < numResultBlocks ; j ++ ) { const iOffset = j < longerBlocksStartAt ? i : i + 1 ; result [ j ] . codewords [ iOffset ] = rawCodewords [ rawCodewordsOffset ++ ] ; } } return result ; } public getNumDataCodewords ( ) { return this . numDataCodewords ; } public getCodewords ( ) { return this . codewords ; } }