/// <summary>
        /// Try to get the nGenotypeFields as efficiently as possible.
        ///
        /// If this is a lazy BCF2 object just grab the field count from there,
        /// otherwise do the whole counting by types test in the actual data
        /// </summary>
        /// <param name="vc">
        /// @return </param>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET:
//ORIGINAL LINE: private final int getNGenotypeFormatFields(final VariantContext vc)
        private int getNGenotypeFormatFields(VariantContext vc)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.broadinstitute.variant.bcf2.BCF2Codec.LazyData lazyData = getLazyData(vc);
            BCF2Codec.LazyData lazyData = getLazyData(vc);
            return(lazyData != null ? lazyData.nGenotypeFields : VCFWriter.calcVCFGenotypeKeys(vc, header).Count);
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private byte[] buildSamplesData(final VariantContext vc) throws IOException
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET:
        private sbyte[] buildSamplesData(VariantContext vc)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.broadinstitute.variant.bcf2.BCF2Codec.LazyData lazyData = getLazyData(vc);
            BCF2Codec.LazyData lazyData = getLazyData(vc);             // has critical side effects
            if (lazyData != null)
            {
                // we never decoded any data from this BCF file, so just pass it back
                return(lazyData.bytes);
            }

            // we have to do work to convert the VC into a BCF2 byte stream
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final List<String> genotypeFields = VCFWriter.calcVCFGenotypeKeys(vc, header);
            IList <string> genotypeFields = VCFWriter.calcVCFGenotypeKeys(vc, header);

            foreach (String field in genotypeFields)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final BCF2FieldWriter.GenotypesWriter writer = fieldManager.getGenotypeFieldWriter(field);
                BCF2FieldWriter.GenotypesWriter writer = fieldManager.getGenotypeFieldWriter(field);
                if (writer == null)
                {
                    errorUnexpectedFieldToWrite(vc, field, "FORMAT");
                }

                Debug.Assert(writer != null);

                writer.start(encoder, vc);
                foreach (String name in sampleNames)
                {
                    Genotype g = vc.getGenotype(name);
                    if (g == null)
                    {
                        g = GenotypeBuilder.createMissing(name, writer.nValuesPerGenotype);
                    }
                    writer.addGenotype(encoder, vc, g);
                }
                writer.done(encoder, vc);
            }
            return(encoder.RecordBytes);
        }
        // --------------------------------------------------------------------------------
        //
        // Interface functions
        //
        // --------------------------------------------------------------------------------

        public override void writeHeader(VCFHeader header)
        {
            // make sure the header is sorted correctly
            header = new VCFHeader(header.MetaDataInSortedOrder, header.GenotypeSampleNames);

            // create the config offsets map
            if (header.ContigLines.Count == 0)
            {
                if (ALLOW_MISSING_CONTIG_LINES)
                {
                    if (GeneralUtils.DEBUG_MODE_ENABLED)
                    {
                        Console.Error.WriteLine("No contig dictionary found in header, falling back to reference sequence dictionary");
                    }
                    createContigDictionary(VCFUtils.makeContigHeaderLines(RefDict, null));
                }
                else
                {
                    throw new IllegalStateException("Cannot write BCF2 file with missing contig lines");
                }
            }
            else
            {
                createContigDictionary(header.ContigLines);
            }

            // set up the map from dictionary string values -> offset
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final ArrayList<String> dict = org.broadinstitute.variant.bcf2.BCF2Utils.makeDictionary(header);
            List <string> dict = BCF2Utils.makeDictionary(header);

            for (int i = 0; i < dict.Count; i++)
            {
                stringDictionaryMap[dict[i]] = i;
            }

            sampleNames = header.GenotypeSampleNames.ToArray();

            // setup the field encodings
            fieldManager.setup(header, encoder, stringDictionaryMap);

            try
            {
                // write out the header into a byte stream, get it's length, and write everything to the file
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final ByteArrayOutputStream capture = new ByteArrayOutputStream();
                ByteArrayOutputStream capture = new ByteArrayOutputStream();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final OutputStreamWriter writer = new OutputStreamWriter(capture);
                OutputStreamWriter writer = new OutputStreamWriter(capture);
                this.header = VCFWriter.writeHeader(header, writer, doNotWriteGenotypes, VCFWriter.VersionLine, "BCF2 stream");
                writer.append('\0');                 // the header is null terminated by a byte
                writer.close();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final byte[] headerBytes = capture.toByteArray();
                sbyte[] headerBytes = capture.toByteArray();
                (new BCFVersion(MAJOR_VERSION, MINOR_VERSION)).write(outputStream);
                BCF2Type.INT32.write(headerBytes.Length, outputStream);
                outputStream.write(headerBytes);
            }
            catch (IOException e)
            {
                throw new Exception("BCF2 stream: Got IOException while trying to write BCF2 header", e);
            }
        }