Skip to content

Commit

Permalink
fix string with encoding create exception
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 16, 2024
1 parent c771a9e commit ef3bfb0
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/Source/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class Reader : IReader, IDisposable
#region Builder

// private for IDisposable
private Reader() { }
private Reader()
{
}

public Reader(byte[] buffer, int offset = 0)
{
Expand Down Expand Up @@ -344,8 +346,8 @@ public T Read<T>()

// skip y bytes read
_position += sizeof(float);


// get encoded value
var w = BitConverter.ToSingle(_buffer, _position);

Expand All @@ -355,42 +357,52 @@ public T Read<T>()
return (T)(object)new Float4(x, y, z, w);
}
}
catch { }
catch
{
//
}

_success = false;
return default;
}

public T Read<T>(Encoding encode)
{
char prefix = Writer.GetPrefix(typeof(T));

// string
if (typeof(T) == typeof(string))
try
{
// compare (buffer prefix) to (type prefix)
if (!ValidPrefix(prefix)) return default;
char prefix = Writer.GetPrefix(typeof(T));

// get length of bytes
int length = BitConverter.ToInt32(_buffer, _position);
_position += sizeof(int);
if (length <= 0 || length > (Length - _position))
// string
if (typeof(T) == typeof(string))
{
_success = false;
return default;
}
// compare (buffer prefix) to (type prefix)
if (!ValidPrefix(prefix)) return default;

// get encoded value
byte[] bytes = new byte[length];
Buffer.BlockCopy(_buffer, _position, bytes, 0, bytes.Length);
// get length of bytes
int length = BitConverter.ToInt32(_buffer, _position);
_position += sizeof(int);
if (length <= 0 || length > (Length - _position))
{
_success = false;
return default;
}

// skip bytes read
_position += bytes.Length;
// get encoded value
byte[] bytes = new byte[length];
Buffer.BlockCopy(_buffer, _position, bytes, 0, bytes.Length);

// skip bytes read
_position += bytes.Length;

// convert bytes to string
string value = encode.GetString(bytes);
// convert bytes to string
string value = encode.GetString(bytes);

return (T)(object)value;
return (T)(object)value;
}
}
catch
{
//
}

_success = false;
Expand Down Expand Up @@ -460,4 +472,4 @@ protected virtual void Dispose(bool disposing)

#endregion
}
}
}

0 comments on commit ef3bfb0

Please sign in to comment.