static BitArray interleaveWithECBytes(BitArray bits,int numTotalBytes,int numDataBytes,int numRSBlocks) throws WriterException {
  if (bits.getSizeInBytes() != numDataBytes) {
    throw new WriterException("Number of bits and data bytes does not match");
  }
  int dataBytesOffset=0;
  int maxNumDataBytes=0;
  int maxNumEcBytes=0;
  Collection<BlockPair> blocks=new ArrayList<>(numRSBlocks);
  for (int i=0; i < numRSBlocks; ++i) {
    int[] numDataBytesInBlock=new int[1];
    int[] numEcBytesInBlock=new int[1];
    getNumDataBytesAndNumECBytesForBlockID(numTotalBytes,numDataBytes,numRSBlocks,i,numDataBytesInBlock,numEcBytesInBlock);
    int size=numDataBytesInBlock[0];
    byte[] dataBytes=new byte[size];
    bits.toBytes(8 * dataBytesOffset,dataBytes,0,size);
    byte[] ecBytes=generateECBytes(dataBytes,numEcBytesInBlock[0]);
    blocks.add(new BlockPair(dataBytes,ecBytes));
    maxNumDataBytes=Math.max(maxNumDataBytes,size);
    maxNumEcBytes=Math.max(maxNumEcBytes,ecBytes.length);
    dataBytesOffset+=numDataBytesInBlock[0];
  }
  if (numDataBytes != dataBytesOffset) {
    throw new WriterException("Data bytes does not match offset");
  }
  BitArray result=new BitArray();
  for (int i=0; i < maxNumDataBytes; ++i) {
    for (    BlockPair block : blocks) {
      byte[] dataBytes=block.getDataBytes();
      if (i < dataBytes.length) {
        result.appendBits(dataBytes[i],8);
      }
    }
  }
  for (int i=0; i < maxNumEcBytes; ++i) {
    for (    BlockPair block : blocks) {
      byte[] ecBytes=block.getErrorCorrectionBytes();
      if (i < ecBytes.length) {
        result.appendBits(ecBytes[i],8);
      }
    }
  }
  if (numTotalBytes != result.getSizeInBytes()) {
    throw new WriterException("Interleaving error: " + numTotalBytes + " and "+ result.getSizeInBytes()+ " differ.");
  }
  return result;
}

static BitArray interleaveWithECBytes ( BitArray bits , int numTotalBytes , int numDataBytes , int numRSBlocks ) throws WriterException { if ( bits . getSizeInBytes ( ) != numDataBytes ) { throw new WriterException ( "Number of bits and data bytes does not match" ) ; } int dataBytesOffset = 0 ; int maxNumDataBytes = 0 ; int maxNumEcBytes = 0 ; Collection < BlockPair > blocks = new ArrayList < > ( numRSBlocks ) ; for ( int i = 0 ; i < numRSBlocks ; ++ i ) { int [ ] numDataBytesInBlock = new int [ 1 ] ; int [ ] numEcBytesInBlock = new int [ 1 ] ; getNumDataBytesAndNumECBytesForBlockID ( numTotalBytes , numDataBytes , numRSBlocks , i , numDataBytesInBlock , numEcBytesInBlock ) ; int size = numDataBytesInBlock [ 0 ] ; byte [ ] dataBytes = new byte [ size ] ; bits . toBytes ( 8 * dataBytesOffset , dataBytes , 0 , size ) ; byte [ ] ecBytes = generateECBytes ( dataBytes , numEcBytesInBlock [ 0 ] ) ; blocks . add ( new BlockPair ( dataBytes , ecBytes ) ) ; maxNumDataBytes = Math . max ( maxNumDataBytes , size ) ; maxNumEcBytes = Math . max ( maxNumEcBytes , ecBytes . length ) ; dataBytesOffset += numDataBytesInBlock [ 0 ] ; } if ( numDataBytes != dataBytesOffset ) { throw new WriterException ( "Data bytes does not match offset" ) ; } BitArray result = new BitArray ( ) ; for ( int i = 0 ; i < maxNumDataBytes ; ++ i ) { for ( BlockPair block : blocks ) { byte [ ] dataBytes = block . getDataBytes ( ) ; if ( i < dataBytes . length ) { result . appendBits ( dataBytes [ i ] , 8 ) ; } } } for ( int i = 0 ; i < maxNumEcBytes ; ++ i ) { for ( BlockPair block : blocks ) { byte [ ] ecBytes = block . getErrorCorrectionBytes ( ) ; if ( i < ecBytes . length ) { result . appendBits ( ecBytes [ i ] , 8 ) ; } } } if ( numTotalBytes != result . getSizeInBytes ( ) ) { throw new WriterException ( "Interleaving error: " + numTotalBytes + " and " + result . getSizeInBytes ( ) + " differ." ) ; } return result ; }
interleave " bits " with corresponding error correction bytes . on success , store the result in " result " . the interleave rule is complicated . see 8 . 6 of jisx0510 : 2004 ( p . 37 ) for details .
