-4

I am new in C# and I will really appreciate some help about 2 Java methods I need to implement in C#

public static String getTerminatedString(ByteBuffer buf) {
                return new String(getTerminatedArray(buf));
        }



public static byte[] getTerminatedArray(ByteBuffer buf) {
            int start = buf.position();

            while(buf.get() != 0) {}
            int end = buf.position();

            byte[] bytes = new byte[end - start - 1]; //don't include terminator
            buf.position(start);
            buf.get(bytes);

            //put position after array
            buf.position(end); //skip terminator

            return bytes;
    }

Thanks in advance!!!

UPDATED:

So far I wrote this:

 public static String getTerminatedString(byte[] buf) 
        {
            try
            {
                unsafe
                {
                    byte[] res = getTerminatedArray(buf);

                    fixed (byte* ptr_byte = &res[0]) // NullRef exception here
                    {
                        sbyte* ptr_sbyte = (sbyte*)ptr_byte;
                        return new String(ptr_sbyte);
                    }
                }
            }
            catch (NullReferenceException nre)
            {
                Console.Error.WriteLine(nre.Message + Environment.NewLine + nre.StackTrace);
                return null;
            }
        }

        public static byte[] getTerminatedArray(byte[] buf) 
        {
            try
            {

                int start = buf[0];
                int end = 0;
                for (int i = 0; i < buf.Length; i++)
                {
                    if (buf[i] == 0)
                    {
                        end = i;
                        break;
                    }
                }

                byte[] bytes = new byte[end - start - 1]; //overflow exeption here

                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = buf[i];
                }

                return bytes;
            }
            catch (OverflowException oe)
            {
                Console.Error.WriteLine(oe.Message + Environment.NewLine + oe.StackTrace);
                return null;
            }
        }

but I got the 2 exceptions marked above. I checked the logic behind these ByteBuffer methods and dunno what am I doing wrong >.>

So the nullreferenceexception is a result of the catch block return null. The problem is in byte[] bytes = new byte[end - start - 1]; (in case my implementation is correct... Is it? :D)

Borislav
  • 21
  • 3
  • 3
    Given that NIO doesn't really have any direct equivalent in .NET, you should really take a step back and think about the idiomatic way to implement the bigger picture. – Jon Skeet Jan 20 '13 at 16:49
  • 2
    It does not seem like you've put any effort into solving this problem yourself. Please don't expect us to do that for you. – Ameen Jan 20 '13 at 16:50
  • Try asking something more specific. – leticia Jan 20 '13 at 17:05
  • 1
    please-translate-this-code-for-me is frowned upon on stackoverflow – Michael Viktor Starberg Jan 20 '13 at 17:09
  • @MichaelViktorStarberg sorry, I'm new here as well, not only in c#... I noticed that so I updated my first post with my current implementation of the two methods. – Borislav Jan 20 '13 at 17:15
  • I understand the nullreferenceexception in from the return null in the catch block of getTerminatedString, so the problem is in byte[] bytes = new byte[end - start - 1]; in getTerminatedArray method – Borislav Jan 20 '13 at 17:22

3 Answers3

1

Provided your buffer is a Byte[] conversion of a String, to get back your text just use:

String myString = Encoding.UTF8.GetString(buffer);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
1

The ByteBuffer you have in Java is not the same as an array of byte in .NET Hence you need to reconsider your code. You are not even close to do what the Java code does.

One clue is in

int start = buf[0];
0

Tnx Michael!

I checked the ByteBuffer documentation in Java, also found some really interesting and useful C# ByteBuffer Implementation - http://www.codeforge.com/read/92854/ByteBuffer.cs__html

I added

public virtual byte[] Get(byte[] bytes)
    {
        long oldpos = Position;
        bytes = reader.ReadBytes(bytes.Length);
        Position = oldpos;

        return bytes;
    }

so I can use buf.get(bytes); //the MemoryStream position shouldn't change after this method is used, should it?

I tested it with

string str = "Test_tesT-Test" + char.MinValue;

and so far it correctly removes the terminator

Borislav
  • 21
  • 3