How to set the size of an array of bytes dynamically?

Asked

Viewed 792 times

3

I’m developing an application that creates a arquivo.txt and saved in a column of my table in the database. The problem is in the query of that file.

What happens, when I want to do the query I have to have an array of bytes with defined size, to receive this data from the database that are in byte format. But I can’t dynamically define this size.

I even used Int32.Maxvalue, but it’s giving memory error.

Anyone can help?

Follows the code.

public static void GetArqTxtBD(int idColumBD, string pstrNomeArqTxt)
{
    using (SqlConnection conn = ConexaoBD.CriarConexao())
    {
        using (SqlCommand cmd = new SqlCommand("uspCtzTesteSelectArqtxtBd", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@idTesteSalvar", SqlDbType.Int).Value = idColumBD;

            FileStream fs;                                  // Escreve o BLOB para o arquivo (*.txt).
            BinaryWriter bw;                                // Define um Streams para o objeto 
            int tamanhoBuffer = Int32.MaxValue;             // Tamanho do buffer do BLOB
            byte[] byteSaida = new byte[tamanhoBuffer];     // o buffer BLOB byte[] para ser preenchido com GetBytes.
            long retorno;                                   // Os bytes retornados de GetBytes.
            long inicioIndice = 0;                          // A posicao inicial no BLOB de saida

            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    // Cria o arquivo para tratar a saida dos dados
                    using (fs = new FileStream(pstrNomeArqTxt, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        using (bw = new BinaryWriter(fs))
                        {
                            // Reseta o byte de inicio para o novo BLOB.
                            inicioIndice = 0;

                            // Le os bytes no byteSaida[] e retem o numero de bytes retornados
                            retorno = reader.GetBytes(0, inicioIndice, byteSaida, 0, tamanhoBuffer);

                            // Continua lendo e escrevendo enquanto existir bytes ate completar o tamanho do buffer
                            while (retorno == tamanhoBuffer)
                            {
                                bw.Write(byteSaida);
                                bw.Flush();

                                //Reposiciona o inidice de inicio para o fim ultimo buffer e preenche o buffer
                                inicioIndice += tamanhoBuffer;
                                retorno = reader.GetBytes(0, inicioIndice, byteSaida, 0, tamanhoBuffer);
                            }
                            // Escreve o restante do buffer
                            bw.Write(byteSaida, 0, (int)retorno);
                            bw.Flush();
                        }
                    }
                }
            }
        }
    }
    Process.Start(pstrNomeArqTxt);
}
  • 1

    I was able to solve it, but I don’t know if it would be the best way. The code is the following: sizeBuffer = ((byte[])Reader.Getvalue(0)). Length; byteSaida = new byte[sizeBuffer]; In case now my array will have the size of the text file, and, there will be 2gb overflow of memory.

1 answer

2

The current implementation of System.Array use Int32 for all its internal meters etc, then the theoretical maximum number of elements is Int32.MaxValue.

But the . NET framework also has a maximum object size limit of 2GB, imposed by Microsoft CLR.

A good discussion and alternative solution here ...

And some related, questions and answers here...

  • 1

    I will look at these links you have made available. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.