-1

So what I'm trying to do is to save a byte[] into my database. If the byte is to long to be send to the database I whould like to split it into two bytes of half the size. However I'm doing something wrong, because when I check that the splittet bytes combined is equal to the original byte is ain't.

My code looks like this:

    public File Add(File item)
    {
        try
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            db.Entry(item).State = EntityState.Added;
            db.SaveChanges();
        }
        catch
        {
            File firstHalf = item;
            File lastHalf = item;
            firstHalf.@byte = item.@byte.Take(item.@byte.Length / 2).ToArray();
            lastHalf.@byte = item.@byte.Skip(item.@byte.Length / 2).ToArray();

            byte[] rv = new byte[firstHalf.@byte.Length + firstHalf.@byte.Length];
            System.Buffer.BlockCopy(firstHalf.@byte, 0, rv, 0, firstHalf.@byte.Length);
            System.Buffer.BlockCopy(lastHalf.@byte, 0, rv, firstHalf.@byte.Length, lastHalf.@byte.Length);

            if(rv == item.@byte)
            {
                Add(firstHalf);
                Add(lastHalf);
            }
        }

        return item;
    }


Edit

public partial class File
{
    public int C_id { get; set; }
    public string name { get; set; }
    public byte[] @byte { get; set; }
    public int file_id { get; set; }
    public int fk_folder { get; set; }
    public int fk_profile { get; set; }
}
Michael Tot Korsgaard
  • 3,892
  • 11
  • 53
  • 89

2 Answers2

3

To compare use rv.SequenceEqual(item.@byte) (or a loop) as currently you look to see if they are equal references (which they are not).

Also don't use an Exception for control-of-flow!

Alex K.
  • 171,639
  • 30
  • 264
  • 288
2

Problem starts here:

File firstHalf = item;
File lastHalf = item;
firstHalf.@byte = item.@byte.Take(item.@byte.Length / 2).ToArray();

You just creating new reference to item in first line, so then in third line you're corrupting original @byte.

You need to use deep copy of item instead.

One of possible approaches - create Clone method in File class which will be return new instance of File with copied fiedls, and instead of File firstHalf = item; it will be File firstHalf = item.Clone();

It is first approach came into my mind, probably not the best one. In fact, I can't estimate right now - do you really need two exact copies of your item - probably this logic should be refactored.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • 1
    This is just the first of a dozen mistakes :( – Ben Voigt Jan 13 '15 at 15:18
  • 1
    @BenVoigt, yes and most obvious one. In fact only this is enough to corrupt source data. – Andrey Korneyev Jan 13 '15 at 15:20
  • @BenVoigt: I would love if you could elaborate ;-) I know I'm not the best and would like to learn – Michael Tot Korsgaard Jan 13 '15 at 15:21
  • 2
    @MichaelTotKorsgaard: Well, the line immediately following does not skip the same number of items taken in the first set. And then it throws away half the remaining items. Size of `rv` is not computed correctly. Comparison is done incorrectly as Alex mentioned. – Ben Voigt Jan 13 '15 at 15:24