Remove BOF and EOF from byte[]

  • Thread starter Thread starter TJO
  • Start date Start date
T

TJO

Can someone recomend a good technique for removing BOF and EOF markers from
a byte[]? I am passing a byte[] thru an FTP socket. I am serializing an
object into a byte[] and then adding an EOF marker and a BOF marker to the
byte[]. On the receiving end I need to remove the markers and then
deserialize my object. My object does not seem to deserialize so I am not
sure I am removing the EOF and BOF properly. All advice welcome. Thank
you.

Here is the code snippet.

byte[] bof = Encoding.Ascii.GetBytes("<BOF>");
byte[] eof = Encoding.Ascii.GetBytes("<EOF>");
byte[] myobjbytes = myobj.Serialize();

// Create empty byte[] of combined objects
byte[] combinedBytes = new byte[ eof.Length + myobjbytes.Lengh +
bof.Length ];

// Copy contents of objects to empty byte[]
System.Buffer.BlockCopy(
bof, 0,
myobjbytes, 0, bof.Length);

System.Buffer.BlockCopy(
myobjbytes , 0,
combinedBytes, bof.Length, myobjbytes .Length);

System.Buffer.BlockCopy(
eof, 0,
combinedBytes, bof.Length + myobjbytes.Length, eof.Length);

// Send byte[] thru the socket

// Now I want to trim off the EOF and BOF then Deserialize my object.
// Here is my attept but the remaining object does not like to be
deserialized :(

private byte[] TrimBOF_EOF(string BOF, string EOF, byte[] bytes)
{
string s = new string(Encoding.ASCII.GetChars(bytes));

s = s.Remove(0, BOF.Length);

// Get position of EOF
int EOFPos = s.LastIndexOf(EOF);

// Trim EOF mark thru the end of the string
if(EOFPos > -1)
s = s.Remove(s.Length-EOF.Length, EOF.Length);

return Encoding.ASCII.GetBytes(s);
}
 
TJO said:
Can someone recomend a good technique for removing BOF and EOF markers from
a byte[]? I am passing a byte[] thru an FTP socket. I am serializing an
object into a byte[] and then adding an EOF marker and a BOF marker to the
byte[]. On the receiving end I need to remove the markers and then
deserialize my object. My object does not seem to deserialize so I am not
sure I am removing the EOF and BOF properly. All advice welcome. Thank
you.

The fact that you're turning into a string and back is probably what's
going wrong - the top bit is going to be removed from every byte.

Why not just use (for your TrimBOF_EOF method):

byte[] ret = new byte[bytes.Length-BOF.Length-EOF.Length];
Buffer.BlockCopy (bytes, BOF.Length, ret, 0, ret.Length);
return ret;
 
I was thinking the same thing. I'll try it.

Jon Skeet said:
TJO said:
Can someone recomend a good technique for removing BOF and EOF markers
from
a byte[]? I am passing a byte[] thru an FTP socket. I am serializing an
object into a byte[] and then adding an EOF marker and a BOF marker to
the
byte[]. On the receiving end I need to remove the markers and then
deserialize my object. My object does not seem to deserialize so I am
not
sure I am removing the EOF and BOF properly. All advice welcome. Thank
you.

The fact that you're turning into a string and back is probably what's
going wrong - the top bit is going to be removed from every byte.

Why not just use (for your TrimBOF_EOF method):

byte[] ret = new byte[bytes.Length-BOF.Length-EOF.Length];
Buffer.BlockCopy (bytes, BOF.Length, ret, 0, ret.Length);
return ret;
 
Works Great thanks!
TJO said:
I was thinking the same thing. I'll try it.

Jon Skeet said:
TJO said:
Can someone recomend a good technique for removing BOF and EOF markers
from
a byte[]? I am passing a byte[] thru an FTP socket. I am serializing
an
object into a byte[] and then adding an EOF marker and a BOF marker to
the
byte[]. On the receiving end I need to remove the markers and then
deserialize my object. My object does not seem to deserialize so I am
not
sure I am removing the EOF and BOF properly. All advice welcome. Thank
you.

The fact that you're turning into a string and back is probably what's
going wrong - the top bit is going to be removed from every byte.

Why not just use (for your TrimBOF_EOF method):

byte[] ret = new byte[bytes.Length-BOF.Length-EOF.Length];
Buffer.BlockCopy (bytes, BOF.Length, ret, 0, ret.Length);
return ret;
 
Back
Top